Skip to content

Commit

Permalink
Add option to save package-lock without formatting
Browse files Browse the repository at this point in the history
Adds a new config `--format-package-lock`, which defaults to true.

If set to false, then the package lock will be written without any
indentation or line breaks.

PR-URL: #248
Credit: @bl00mber
Close: #248
Reviewed-by: @isaacs

Note: squashed and edited slightly for style. -- @isaacs
  • Loading branch information
bl00mber authored and isaacs committed Sep 30, 2019
1 parent 5c380e5 commit f6b0459
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/misc/npm-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ Makes various commands more forceful.
* skips cache when requesting from the registry.
* prevents checks against clobbering non-npm files.

### format-package-lock

* Default: true
* Type: Boolean

Format `package-lock.json` or `npm-shrinkwrap.json` as a human readable file.

### fetch-retries

* Default: 2
Expand Down
2 changes: 2 additions & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Object.defineProperty(exports, 'defaults', {get: function () {
editor: osenv.editor(),
'engine-strict': false,
force: false,
'format-package-lock': true,

'fetch-retries': 2,
'fetch-retry-factor': 10,
Expand Down Expand Up @@ -283,6 +284,7 @@ exports.types = {
editor: String,
'engine-strict': Boolean,
force: Boolean,
'format-package-lock': Boolean,
'fetch-retries': Number,
'fetch-retry-factor': Number,
'fetch-retry-mintimeout': Number,
Expand Down
8 changes: 6 additions & 2 deletions lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,15 @@ function checkPackageFile (dir, name) {
return readFile(
file, 'utf8'
).then((data) => {
const format = npm.config.get('format-package-lock') !== false
const indent = format ? detectIndent(data).indent : 0
const newline = format ? detectNewline(data) : 0

return {
path: file,
raw: data,
indent: detectIndent(data).indent,
newline: detectNewline(data)
indent,
newline
}
}).catch({code: 'ENOENT'}, () => {})
}
116 changes: 116 additions & 0 deletions test/tap/format-package-lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict'
const fs = require('fs')
const path = require('path')
const test = require('tap').test
const mr = require('npm-registry-mock')
const Tacks = require('tacks')
const File = Tacks.File
const Dir = Tacks.Dir
const common = require('../common-tap.js')

const basedir = common.pkg
const testdir = path.join(basedir, 'testdir')
const cachedir = common.cache
const globaldir = path.join(basedir, 'global')
const tmpdir = path.join(basedir, 'tmp')

const pkgPath = path.join(testdir, 'package.json')
const pkgLockPath = path.join(testdir, 'package-lock.json')
const shrinkwrapPath = path.join(testdir, 'npm-shrinkwrap.json')
const CRLFreg = /\r\n|\r|\n/

const env = common.newEnv().extend({
npm_config_cache: cachedir,
npm_config_tmp: tmpdir,
npm_config_prefix: globaldir,
npm_config_registry: common.registry,
npm_config_loglevel: 'warn',
npm_config_format_package_lock: false
})

var server
var fixture = new Tacks(Dir({
cache: Dir(),
global: Dir(),
tmp: Dir(),
testdir: Dir({
'package.json': File({
name: 'install-package-lock-only',
version: '1.0.0',
dependencies: {
mkdirp: '^0.3.4'
}
})
})
}))

function setup () {
cleanup()
fixture.create(basedir)
}

function cleanup () {
fixture.remove(basedir)
}

test('setup', function (t) {
mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
if (err) throw err
server = s
t.done()
})
})

test('package-lock.json unformatted, package.json formatted when config has `format-package-lock: false`', function (t) {
setup()
common.npm(['install'], {cwd: testdir, env}).spread((code, stdout, stderr) => {
t.is(code, 0, 'ok')
t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
const pkgLockUtf8 = fs.readFileSync(pkgLockPath, 'utf-8')
t.equal(pkgLockUtf8.split(CRLFreg).length, 2, 'package-lock.json is unformatted')
const pkgUtf8 = fs.readFileSync(pkgPath, 'utf-8')
t.notEqual(pkgUtf8.split(CRLFreg).length, 2, 'package.json is formatted')
t.done()
})
})

test('npm-shrinkwrap.json unformatted when config has `format-package-lock: false`', function (t) {
setup()
common.npm(['shrinkwrap'], {cwd: testdir, env}).spread((code, stdout, stderr) => {
t.is(code, 0, 'ok')
t.ok(fs.existsSync(shrinkwrapPath), 'ensure that npm-shrinkwrap.json was created')
const shrinkwrapUtf8 = fs.readFileSync(shrinkwrapPath, 'utf-8')
t.equal(shrinkwrapUtf8.split(CRLFreg).length, 2, 'npm-shrinkwrap.json is unformatted')
t.done()
})
})

test('package-lock.json and package.json formatted when config has `format-package-lock: true`', function (t) {
setup()
common.npm(['install'], {cwd: testdir}).spread((code, stdout, stderr) => {
t.is(code, 0, 'ok')
t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
const pkgLockUtf8 = fs.readFileSync(pkgLockPath, 'utf-8')
t.notEqual(pkgLockUtf8.split(CRLFreg).length, 2, 'package-lock.json is formatted')
const pkgUtf8 = fs.readFileSync(pkgPath, 'utf-8')
t.notEqual(pkgUtf8.split(CRLFreg).length, 2, 'package.json is formatted')
t.done()
})
})

test('npm-shrinkwrap.json formatted when config has `format-package-lock: true`', function (t) {
setup()
common.npm(['shrinkwrap'], {cwd: testdir}).spread((code, stdout, stderr) => {
t.is(code, 0, 'ok')
t.ok(fs.existsSync(shrinkwrapPath), 'ensure that npm-shrinkwrap.json was created')
const shrinkwrapUtf8 = fs.readFileSync(shrinkwrapPath, 'utf-8')
t.notEqual(shrinkwrapUtf8.split(CRLFreg).length, 2, 'npm-shrinkwrap.json is unformatted')
t.done()
})
})

test('cleanup', function (t) {
server.close()
cleanup()
t.done()
})

0 comments on commit f6b0459

Please # to comment.