From 96216da5f7a9a8cb02b67d9478a1f20e513f9859 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Thu, 14 Feb 2019 23:31:08 +0000 Subject: [PATCH] feat: preserve formatting when writing to package.json (#282) --- index.js | 11 +++++----- lib/lifecycles/bump.js | 10 +++++++-- package.json | 3 +++ test.js | 48 +++++++++++++++++++++++++++++++++--------- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index d8432b03c..8408defdc 100755 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ -const latestSemverTag = require('./lib/latest-semver-tag') -const path = require('path') -const printError = require('./lib/print-error') - const bump = require('./lib/lifecycles/bump') const changelog = require('./lib/lifecycles/changelog') const commit = require('./lib/lifecycles/commit') +const fs = require('fs') +const latestSemverTag = require('./lib/latest-semver-tag') +const path = require('path') +const printError = require('./lib/print-error') const tag = require('./lib/lifecycles/tag') module.exports = function standardVersion (argv) { @@ -13,7 +13,8 @@ module.exports = function standardVersion (argv) { if (pkg) return var pkgPath = path.resolve(process.cwd(), filename) try { - pkg = require(pkgPath) + var data = fs.readFileSync(pkgPath, 'utf8') + pkg = JSON.parse(data) } catch (err) {} }) let newVersion diff --git a/lib/lifecycles/bump.js b/lib/lifecycles/bump.js index 7fa0857b8..0401b7e9b 100644 --- a/lib/lifecycles/bump.js +++ b/lib/lifecycles/bump.js @@ -3,12 +3,15 @@ const chalk = require('chalk') const checkpoint = require('../checkpoint') const conventionalRecommendedBump = require('conventional-recommended-bump') +const detectIndent = require('detect-indent') +const detectNewline = require('detect-newline') const figures = require('figures') const fs = require('fs') const DotGitignore = require('dotgitignore') const path = require('path') const runLifecycleScript = require('../run-lifecycle-script') const semver = require('semver') +const stringifyPackage = require('stringify-package') const writeFile = require('../write-file') var configsToUpdate = {} @@ -160,11 +163,14 @@ function updateConfigs (args, newVersion) { if (dotgit.ignore(configPath)) return var stat = fs.lstatSync(configPath) if (stat.isFile()) { - var config = require(configPath) + var data = fs.readFileSync(configPath, 'utf8') + var indent = detectIndent(data).indent + var newline = detectNewline(data) + var config = JSON.parse(data) var filename = path.basename(configPath) checkpoint(args, 'bumping version in ' + filename + ' from %s to %s', [config.version, newVersion]) config.version = newVersion - writeFile(args, configPath, JSON.stringify(config, null, 2) + '\n') + writeFile(args, configPath, stringifyPackage(config, indent, newline)) // flag any config files that we modify the version # for // as having been updated. configsToUpdate[configPath] = true diff --git a/package.json b/package.json index 4e840f134..e9b06b58c 100644 --- a/package.json +++ b/package.json @@ -41,11 +41,14 @@ "chalk": "^2.4.1", "conventional-changelog": "^3.0.5", "conventional-recommended-bump": "^4.0.4", + "detect-indent": "^5.0.0", + "detect-newline": "^2.1.0", "dotgitignore": "^1.0.3", "figures": "^2.0.0", "fs-access": "^1.0.0", "git-semver-tags": "^2.0.2", "semver": "^5.2.0", + "stringify-package": "^1.0.0", "yargs": "^12.0.2" }, "devDependencies": { diff --git a/test.js b/test.js index 158e181b1..88126d430 100644 --- a/test.js +++ b/test.js @@ -95,16 +95,6 @@ function initInTempFolder () { shell.cd('tmp') shell.exec('git init') commit('root-commit') - ;['package.json', - 'manifest.json', - 'bower.json' - ].forEach(metadata => { - try { - delete require.cache[require.resolve(path.join(process.cwd(), metadata))] - } catch (err) { - // we haven't loaded the metadata file yet. - } - }) writePackageJson('1.0.0') } @@ -615,6 +605,44 @@ describe('cli', function () { pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\n')) }) + it('preserves indentation of tabs in package.json', function () { + var indentation = '\t' + var newPkgJson = ['{', indentation + '"version": "1.0.0"', '}', ''].join('\n') + fs.writeFileSync('package.json', newPkgJson, 'utf-8') + + execCli().code.should.equal(0) + var pkgJson = fs.readFileSync('package.json', 'utf-8') + pkgJson.should.equal(['{', indentation + '"version": "1.0.1"', '}', ''].join('\n')) + }) + + it('preserves indentation of spaces in package.json', function () { + var indentation = ' ' + var newPkgJson = ['{', indentation + '"version": "1.0.0"', '}', ''].join('\n') + fs.writeFileSync('package.json', newPkgJson, 'utf-8') + + execCli().code.should.equal(0) + var pkgJson = fs.readFileSync('package.json', 'utf-8') + pkgJson.should.equal(['{', indentation + '"version": "1.0.1"', '}', ''].join('\n')) + }) + + it('preserves line feed in package.json', function () { + var newPkgJson = ['{', ' "version": "1.0.0"', '}', ''].join('\n') + fs.writeFileSync('package.json', newPkgJson, 'utf-8') + + execCli().code.should.equal(0) + var pkgJson = fs.readFileSync('package.json', 'utf-8') + pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\n')) + }) + + it('preserves carriage return + line feed in package.json', function () { + var newPkgJson = ['{', ' "version": "1.0.0"', '}', ''].join('\r\n') + fs.writeFileSync('package.json', newPkgJson, 'utf-8') + + execCli().code.should.equal(0) + var pkgJson = fs.readFileSync('package.json', 'utf-8') + pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\r\n')) + }) + it('does not run git hooks if the --no-verify flag is passed', function () { writeGitPreCommitHook()