Skip to content

Commit abcc70b

Browse files
committed
fix: preserve package.json and npm-shrinkwrap.json formatting
1 parent fc62671 commit abcc70b

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

lib/update-package-version.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
const {readJson, writeJson, pathExists} = require('fs-extra');
1+
const {readFile, writeFile, pathExists} = require('fs-extra');
22

33
module.exports = async (version, logger) => {
4-
const pkg = await readJson('./package.json');
4+
const pkg = (await readFile('./package.json')).toString();
55

6-
await writeJson('./package.json', Object.assign(pkg, {version}));
6+
await writeFile('./package.json', replaceVersion(pkg, version));
77
logger.log('Wrote version %s to package.json', version);
88

99
if (await pathExists('./npm-shrinkwrap.json')) {
10-
const shrinkwrap = await readJson('./npm-shrinkwrap.json');
11-
shrinkwrap.version = version;
12-
await writeJson('./npm-shrinkwrap.json', shrinkwrap);
10+
const shrinkwrap = (await readFile('./npm-shrinkwrap.json')).toString();
11+
await writeFile('./npm-shrinkwrap.json', replaceVersion(shrinkwrap, version));
1312
logger.log('Wrote version %s to npm-shrinkwrap.json', version);
1413
}
1514
};
15+
16+
function replaceVersion(json, version) {
17+
return json.replace(/("version"\s*:\s*")\S+?(\s*")/, `$1${version}$2`);
18+
}

test/update-package-version.test.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import {writeJson, readJson} from 'fs-extra';
2+
import {writeFile, readFile} from 'fs-extra';
33
import tempy from 'tempy';
44
import execa from 'execa';
55
import {stub} from 'sinon';
@@ -21,28 +21,49 @@ test.afterEach.always(() => {
2121
});
2222

2323
test.serial('Updade package.json', async t => {
24+
const pkg = `{
25+
"name": "test",
26+
"description": "pacakage description",
27+
"version":
28+
"0.0.0-dev" ,
29+
"arr": [
30+
1,
31+
2, 3
32+
]
33+
}
34+
`;
35+
2436
// Create package.json in repository root
25-
await writeJson('./package.json', {version: '0.0.0-dev'});
37+
await writeFile('./package.json', pkg);
2638

2739
await updatePackageVersion('1.0.0', t.context.logger);
2840

2941
// Verify package.json has been updated
30-
t.is((await readJson('./package.json')).version, '1.0.0');
42+
t.is((await readFile('./package.json')).toString(), pkg.replace('0.0.0-dev', '1.0.0'));
43+
3144
// Verify the logger has been called with the version updated
3245
t.deepEqual(t.context.log.args[0], ['Wrote version %s to package.json', '1.0.0']);
3346
});
3447

3548
test.serial('Updade package.json and npm-shrinkwrap.json', async t => {
49+
const pkg = `{
50+
"name": "test"
51+
, "description":
52+
"pacakage description",
53+
54+
"version": "0.0.0-dev"
55+
}`;
3656
// Create package.json in repository root
37-
await writeJson('./package.json', {version: '0.0.0-dev'});
57+
await writeFile('./package.json', pkg);
3858
// Create a npm-shrinkwrap.json file
3959
await execa('npm', ['shrinkwrap']);
60+
const shrinkwrap = (await readFile('./npm-shrinkwrap.json')).toString();
4061

4162
await updatePackageVersion('1.0.0', t.context.logger);
4263

4364
// Verify package.json and npm-shrinkwrap.json have been updated
44-
t.is((await readJson('./package.json')).version, '1.0.0');
45-
t.is((await readJson('./npm-shrinkwrap.json')).version, '1.0.0');
65+
t.is((await readFile('./package.json')).toString(), pkg.replace('0.0.0-dev', '1.0.0'));
66+
t.is((await readFile('./npm-shrinkwrap.json')).toString(), shrinkwrap.replace('0.0.0-dev', '1.0.0'));
4667
// Verify the logger has been called with the version updated
4768
t.deepEqual(t.context.log.args[0], ['Wrote version %s to package.json', '1.0.0']);
4869
t.deepEqual(t.context.log.args[1], ['Wrote version %s to npm-shrinkwrap.json', '1.0.0']);

0 commit comments

Comments
 (0)