Skip to content

Commit 54d889f

Browse files
committed
chore: uphold the prev package.json indents
closes #18 closes #19
1 parent 2ea4bbd commit 54d889f

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

lib/createInlinePluginCreator.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { writeFileSync } = require("fs");
22
const { identity, once } = require("lodash");
33
const EventEmitter = require("promise-events");
44
const getCommitsFiltered = require("./getCommitsFiltered");
5-
const getManifest = require("./getManifest");
5+
const { getManifest, getIndent } = require("./getManifest");
66
const hasChangedDeep = require("./hasChangedDeep");
77

88
/**
@@ -56,6 +56,7 @@ function createInlinePluginCreator(packages, multiContext) {
5656
const updateManifestDeps = (pkg, path) => {
5757
// Get and parse manifest file contents.
5858
const manifest = getManifest(path);
59+
const indent = getIndent(path);
5960

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

7576
// Write package.json back out.
76-
writeFileSync(path, JSON.stringify(manifest, null, 2));
77+
writeFileSync(path, JSON.stringify(manifest, null, indent));
7778
};
7879

7980
/**

lib/getManifest.js

+35-7
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

+1-1
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

+1-1
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)