Skip to content

Commit 4090374

Browse files
meorphismeorphis
andauthored
just use regex to replace version in package json (#196)
Co-authored-by: meorphis <[email protected]>
1 parent 5d68d63 commit 4090374

File tree

3 files changed

+161
-7
lines changed

3 files changed

+161
-7
lines changed

src/updaters/node/package-json.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {jsonStringify} from '../../util/json-stringify';
1615
import {logger as defaultLogger, Logger} from '../../util/logger';
1716
import {DefaultUpdater} from '../default';
1817

19-
type LockFile = {version: string};
20-
2118
/**
2219
* This updates a Node.js package.json file's main version.
2320
*/
@@ -28,9 +25,7 @@ export class PackageJson extends DefaultUpdater {
2825
* @returns {string} The updated content
2926
*/
3027
updateContent(content: string, logger: Logger = defaultLogger): string {
31-
const parsed = JSON.parse(content) as LockFile;
32-
logger.info(`updating from ${parsed.version} to ${this.version}`);
33-
parsed.version = this.version.toString();
34-
return jsonStringify(parsed, content);
28+
logger.info(`updating to ${this.version}`);
29+
return content.replace(/"version": ".*"/, `"version": "${this.version}"`);
3530
}
3631
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"name": "@nirvana-labs/nirvana",
3+
"version": "0.1.0",
4+
"description": "The official TypeScript library for the Nirvana Labs API",
5+
"author": "Nirvana Labs <[email protected]>",
6+
"types": "dist/index.d.ts",
7+
"main": "dist/index.js",
8+
"type": "commonjs",
9+
"repository": "github:nirvana-labs/nirvana-node",
10+
"license": "Apache-2.0",
11+
"packageManager": "[email protected]",
12+
"files": [
13+
"**/*"
14+
],
15+
"private": false,
16+
"scripts": {
17+
"test": "./scripts/test",
18+
"build": "./scripts/build",
19+
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
20+
"format": "prettier --write --cache --cache-strategy metadata . !dist",
21+
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build && ./scripts/utils/git-swap.sh; fi",
22+
"tsn": "ts-node -r tsconfig-paths/register",
23+
"lint": "./scripts/lint",
24+
"fix": "./scripts/format"
25+
},
26+
"dependencies": {
27+
"@types/node": "^18.11.18",
28+
"@types/node-fetch": "^2.6.4",
29+
"abort-controller": "^3.0.0",
30+
"agentkeepalive": "^4.2.1",
31+
"form-data-encoder": "1.7.2",
32+
"formdata-node": "^4.3.2",
33+
"node-fetch": "^2.6.7"
34+
},
35+
"devDependencies": {
36+
"@swc/core": "^1.3.102",
37+
"@swc/jest": "^0.2.29",
38+
"@types/jest": "^29.4.0",
39+
"@typescript-eslint/eslint-plugin": "^6.7.0",
40+
"@typescript-eslint/parser": "^6.7.0",
41+
"eslint": "^8.49.0",
42+
"eslint-plugin-prettier": "^5.0.1",
43+
"eslint-plugin-unused-imports": "^3.0.0",
44+
"iconv-lite": "^0.6.3",
45+
"jest": "^29.4.0",
46+
"prettier": "^3.0.0",
47+
"ts-jest": "^29.1.0",
48+
"ts-node": "^10.5.0",
49+
"tsc-multi": "^1.1.0",
50+
"tsconfig-paths": "^4.0.0",
51+
"typescript": "^4.8.2"
52+
},
53+
"sideEffects": [
54+
"./_shims/index.js",
55+
"./_shims/index.mjs",
56+
"./shims/node.js",
57+
"./shims/node.mjs",
58+
"./shims/web.js",
59+
"./shims/web.mjs"
60+
],
61+
"imports": {
62+
"@nirvana-labs/nirvana": ".",
63+
"@nirvana-labs/nirvana/*": "./src/*"
64+
},
65+
"exports": {
66+
"./_shims/auto/*": {
67+
"deno": {
68+
"types": "./dist/_shims/auto/*.d.ts",
69+
"require": "./dist/_shims/auto/*.js",
70+
"default": "./dist/_shims/auto/*.mjs"
71+
},
72+
"bun": {
73+
"types": "./dist/_shims/auto/*.d.ts",
74+
"require": "./dist/_shims/auto/*-bun.js",
75+
"default": "./dist/_shims/auto/*-bun.mjs"
76+
},
77+
"browser": {
78+
"types": "./dist/_shims/auto/*.d.ts",
79+
"require": "./dist/_shims/auto/*.js",
80+
"default": "./dist/_shims/auto/*.mjs"
81+
},
82+
"worker": {
83+
"types": "./dist/_shims/auto/*.d.ts",
84+
"require": "./dist/_shims/auto/*.js",
85+
"default": "./dist/_shims/auto/*.mjs"
86+
},
87+
"workerd": {
88+
"types": "./dist/_shims/auto/*.d.ts",
89+
"require": "./dist/_shims/auto/*.js",
90+
"default": "./dist/_shims/auto/*.mjs"
91+
},
92+
"node": {
93+
"types": "./dist/_shims/auto/*-node.d.ts",
94+
"require": "./dist/_shims/auto/*-node.js",
95+
"default": "./dist/_shims/auto/*-node.mjs"
96+
},
97+
"types": "./dist/_shims/auto/*.d.ts",
98+
"require": "./dist/_shims/auto/*.js",
99+
"default": "./dist/_shims/auto/*.mjs"
100+
},
101+
".": {
102+
"require": {
103+
"types": "./dist/index.d.ts",
104+
"default": "./dist/index.js"
105+
},
106+
"types": "./dist/index.d.mts",
107+
"default": "./dist/index.mjs"
108+
},
109+
"./*.mjs": {
110+
"types": [
111+
"./dist/*.d.ts",
112+
"./dist/*/index.d.ts"
113+
],
114+
"default": [
115+
"./dist/*.mjs",
116+
"./dist/*/index.mjs"
117+
]
118+
},
119+
"./*.js": {
120+
"types": [
121+
"./dist/*.d.ts",
122+
"./dist/*/index.d.ts"
123+
],
124+
"default": [
125+
"./dist/*.js",
126+
"./dist/*/index.js"
127+
]
128+
},
129+
"./*": {
130+
"types": [
131+
"./dist/*.d.ts",
132+
"./dist/*/index.d.ts"
133+
],
134+
"require": [
135+
"./dist/*.js",
136+
"./dist/*/index.js"
137+
],
138+
"default": [
139+
"./dist/*.mjs",
140+
"./dist/*/index.mjs"
141+
]
142+
}
143+
}
144+
}

test/updaters/package-json.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import snapshot = require('snap-shot-it');
1818
import {describe, it} from 'mocha';
1919
import {PackageJson} from '../../src/updaters/node/package-json';
2020
import {Version} from '../../src/version';
21+
import {expect} from 'chai';
2122

2223
const fixturesPath = './test/updaters/fixtures';
2324

@@ -34,5 +35,19 @@ describe('PackageJson', () => {
3435
const newContent = packageJson.updateContent(oldContent);
3536
snapshot(newContent.replace(/\r\n/g, '\n'));
3637
});
38+
39+
it('does not reformat the content', async () => {
40+
const oldContent = readFileSync(
41+
resolve(fixturesPath, './package-formatting.json'),
42+
'utf8'
43+
);
44+
const packageJson = new PackageJson({
45+
version: Version.parse('0.1.1'),
46+
});
47+
const newContent = packageJson.updateContent(oldContent);
48+
expect(
49+
oldContent.replace('"version": "0.1.0"', '"version": "0.1.1"')
50+
).to.eq(newContent);
51+
});
3752
});
3853
});

0 commit comments

Comments
 (0)