Skip to content

Commit

Permalink
feat: uphold the prev package.json indents
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jul 7, 2020
1 parent a2105ce commit ac5832f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/createInlinePluginCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { once } = require("lodash");
const { identity } = require("lodash");
const EventEmitter = require("promise-events");
const getCommitsFiltered = require("./getCommitsFiltered");
const getManifest = require("./getManifest");
const { getManifest, getIndent } = require("./getManifest");
const hasChangedDeep = require("./hasChangedDeep");

/**
Expand Down Expand Up @@ -57,6 +57,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) => {
Expand All @@ -74,7 +75,7 @@ function createInlinePluginCreator(packages, multiContext) {
});

// Write package.json back out.
writeFileSync(path, JSON.stringify(manifest));
writeFileSync(path, JSON.stringify(manifest, null, indent));
};

/**
Expand Down
42 changes: 35 additions & 7 deletions lib/getManifest.js
Original file line number Diff line number Diff line change
@@ -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}"`);

Expand All @@ -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;
Expand Down Expand Up @@ -65,4 +93,4 @@ function getManifest(path) {
}

// Exports.
module.exports = getManifest;
module.exports = { getManifest, getIndent };
2 changes: 1 addition & 1 deletion lib/getWorkspacesYarn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const glob = require("bash-glob");
const getManifest = require("./getManifest");
const { getManifest } = require("./getManifest");
const { checker } = require("./blork");

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/multiSemanticRelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit ac5832f

Please sign in to comment.