Skip to content

Commit bd74f54

Browse files
pvdlggr2m
authored andcommitted
feat: preserve indetentation and newline type of package.json
1 parent 5ed7481 commit bd74f54

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

lib/update-package-version.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
const path = require('path');
2-
const {readJson, writeJson, pathExists} = require('fs-extra');
2+
const parseJson = require('parse-json');
3+
const detectIndent = require('detect-indent');
4+
const detectNewline = require('detect-newline');
5+
const {readFile, writeJson, pathExists} = require('fs-extra');
6+
7+
const DEFAULT_INDENT = 2;
8+
const DEFAULT_NEWLINE = '\n';
39

410
module.exports = async (version, basePath, logger) => {
511
const packagePath = path.join(basePath, 'package.json');
612
const shrinkwrapPath = path.join(basePath, 'npm-shrinkwrap.json');
713
const packageLockPath = path.join(basePath, 'package-lock.json');
8-
const pkg = await readJson(packagePath);
14+
const pkg = await readFile(packagePath, 'utf8');
915

10-
await writeJson(packagePath, {...pkg, ...{version}}, {spaces: 2});
16+
await writeJson(packagePath, {...parseJson(pkg), ...{version}}, getWriteOptions(pkg));
1117
logger.log('Wrote version %s to %s', version, packagePath);
1218

1319
if (await pathExists(shrinkwrapPath)) {
14-
const shrinkwrap = await readJson(shrinkwrapPath);
15-
await writeJson(shrinkwrapPath, {...shrinkwrap, ...{version}}, {spaces: 2});
20+
const shrinkwrap = await readFile(shrinkwrapPath, 'utf8');
21+
await writeJson(shrinkwrapPath, {...parseJson(shrinkwrap), ...{version}}, getWriteOptions(shrinkwrap));
1622
logger.log('Wrote version %s to %s', version, shrinkwrapPath);
1723
}
1824

1925
if (await pathExists(packageLockPath)) {
20-
const packageLock = await readJson(packageLockPath);
21-
await writeJson(packageLockPath, {...packageLock, ...{version}}, {spaces: 2});
26+
const packageLock = await readFile(packageLockPath, 'utf8');
27+
await writeJson(packageLockPath, {...parseJson(packageLock), ...{version}}, getWriteOptions(packageLock));
2228
logger.log('Wrote version %s to %s', version, packageLockPath);
2329
}
2430
};
31+
32+
function getWriteOptions(content) {
33+
return {spaces: detectIndent(content).indent || DEFAULT_INDENT, EOL: detectNewline(content) || DEFAULT_NEWLINE};
34+
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
"dependencies": {
1919
"@semantic-release/error": "^2.2.0",
2020
"aggregate-error": "^1.0.0",
21+
"detect-indent": "^5.0.0",
22+
"detect-newline": "^2.1.0",
2123
"execa": "^0.10.0",
2224
"fs-extra": "^6.0.0",
2325
"lodash": "^4.17.4",
2426
"nerf-dart": "^1.0.0",
2527
"normalize-url": "^2.0.1",
28+
"parse-json": "^4.0.0",
2629
"read-pkg": "^3.0.0",
2730
"registry-auth-token": "^3.3.1"
2831
},

test/update-package-version.test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import {outputJson, readJson} from 'fs-extra';
2+
import {outputJson, readJson, outputFile, readFile} from 'fs-extra';
33
import tempy from 'tempy';
44
import execa from 'execa';
55
import {stub} from 'sinon';
@@ -82,3 +82,32 @@ test.serial('Updade package.json and npm-shrinkwrap.json in a sub-directory', as
8282
t.deepEqual(t.context.log.args[0], ['Wrote version %s to %s', '1.0.0', 'dist/package.json']);
8383
t.deepEqual(t.context.log.args[1], ['Wrote version %s to %s', '1.0.0', 'dist/npm-shrinkwrap.json']);
8484
});
85+
86+
test.serial('Preserve indentation and newline', async t => {
87+
// Create package.json in repository root
88+
await outputFile('./package.json', `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`);
89+
90+
await updatePackageVersion('1.0.0', '.', t.context.logger);
91+
92+
// Verify package.json has been updated
93+
t.is(
94+
await readFile('./package.json', 'utf-8'),
95+
`{\r\n "name": "package-name",\r\n "version": "1.0.0"\r\n}\r\n`
96+
);
97+
98+
// Verify the logger has been called with the version updated
99+
t.deepEqual(t.context.log.args[0], ['Wrote version %s to %s', '1.0.0', 'package.json']);
100+
});
101+
102+
test.serial('Use default indentation and newline if it cannot be detected', async t => {
103+
// Create package.json in repository root
104+
await outputFile('./package.json', `{"name": "package-name","version": "0.0.0-dev"}`);
105+
106+
await updatePackageVersion('1.0.0', '.', t.context.logger);
107+
108+
// Verify package.json has been updated
109+
t.is(await readFile('./package.json', 'utf-8'), `{\n "name": "package-name",\n "version": "1.0.0"\n}\n`);
110+
111+
// Verify the logger has been called with the version updated
112+
t.deepEqual(t.context.log.args[0], ['Wrote version %s to %s', '1.0.0', 'package.json']);
113+
});

0 commit comments

Comments
 (0)