From 3452c484fb1545b641fa55b5c8fcd3255f2dd91e Mon Sep 17 00:00:00 2001 From: Nikita Lebedev Date: Sat, 6 Apr 2019 04:06:33 +0500 Subject: [PATCH 1/4] do not pretty-print package-lock.json to prevent corrupting git lines count history --- doc/misc/npm-config.md | 14 ++++++++++++++ lib/config/defaults.js | 4 ++++ lib/shrinkwrap.js | 18 ++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/misc/npm-config.md b/doc/misc/npm-config.md index 88d30b62ca252..4bf60a4fbb14a 100644 --- a/doc/misc/npm-config.md +++ b/doc/misc/npm-config.md @@ -387,6 +387,20 @@ Makes various commands more forceful. * skips cache when requesting from the registry. * prevents checks against clobbering non-npm files. +### format-package-lock + +* Default: false +* Type: Boolean + +Format `package-lock.json` as a human readable file. + +### format-shrinkwrap + +* Default: false +* Type: Boolean + +Format `npm-shrinkwrap.json` as a human readable file. + ### fetch-retries * Default: 2 diff --git a/lib/config/defaults.js b/lib/config/defaults.js index f563357d4cba3..02b11d5dc2465 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -141,6 +141,8 @@ Object.defineProperty(exports, 'defaults', {get: function () { editor: osenv.editor(), 'engine-strict': false, force: false, + 'format-package-lock': false, + 'format-shrinkwrap': false, 'fetch-retries': 2, 'fetch-retry-factor': 10, @@ -282,6 +284,8 @@ exports.types = { editor: String, 'engine-strict': Boolean, force: Boolean, + 'format-package-lock': Boolean, + 'format-shrinkwrap': Boolean, 'fetch-retries': Number, 'fetch-retry-factor': Number, 'fetch-retry-mintimeout': Number, diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index 75d58bf8e4f0b..a2ca7a6d23cf6 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -270,11 +270,25 @@ function checkPackageFile (dir, name) { return readFile( file, 'utf8' ).then((data) => { + let indent + let newline + if (name === PKGLOCK && npm.config.get('format-package-lock') === false) { + indent = 0 + newline = 0 + } + else if (name === SHRINKWRAP && npm.config.get('format-shrinkwrap') === false) { + indent = 0 + newline = 0 + } + else { + indent = detectIndent(data).indent + newline = detectNewline(data) + } return { path: file, raw: data, - indent: detectIndent(data).indent, - newline: detectNewline(data) + indent, + newline } }).catch({code: 'ENOENT'}, () => {}) } From 5ad00ebda82365136860e945df4b8d8cc3871890 Mon Sep 17 00:00:00 2001 From: Nick Reiley Date: Tue, 10 Sep 2019 01:48:58 +0500 Subject: [PATCH 2/4] make format-package-lock default --- doc/misc/npm-config.md | 4 ++-- lib/config/defaults.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/misc/npm-config.md b/doc/misc/npm-config.md index 4bf60a4fbb14a..6fb086d4a479f 100644 --- a/doc/misc/npm-config.md +++ b/doc/misc/npm-config.md @@ -389,14 +389,14 @@ Makes various commands more forceful. ### format-package-lock -* Default: false +* Default: true * Type: Boolean Format `package-lock.json` as a human readable file. ### format-shrinkwrap -* Default: false +* Default: true * Type: Boolean Format `npm-shrinkwrap.json` as a human readable file. diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 02b11d5dc2465..486fcdd1535af 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -141,8 +141,8 @@ Object.defineProperty(exports, 'defaults', {get: function () { editor: osenv.editor(), 'engine-strict': false, force: false, - 'format-package-lock': false, - 'format-shrinkwrap': false, + 'format-package-lock': true, + 'format-shrinkwrap': true, 'fetch-retries': 2, 'fetch-retry-factor': 10, From 9f31a4bddb6f51b71a7c33826e1b92ce08498491 Mon Sep 17 00:00:00 2001 From: Nick Reiley Date: Tue, 10 Sep 2019 20:44:53 +0500 Subject: [PATCH 3/4] remove format-shrinkwrap property and name condition --- doc/misc/npm-config.md | 9 +-------- lib/config/defaults.js | 2 -- lib/shrinkwrap.js | 9 ++------- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/doc/misc/npm-config.md b/doc/misc/npm-config.md index 6fb086d4a479f..f1055a56edbc7 100644 --- a/doc/misc/npm-config.md +++ b/doc/misc/npm-config.md @@ -392,14 +392,7 @@ Makes various commands more forceful. * Default: true * Type: Boolean -Format `package-lock.json` as a human readable file. - -### format-shrinkwrap - -* Default: true -* Type: Boolean - -Format `npm-shrinkwrap.json` as a human readable file. +Format `package-lock.json` or `npm-shrinkwrap.json` as a human readable file. ### fetch-retries diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 486fcdd1535af..7f43372d11fa6 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -142,7 +142,6 @@ Object.defineProperty(exports, 'defaults', {get: function () { 'engine-strict': false, force: false, 'format-package-lock': true, - 'format-shrinkwrap': true, 'fetch-retries': 2, 'fetch-retry-factor': 10, @@ -285,7 +284,6 @@ exports.types = { 'engine-strict': Boolean, force: Boolean, 'format-package-lock': Boolean, - 'format-shrinkwrap': Boolean, 'fetch-retries': Number, 'fetch-retry-factor': Number, 'fetch-retry-mintimeout': Number, diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index eb626a3d7c4aa..a223e015ea5f0 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -284,15 +284,10 @@ function checkPackageFile (dir, name) { ).then((data) => { let indent let newline - if (name === PKGLOCK && npm.config.get('format-package-lock') === false) { + if (npm.config.get('format-package-lock') === false) { indent = 0 newline = 0 - } - else if (name === SHRINKWRAP && npm.config.get('format-shrinkwrap') === false) { - indent = 0 - newline = 0 - } - else { + } else { indent = detectIndent(data).indent newline = detectNewline(data) } From 89262af5c273dc179e0c0a37fbc79e6853ebdbda Mon Sep 17 00:00:00 2001 From: Nick Reiley Date: Thu, 12 Sep 2019 10:30:40 +0500 Subject: [PATCH 4/4] test: format-package-lock --- test/tap/format-package-lock.js | 116 ++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 test/tap/format-package-lock.js diff --git a/test/tap/format-package-lock.js b/test/tap/format-package-lock.js new file mode 100644 index 0000000000000..ddf40915d9bd3 --- /dev/null +++ b/test/tap/format-package-lock.js @@ -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() +})