Skip to content

Commit ac5832f

Browse files
committed
feat: uphold the prev package.json indents
1 parent a2105ce commit ac5832f

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

lib/createInlinePluginCreator.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { once } = require("lodash");
33
const { identity } = require("lodash");
44
const EventEmitter = require("promise-events");
55
const getCommitsFiltered = require("./getCommitsFiltered");
6-
const getManifest = require("./getManifest");
6+
const { getManifest, getIndent } = require("./getManifest");
77
const hasChangedDeep = require("./hasChangedDeep");
88

99
/**
@@ -57,6 +57,7 @@ function createInlinePluginCreator(packages, multiContext) {
5757
const updateManifestDeps = (pkg, path) => {
5858
// Get and parse manifest file contents.
5959
const manifest = getManifest(path);
60+
const indent = getIndent(path);
6061

6162
// Loop through localDeps to update dependencies/devDependencies/peerDependencies in manifest.
6263
pkg._localDeps.forEach((d) => {
@@ -74,7 +75,7 @@ function createInlinePluginCreator(packages, multiContext) {
7475
});
7576

7677
// Write package.json back out.
77-
writeFileSync(path, JSON.stringify(manifest));
78+
writeFileSync(path, JSON.stringify(manifest, null, indent));
7879
};
7980

8081
/**

lib/getManifest.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const { existsSync, lstatSync, readFileSync } = require("fs");
22

33
/**
4-
* Get the parsed contents of a package.json manifest file.
4+
* Read the content of target package.json if exists.
55
*
6-
* @param {string} path The path to the package.json manifest file.
7-
* @returns {object} The manifest file's contents.
6+
* @param {string} path file path
7+
* @returns {string} file content
88
*
99
* @internal
1010
*/
11-
function getManifest(path) {
11+
function readManifest(path) {
1212
// Check it exists.
1313
if (!existsSync(path)) throw new ReferenceError(`package.json file not found: "${path}"`);
1414

@@ -25,13 +25,41 @@ function getManifest(path) {
2525
if (!stat.isFile()) throw new ReferenceError(`package.json is not a file: "${path}"`);
2626

2727
// Read the file.
28-
let contents;
2928
try {
30-
contents = readFileSync(path, "utf8");
29+
return readFileSync(path, "utf8");
3130
} catch (_) {
3231
// istanbul ignore next (hard to test — happens if no read access etc).
3332
throw new ReferenceError(`package.json cannot be read: "${path}"`);
3433
}
34+
}
35+
36+
/**
37+
* Extract the current indent sequence from file.
38+
*
39+
* @param {string} path The path to the package.json manifest file.
40+
* @returns {string} indent symbols
41+
*
42+
* @internal
43+
*/
44+
function getIndent(path) {
45+
// Read the file.
46+
const contents = readManifest(path);
47+
const match = /\n([^"]+)/.exec(contents);
48+
49+
return match ? match[1] : 2;
50+
}
51+
52+
/**
53+
* Get the parsed contents of a package.json manifest file.
54+
*
55+
* @param {string} path The path to the package.json manifest file.
56+
* @returns {object} The manifest file's contents.
57+
*
58+
* @internal
59+
*/
60+
function getManifest(path) {
61+
// Read the file.
62+
const contents = readManifest(path);
3563

3664
// Parse the file.
3765
let manifest;
@@ -65,4 +93,4 @@ function getManifest(path) {
6593
}
6694

6795
// Exports.
68-
module.exports = getManifest;
96+
module.exports = { getManifest, getIndent };

lib/getWorkspacesYarn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const glob = require("bash-glob");
2-
const getManifest = require("./getManifest");
2+
const { getManifest } = require("./getManifest");
33
const { checker } = require("./blork");
44

55
/**

lib/multiSemanticRelease.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { check } = require("./blork");
44
const getLogger = require("./getLogger");
55
const getConfig = require("./getConfig");
66
const getConfigSemantic = require("./getConfigSemantic");
7-
const getManifest = require("./getManifest");
7+
const { getManifest } = require("./getManifest");
88
const cleanPath = require("./cleanPath");
99
const RescopedStream = require("./RescopedStream");
1010
const createInlinePluginCreator = require("./createInlinePluginCreator");

0 commit comments

Comments
 (0)