Skip to content

Commit

Permalink
feat: preserve formatting when writing to package.json (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
alasdairhurst authored and bcoe committed Feb 14, 2019
1 parent 43e7cdc commit 96216da
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/lifecycles/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
48 changes: 38 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 96216da

Please sign in to comment.