Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
fix: package-lock.json custom indentation
Browse files Browse the repository at this point in the history
package-lock.json files should follow the same indentation used on
package.json files.

This commit fixes the issue by forwarding the proper indent selected for
package.json to Shrinkwarp.save method. Also, added tests asserting
appropriate tab-indentation for both files.

Fixes: npm/cli#1662

PR-URL: #141
Credit: @ruyadorno
Close: #141
Reviewed-by: @isaacs
  • Loading branch information
ruyadorno authored and isaacs committed Sep 29, 2020
1 parent d38e967 commit c9f5323
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,10 @@ module.exports = cls => class Reifier extends cls {
const json = (JSON.stringify(pjData, null, format) + '\n')
.replace(/\n/g, eol)

const saveOpt = { format: this[_formatPackageLock] }
const saveOpt = {
format: (this[_formatPackageLock] && format) ? format
: this[_formatPackageLock],
}
return Promise.all([
this[_usePackageLock] && this.idealTree.meta.save(saveOpt),
writeFile(pj, json),
Expand Down
4 changes: 3 additions & 1 deletion lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,9 @@ class Shrinkwrap {
throw new Error('run load() before saving data')

const { format = true } = options
const indent = format ? this.indent || 2 : 0
const defaultIndent = this.indent || 2
const indent = format === true ? defaultIndent
: format || 0
const eol = format ? this.newline || '\n' : ''
const data = this.commit()
const json = stringify(data, swKeyOrder, indent).replace(/\n/g, eol)
Expand Down
42 changes: 42 additions & 0 deletions tap-snapshots/test-arborist-reify.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27084,6 +27084,48 @@ Object {
}
`

exports[`test/arborist/reify.js TAP store files with a custom indenting > must match snapshot 1`] = `
{
"name": "tab-indented-package-json",
"version": "1.0.0",
"dependencies": {
"abbrev": "^1.0.0"
}
}

`

exports[`test/arborist/reify.js TAP store files with a custom indenting > must match snapshot 2`] = `
{
"name": "tab-indented-package-json",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "tab-indented-package-json",
"version": "1.0.0",
"dependencies": {
"abbrev": "^1.0.0"
}
},
"node_modules/abbrev": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
"integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q=="
}
},
"dependencies": {
"abbrev": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
"integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q=="
}
}
}

`

exports[`test/arborist/reify.js TAP tarball deps with transitive tarball deps > expect resolving Promise 1`] = `
Node {
"children": Map {
Expand Down
10 changes: 10 additions & 0 deletions tap-snapshots/test-shrinkwrap.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14276,6 +14276,16 @@ Object {
}
`

exports[`test/shrinkwrap.js TAP saving dependency-free shrinkwrap object load file, and save it with a custom format > custom indented json output 1`] = `
{
"lockfileVersion": 2,
"requires": true,
"packages": {},
"dependencies": {}
}
`

exports[`test/shrinkwrap.js TAP saving dependency-free shrinkwrap object load the unindented file, and save it back default > indented json output 1`] = `
{
"lockfileVersion": 2,
Expand Down
14 changes: 14 additions & 0 deletions test/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,20 @@ t.test('add a dep present in the tree, with v1 shrinkwrap', async t => {
t.matchSnapshot(fs.readFileSync(path + '/package.json', 'utf8'))
})

t.test('store files with a custom indenting', async t => {
const tabIndentedPackageJson =
fs.readFileSync(
resolve(__dirname, '../fixtures/tab-indented-package-json/package.json'),
'utf8'
)
const path = t.testdir({
'package.json': tabIndentedPackageJson
})
const tree = await reify(path)
t.matchSnapshot(fs.readFileSync(path + '/package.json', 'utf8'))
t.matchSnapshot(fs.readFileSync(path + '/package-lock.json', 'utf8'))
})

t.test('modules bundled by the root should be installed', async t => {
const path = fixture(t, 'root-bundler')
const tree = await reify(path)
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/tab-indented-package-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "tab-indented-package-json",
"version": "1.0.0",
"dependencies": {
"abbrev": "^1.0.0"
}
}
11 changes: 11 additions & 0 deletions test/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ t.test('saving dependency-free shrinkwrap object', t => {
t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'indented json output')
})

t.test('load file, and save it with a custom format', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
sw.filename,
resolve(`${dir}/package-lock.json`),
'correct filepath on shrinkwrap instance'
)
await sw.save({ format: '\t' })
t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'custom indented json output')
})

t.end()
})

Expand Down

0 comments on commit c9f5323

Please sign in to comment.