diff --git a/lib/createInlinePluginCreator.js b/lib/createInlinePluginCreator.js index e7f1c7bc..3fb950fe 100644 --- a/lib/createInlinePluginCreator.js +++ b/lib/createInlinePluginCreator.js @@ -2,7 +2,7 @@ const { writeFileSync } = require("fs"); const { identity, once } = require("lodash"); const EventEmitter = require("promise-events"); const getCommitsFiltered = require("./getCommitsFiltered"); -const getManifest = require("./getManifest"); +const { getManifest, getIndent } = require("./getManifest"); const hasChangedDeep = require("./hasChangedDeep"); /** @@ -56,6 +56,7 @@ function createInlinePluginCreator(packages, multiContext) { const updateManifestDeps = (pkg, path) => { // Get and parse manifest file contents. const manifest = getManifest(path); + const indent = getIndent(path); // Loop through localDeps to update dependencies/devDependencies/peerDependencies in manifest. pkg._localDeps.forEach((d) => { @@ -73,7 +74,7 @@ function createInlinePluginCreator(packages, multiContext) { }); // Write package.json back out. - writeFileSync(path, JSON.stringify(manifest, null, 2)); + writeFileSync(path, JSON.stringify(manifest, null, indent)); }; /** diff --git a/lib/getManifest.js b/lib/getManifest.js index 4cbff907..220fcc0a 100644 --- a/lib/getManifest.js +++ b/lib/getManifest.js @@ -1,14 +1,14 @@ const { existsSync, lstatSync, readFileSync } = require("fs"); /** - * Get the parsed contents of a package.json manifest file. + * Read the content of target package.json if exists. * - * @param {string} path The path to the package.json manifest file. - * @returns {object} The manifest file's contents. + * @param {string} path file path + * @returns {string} file content * * @internal */ -function getManifest(path) { +function readManifest(path) { // Check it exists. if (!existsSync(path)) throw new ReferenceError(`package.json file not found: "${path}"`); @@ -25,13 +25,41 @@ function getManifest(path) { if (!stat.isFile()) throw new ReferenceError(`package.json is not a file: "${path}"`); // Read the file. - let contents; try { - contents = readFileSync(path, "utf8"); + return readFileSync(path, "utf8"); } catch (_) { // istanbul ignore next (hard to test — happens if no read access etc). throw new ReferenceError(`package.json cannot be read: "${path}"`); } +} + +/** + * Extract the current indent sequence from file. + * + * @param {string} path The path to the package.json manifest file. + * @returns {string} indent symbols + * + * @internal + */ +function getIndent(path) { + // Read the file. + const contents = readManifest(path); + const match = /\n([^"]+)/.exec(contents); + + return match ? match[1] : 2; +} + +/** + * Get the parsed contents of a package.json manifest file. + * + * @param {string} path The path to the package.json manifest file. + * @returns {object} The manifest file's contents. + * + * @internal + */ +function getManifest(path) { + // Read the file. + const contents = readManifest(path); // Parse the file. let manifest; @@ -65,4 +93,4 @@ function getManifest(path) { } // Exports. -module.exports = getManifest; +module.exports = { getManifest, getIndent }; diff --git a/lib/getWorkspacesYarn.js b/lib/getWorkspacesYarn.js index 19decffc..1579e378 100644 --- a/lib/getWorkspacesYarn.js +++ b/lib/getWorkspacesYarn.js @@ -1,5 +1,5 @@ const glob = require("bash-glob"); -const getManifest = require("./getManifest"); +const { getManifest } = require("./getManifest"); const { checker } = require("./blork"); /** diff --git a/lib/multiSemanticRelease.js b/lib/multiSemanticRelease.js index 8c5a1cf9..328596e1 100644 --- a/lib/multiSemanticRelease.js +++ b/lib/multiSemanticRelease.js @@ -4,7 +4,7 @@ const { check } = require("./blork"); const getLogger = require("./getLogger"); const getConfig = require("./getConfig"); const getConfigSemantic = require("./getConfigSemantic"); -const getManifest = require("./getManifest"); +const { getManifest } = require("./getManifest"); const cleanPath = require("./cleanPath"); const RescopedStream = require("./RescopedStream"); const createInlinePluginCreator = require("./createInlinePluginCreator");