Skip to content

Commit f6b0459

Browse files
bl00mberisaacs
authored andcommitted
Add option to save package-lock without formatting
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: @bl00mberClose: #248 Reviewed-by: @isaacs Note: squashed and edited slightly for style. -- @isaacs
1 parent 5c380e5 commit f6b0459

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

‎doc/misc/npm-config.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,13 @@ Makes various commands more forceful.
387387
* skips cache when requesting from the registry.
388388
* prevents checks against clobbering non-npm files.
389389

390+
### format-package-lock
391+
392+
* Default: true
393+
* Type: Boolean
394+
395+
Format `package-lock.json` or `npm-shrinkwrap.json` as a human readable file.
396+
390397
### fetch-retries
391398

392399
* Default: 2

‎lib/config/defaults.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Object.defineProperty(exports, 'defaults',{get: function (){
141141
editor: osenv.editor(),
142142
'engine-strict': false,
143143
force: false,
144+
'format-package-lock': true,
144145

145146
'fetch-retries': 2,
146147
'fetch-retry-factor': 10,
@@ -283,6 +284,7 @@ exports.types ={
283284
editor: String,
284285
'engine-strict': Boolean,
285286
force: Boolean,
287+
'format-package-lock': Boolean,
286288
'fetch-retries': Number,
287289
'fetch-retry-factor': Number,
288290
'fetch-retry-mintimeout': Number,

‎lib/shrinkwrap.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,15 @@ function checkPackageFile (dir, name){
282282
returnreadFile(
283283
file,'utf8'
284284
).then((data)=>{
285+
constformat=npm.config.get('format-package-lock')!==false
286+
constindent=format ? detectIndent(data).indent : 0
287+
constnewline=format ? detectNewline(data) : 0
288+
285289
return{
286290
path: file,
287291
raw: data,
288-
indent: detectIndent(data).indent,
289-
newline: detectNewline(data)
292+
indent,
293+
newline
290294
}
291295
}).catch({code: 'ENOENT'},()=>{})
292296
}

‎test/tap/format-package-lock.js‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict'
2+
constfs=require('fs')
3+
constpath=require('path')
4+
consttest=require('tap').test
5+
constmr=require('npm-registry-mock')
6+
constTacks=require('tacks')
7+
constFile=Tacks.File
8+
constDir=Tacks.Dir
9+
constcommon=require('../common-tap.js')
10+
11+
constbasedir=common.pkg
12+
consttestdir=path.join(basedir,'testdir')
13+
constcachedir=common.cache
14+
constglobaldir=path.join(basedir,'global')
15+
consttmpdir=path.join(basedir,'tmp')
16+
17+
constpkgPath=path.join(testdir,'package.json')
18+
constpkgLockPath=path.join(testdir,'package-lock.json')
19+
constshrinkwrapPath=path.join(testdir,'npm-shrinkwrap.json')
20+
constCRLFreg=/\r\n|\r|\n/
21+
22+
constenv=common.newEnv().extend({
23+
npm_config_cache: cachedir,
24+
npm_config_tmp: tmpdir,
25+
npm_config_prefix: globaldir,
26+
npm_config_registry: common.registry,
27+
npm_config_loglevel: 'warn',
28+
npm_config_format_package_lock: false
29+
})
30+
31+
varserver
32+
varfixture=newTacks(Dir({
33+
cache: Dir(),
34+
global: Dir(),
35+
tmp: Dir(),
36+
testdir: Dir({
37+
'package.json': File({
38+
name: 'install-package-lock-only',
39+
version: '1.0.0',
40+
dependencies: {
41+
mkdirp: '^0.3.4'
42+
}
43+
})
44+
})
45+
}))
46+
47+
functionsetup(){
48+
cleanup()
49+
fixture.create(basedir)
50+
}
51+
52+
functioncleanup(){
53+
fixture.remove(basedir)
54+
}
55+
56+
test('setup',function(t){
57+
mr({port: common.port,throwOnUnmatched: true},function(err,s){
58+
if(err)throwerr
59+
server=s
60+
t.done()
61+
})
62+
})
63+
64+
test('package-lock.json unformatted, package.json formatted when config has `format-package-lock: false`',function(t){
65+
setup()
66+
common.npm(['install'],{cwd: testdir, env}).spread((code,stdout,stderr)=>{
67+
t.is(code,0,'ok')
68+
t.ok(fs.existsSync(pkgLockPath),'ensure that package-lock.json was created')
69+
constpkgLockUtf8=fs.readFileSync(pkgLockPath,'utf-8')
70+
t.equal(pkgLockUtf8.split(CRLFreg).length,2,'package-lock.json is unformatted')
71+
constpkgUtf8=fs.readFileSync(pkgPath,'utf-8')
72+
t.notEqual(pkgUtf8.split(CRLFreg).length,2,'package.json is formatted')
73+
t.done()
74+
})
75+
})
76+
77+
test('npm-shrinkwrap.json unformatted when config has `format-package-lock: false`',function(t){
78+
setup()
79+
common.npm(['shrinkwrap'],{cwd: testdir, env}).spread((code,stdout,stderr)=>{
80+
t.is(code,0,'ok')
81+
t.ok(fs.existsSync(shrinkwrapPath),'ensure that npm-shrinkwrap.json was created')
82+
constshrinkwrapUtf8=fs.readFileSync(shrinkwrapPath,'utf-8')
83+
t.equal(shrinkwrapUtf8.split(CRLFreg).length,2,'npm-shrinkwrap.json is unformatted')
84+
t.done()
85+
})
86+
})
87+
88+
test('package-lock.json and package.json formatted when config has `format-package-lock: true`',function(t){
89+
setup()
90+
common.npm(['install'],{cwd: testdir}).spread((code,stdout,stderr)=>{
91+
t.is(code,0,'ok')
92+
t.ok(fs.existsSync(pkgLockPath),'ensure that package-lock.json was created')
93+
constpkgLockUtf8=fs.readFileSync(pkgLockPath,'utf-8')
94+
t.notEqual(pkgLockUtf8.split(CRLFreg).length,2,'package-lock.json is formatted')
95+
constpkgUtf8=fs.readFileSync(pkgPath,'utf-8')
96+
t.notEqual(pkgUtf8.split(CRLFreg).length,2,'package.json is formatted')
97+
t.done()
98+
})
99+
})
100+
101+
test('npm-shrinkwrap.json formatted when config has `format-package-lock: true`',function(t){
102+
setup()
103+
common.npm(['shrinkwrap'],{cwd: testdir}).spread((code,stdout,stderr)=>{
104+
t.is(code,0,'ok')
105+
t.ok(fs.existsSync(shrinkwrapPath),'ensure that npm-shrinkwrap.json was created')
106+
constshrinkwrapUtf8=fs.readFileSync(shrinkwrapPath,'utf-8')
107+
t.notEqual(shrinkwrapUtf8.split(CRLFreg).length,2,'npm-shrinkwrap.json is unformatted')
108+
t.done()
109+
})
110+
})
111+
112+
test('cleanup',function(t){
113+
server.close()
114+
cleanup()
115+
t.done()
116+
})

0 commit comments

Comments
(0)