Skip to content

Commit 96216da

Browse files
alasdairhurstbcoe
authored andcommittedFeb 14, 2019
feat: preserve formatting when writing to package.json (#282)
1 parent 43e7cdc commit 96216da

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed
 

‎index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const latestSemverTag = require('./lib/latest-semver-tag')
2-
const path = require('path')
3-
const printError = require('./lib/print-error')
4-
51
const bump = require('./lib/lifecycles/bump')
62
const changelog = require('./lib/lifecycles/changelog')
73
const commit = require('./lib/lifecycles/commit')
4+
const fs = require('fs')
5+
const latestSemverTag = require('./lib/latest-semver-tag')
6+
const path = require('path')
7+
const printError = require('./lib/print-error')
88
const tag = require('./lib/lifecycles/tag')
99

1010
module.exports = function standardVersion (argv) {
@@ -13,7 +13,8 @@ module.exports = function standardVersion (argv) {
1313
if (pkg) return
1414
var pkgPath = path.resolve(process.cwd(), filename)
1515
try {
16-
pkg = require(pkgPath)
16+
var data = fs.readFileSync(pkgPath, 'utf8')
17+
pkg = JSON.parse(data)
1718
} catch (err) {}
1819
})
1920
let newVersion

‎lib/lifecycles/bump.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
const chalk = require('chalk')
44
const checkpoint = require('../checkpoint')
55
const conventionalRecommendedBump = require('conventional-recommended-bump')
6+
const detectIndent = require('detect-indent')
7+
const detectNewline = require('detect-newline')
68
const figures = require('figures')
79
const fs = require('fs')
810
const DotGitignore = require('dotgitignore')
911
const path = require('path')
1012
const runLifecycleScript = require('../run-lifecycle-script')
1113
const semver = require('semver')
14+
const stringifyPackage = require('stringify-package')
1215
const writeFile = require('../write-file')
1316

1417
var configsToUpdate = {}
@@ -160,11 +163,14 @@ function updateConfigs (args, newVersion) {
160163
if (dotgit.ignore(configPath)) return
161164
var stat = fs.lstatSync(configPath)
162165
if (stat.isFile()) {
163-
var config = require(configPath)
166+
var data = fs.readFileSync(configPath, 'utf8')
167+
var indent = detectIndent(data).indent
168+
var newline = detectNewline(data)
169+
var config = JSON.parse(data)
164170
var filename = path.basename(configPath)
165171
checkpoint(args, 'bumping version in ' + filename + ' from %s to %s', [config.version, newVersion])
166172
config.version = newVersion
167-
writeFile(args, configPath, JSON.stringify(config, null, 2) + '\n')
173+
writeFile(args, configPath, stringifyPackage(config, indent, newline))
168174
// flag any config files that we modify the version # for
169175
// as having been updated.
170176
configsToUpdate[configPath] = true

‎package.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@
4141
"chalk": "^2.4.1",
4242
"conventional-changelog": "^3.0.5",
4343
"conventional-recommended-bump": "^4.0.4",
44+
"detect-indent": "^5.0.0",
45+
"detect-newline": "^2.1.0",
4446
"dotgitignore": "^1.0.3",
4547
"figures": "^2.0.0",
4648
"fs-access": "^1.0.0",
4749
"git-semver-tags": "^2.0.2",
4850
"semver": "^5.2.0",
51+
"stringify-package": "^1.0.0",
4952
"yargs": "^12.0.2"
5053
},
5154
"devDependencies": {

‎test.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,6 @@ function initInTempFolder () {
9595
shell.cd('tmp')
9696
shell.exec('git init')
9797
commit('root-commit')
98-
;['package.json',
99-
'manifest.json',
100-
'bower.json'
101-
].forEach(metadata => {
102-
try {
103-
delete require.cache[require.resolve(path.join(process.cwd(), metadata))]
104-
} catch (err) {
105-
// we haven't loaded the metadata file yet.
106-
}
107-
})
10898
writePackageJson('1.0.0')
10999
}
110100

@@ -615,6 +605,44 @@ describe('cli', function () {
615605
pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\n'))
616606
})
617607

608+
it('preserves indentation of tabs in package.json', function () {
609+
var indentation = '\t'
610+
var newPkgJson = ['{', indentation + '"version": "1.0.0"', '}', ''].join('\n')
611+
fs.writeFileSync('package.json', newPkgJson, 'utf-8')
612+
613+
execCli().code.should.equal(0)
614+
var pkgJson = fs.readFileSync('package.json', 'utf-8')
615+
pkgJson.should.equal(['{', indentation + '"version": "1.0.1"', '}', ''].join('\n'))
616+
})
617+
618+
it('preserves indentation of spaces in package.json', function () {
619+
var indentation = ' '
620+
var newPkgJson = ['{', indentation + '"version": "1.0.0"', '}', ''].join('\n')
621+
fs.writeFileSync('package.json', newPkgJson, 'utf-8')
622+
623+
execCli().code.should.equal(0)
624+
var pkgJson = fs.readFileSync('package.json', 'utf-8')
625+
pkgJson.should.equal(['{', indentation + '"version": "1.0.1"', '}', ''].join('\n'))
626+
})
627+
628+
it('preserves line feed in package.json', function () {
629+
var newPkgJson = ['{', ' "version": "1.0.0"', '}', ''].join('\n')
630+
fs.writeFileSync('package.json', newPkgJson, 'utf-8')
631+
632+
execCli().code.should.equal(0)
633+
var pkgJson = fs.readFileSync('package.json', 'utf-8')
634+
pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\n'))
635+
})
636+
637+
it('preserves carriage return + line feed in package.json', function () {
638+
var newPkgJson = ['{', ' "version": "1.0.0"', '}', ''].join('\r\n')
639+
fs.writeFileSync('package.json', newPkgJson, 'utf-8')
640+
641+
execCli().code.should.equal(0)
642+
var pkgJson = fs.readFileSync('package.json', 'utf-8')
643+
pkgJson.should.equal(['{', ' "version": "1.0.1"', '}', ''].join('\r\n'))
644+
})
645+
618646
it('does not run git hooks if the --no-verify flag is passed', function () {
619647
writeGitPreCommitHook()
620648

0 commit comments

Comments
 (0)
Please sign in to comment.