Skip to content

Commit 39ad1ad

Browse files
ruyadornoisaacs
authored andcommitted
fix: npm config
- Small refactors such as line breaks and favor usage of flatOptions - Removes validBefore? console.error msg - Fix typos PR-URL: #2001 Credit: @ruyadorno Close: #2001 Reviewed-by: @isaacs
1 parent cc42d01 commit 39ad1ad

File tree

2 files changed

+42
-52
lines changed

2 files changed

+42
-52
lines changed

lib/config.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const { defaults, types } = require('./utils/config.js')
33
const usageUtil = require('./utils/usage.js')
44
const output = require('./utils/output.js')
55

6-
const editor = require('editor')
76
const mkdirp = require('mkdirp-infer-owner')
87
const { dirname } = require('path')
98
const { promisify } = require('util')
109
const fs = require('fs')
1110
const readFile = promisify(fs.readFile)
1211
const writeFile = promisify(fs.writeFile)
12+
const editor = promisify(require('editor'))
1313
const { EOL } = require('os')
1414
const ini = require('ini')
1515

@@ -28,18 +28,25 @@ const cmd = (args, cb) => config(args).then(() => cb()).catch(cb)
2828

2929
const completion = (opts, cb) => {
3030
const argv = opts.conf.argv.remain
31-
if (argv[1] !== 'config') argv.unshift('config')
31+
if (argv[1] !== 'config') {
32+
argv.unshift('config')
33+
}
34+
3235
if (argv.length === 2) {
3336
const cmds = ['get', 'set', 'delete', 'ls', 'rm', 'edit']
34-
if (opts.partialWord !== 'l') cmds.push('list')
37+
if (opts.partialWord !== 'l') {
38+
cmds.push('list')
39+
}
3540
return cb(null, cmds)
3641
}
3742

3843
const action = argv[2]
3944
switch (action) {
4045
case 'set':
4146
// todo: complete with valid values, if possible.
42-
if (argv.length > 3) return cb(null, [])
47+
if (argv.length > 3) {
48+
return cb(null, [])
49+
}
4350
// fallthrough
4451
/* eslint no-fallthrough:0 */
4552
case 'get':
@@ -49,12 +56,14 @@ const completion = (opts, cb) => {
4956
case 'edit':
5057
case 'list':
5158
case 'ls':
52-
return cb(null, [])
5359
default:
5460
return cb(null, [])
5561
}
5662
}
5763

64+
const UsageError = () =>
65+
Object.assign(new Error(usage), { code: 'EUSAGE' })
66+
5867
const config = async ([action, key, val]) => {
5968
npm.log.disableProgress()
6069
try {
@@ -72,13 +81,13 @@ const config = async ([action, key, val]) => {
7281
break
7382
case 'list':
7483
case 'ls':
75-
await (npm.config.get('json') ? listJson() : list())
84+
await (npm.flatOptions.json ? listJson() : list())
7685
break
7786
case 'edit':
7887
await edit()
7988
break
8089
default:
81-
throw usage
90+
throw UsageError()
8291
}
8392
} finally {
8493
npm.log.enableProgress()
@@ -87,7 +96,7 @@ const config = async ([action, key, val]) => {
8796

8897
const set = async (key, val) => {
8998
if (key === undefined) {
90-
throw usage
99+
throw UsageError()
91100
}
92101

93102
if (val === undefined) {
@@ -103,9 +112,7 @@ const set = async (key, val) => {
103112
key = key.trim()
104113
val = val.trim()
105114
npm.log.info('config', 'set %j %j', key, val)
106-
const where = npm.config.get('global') ? 'global' : 'user'
107-
const validBefore = npm.config.data.get(where).valid
108-
console.error('validBefore?', validBefore)
115+
const where = npm.flatOptions.global ? 'global' : 'user'
109116
npm.config.set(key, val, where)
110117
if (!npm.config.validate(where)) {
111118
npm.log.warn('config', 'omitting invalid config values')
@@ -127,18 +134,18 @@ const get = async key => {
127134

128135
const del = async key => {
129136
if (!key) {
130-
throw usage
137+
throw UsageError()
131138
}
132139

133-
const where = npm.config.get('global') ? 'global' : 'user'
140+
const where = npm.flatOptions.global ? 'global' : 'user'
134141
npm.config.delete(key, where)
135142
await npm.config.save(where)
136143
}
137144

138145
const edit = async () => {
139146
const { editor: e, global } = npm.flatOptions
140147
if (!e) {
141-
throw new Error('No `editor` config or EDITOR envionment variable set')
148+
throw new Error('No `editor` config or EDITOR environment variable set')
142149
}
143150

144151
const where = global ? 'global' : 'user'
@@ -147,10 +154,14 @@ const edit = async () => {
147154
// save first, just to make sure it's synced up
148155
// this also removes all the comments from the last time we edited it.
149156
await npm.config.save(where)
150-
const data = (await readFile(file, 'utf8').catch(() => '')).replace(/\r\n/g, '\n')
157+
158+
const data = (
159+
await readFile(file, 'utf8').catch(() => '')
160+
).replace(/\r\n/g, '\n')
151161
const defData = Object.entries(defaults).reduce((str, [key, val]) => {
152162
const obj = { [key]: val }
153163
const i = ini.stringify(obj)
164+
.replace(/\r\n/g, '\n') // normalizes output from ini.stringify
154165
.replace(/\n$/m, '')
155166
.replace(/^/g, '; ')
156167
.replace(/\n/g, '\n; ')
@@ -179,9 +190,7 @@ ${defData}
179190
`.split('\n').join(EOL)
180191
await mkdirp(dirname(file))
181192
await writeFile(file, tmpData, 'utf8')
182-
await new Promise((res, rej) => {
183-
editor(file, { editor: e }, (er) => er ? rej(er) : res())
184-
})
193+
await editor(file, { editor: e })
185194
}
186195

187196
const publicVar = k => !/^(\/\/[^:]+:)?_/.test(k)

test/lib/config.js

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ const redactCwd = (path) => {
55
const normalizePath = p => p
66
.replace(/\\+/g, '/')
77
.replace(/\r\n/g, '\n')
8-
return normalizePath(path)
8+
const replaceCwd = p => p
99
.replace(new RegExp(normalizePath(process.cwd()), 'g'), '{CWD}')
10-
.replace(process.execPath, '/path/to/node')
11-
.replace(process.env.HOME, '~/')
10+
const cleanupWinPaths = p => p
11+
.replace(normalizePath(process.execPath), '/path/to/node')
12+
.replace(normalizePath(process.env.HOME), '~/')
13+
14+
return cleanupWinPaths(
15+
replaceCwd(
16+
normalizePath(path)
17+
)
18+
)
1219
}
1320

1421
t.cleanSnapshot = (str) => redactCwd(str)
@@ -167,10 +174,11 @@ t.test('config list --json', t => {
167174
t.test('config delete no args', t => {
168175
config(['delete'], (err) => {
169176
t.equal(
170-
err,
177+
err.message,
171178
'usage instructions',
172179
'should throw usage error'
173180
)
181+
t.equal(err.code, 'EUSAGE', 'should throw expected error code')
174182
t.end()
175183
})
176184
})
@@ -224,7 +232,7 @@ t.test('config delete key --global', t => {
224232
t.test('config set no args', t => {
225233
config(['set'], (err) => {
226234
t.equal(
227-
err,
235+
err.message,
228236
'usage instructions',
229237
'should throw usage error'
230238
)
@@ -366,12 +374,11 @@ t.test('config get no args', t => {
366374
})
367375

368376
t.test('config get key', t => {
369-
t.plan(3)
377+
t.plan(2)
370378

371379
const npmConfigGet = npm.config.get
372-
npm.config.get = (key, where) => {
380+
npm.config.get = (key) => {
373381
t.equal(key, 'foo', 'should use expected key')
374-
t.equal(where, 'user', 'should retrieve key from user config by default')
375382
return 'bar'
376383
}
377384

@@ -389,32 +396,6 @@ t.test('config get key', t => {
389396
})
390397
})
391398

392-
t.test('config get key --global', t => {
393-
t.plan(3)
394-
395-
flatOptions.global = true
396-
const npmConfigGet = npm.config.get
397-
npm.config.get = (key, where) => {
398-
t.equal(key, 'foo', 'should use expected global key')
399-
t.equal(where, 'global', 'should retrieve key from global config')
400-
return 'bar'
401-
}
402-
403-
npm.config.save = where => {
404-
throw new Error('should not save')
405-
}
406-
407-
config(['get', 'foo'], (err) => {
408-
t.ifError(err, 'npm config get key --global')
409-
})
410-
411-
t.teardown(() => {
412-
flatOptions.global = false
413-
npm.config.get = npmConfigGet
414-
delete npm.config.save
415-
})
416-
})
417-
418399
t.test('config get private key', t => {
419400
config(['get', '//private-reg.npmjs.org/:_authThoken'], (err) => {
420401
t.match(

0 commit comments

Comments
 (0)