Skip to content

Commit 5332793

Browse files
committed
fix: npm config
- Small refactors such as line breaks and favor usage of flatOptions - Removes validBefore? console.error msg - Fix typos
1 parent 433ffca commit 5332793

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

lib/config.js

Lines changed: 20 additions & 15 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,7 +56,6 @@ const completion = (opts, cb) => {
4956
case 'edit':
5057
case 'list':
5158
case 'ls':
52-
return cb(null, [])
5359
default:
5460
return cb(null, [])
5561
}
@@ -72,7 +78,7 @@ const config = async ([action, key, val]) => {
7278
break
7379
case 'list':
7480
case 'ls':
75-
await (npm.config.get('json') ? listJson() : list())
81+
await (npm.flatOptions.json ? listJson() : list())
7682
break
7783
case 'edit':
7884
await edit()
@@ -103,9 +109,7 @@ const set = async (key, val) => {
103109
key = key.trim()
104110
val = val.trim()
105111
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)
112+
const where = npm.flatOptions.global ? 'global' : 'user'
109113
npm.config.set(key, val, where)
110114
if (!npm.config.validate(where)) {
111115
npm.log.warn('config', 'omitting invalid config values')
@@ -130,15 +134,15 @@ const del = async key => {
130134
throw usage
131135
}
132136

133-
const where = npm.config.get('global') ? 'global' : 'user'
137+
const where = npm.flatOptions.global ? 'global' : 'user'
134138
npm.config.delete(key, where)
135139
await npm.config.save(where)
136140
}
137141

138142
const edit = async () => {
139143
const { editor: e, global } = npm.flatOptions
140144
if (!e) {
141-
throw new Error('No `editor` config or EDITOR envionment variable set')
145+
throw new Error('No `editor` config or EDITOR environment variable set')
142146
}
143147

144148
const where = global ? 'global' : 'user'
@@ -147,7 +151,10 @@ const edit = async () => {
147151
// save first, just to make sure it's synced up
148152
// this also removes all the comments from the last time we edited it.
149153
await npm.config.save(where)
150-
const data = (await readFile(file, 'utf8').catch(() => '')).replace(/\r\n/g, '\n')
154+
155+
const data = (
156+
await readFile(file, 'utf8').catch(() => '')
157+
).replace(/\r\n/g, '\n')
151158
const defData = Object.entries(defaults).reduce((str, [key, val]) => {
152159
const obj = { [key]: val }
153160
const i = ini.stringify(obj)
@@ -179,9 +186,7 @@ ${defData}
179186
`.split('\n').join(EOL)
180187
await mkdirp(dirname(file))
181188
await writeFile(file, tmpData, 'utf8')
182-
await new Promise((res, rej) => {
183-
editor(file, { editor: e }, (er) => er ? rej(er) : res())
184-
})
189+
await editor(file, { editor: e })
185190
}
186191

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

test/lib/config.js

Lines changed: 12 additions & 32 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)
@@ -366,12 +373,11 @@ t.test('config get no args', t => {
366373
})
367374

368375
t.test('config get key', t => {
369-
t.plan(3)
376+
t.plan(2)
370377

371378
const npmConfigGet = npm.config.get
372-
npm.config.get = (key, where) => {
379+
npm.config.get = (key) => {
373380
t.equal(key, 'foo', 'should use expected key')
374-
t.equal(where, 'user', 'should retrieve key from user config by default')
375381
return 'bar'
376382
}
377383

@@ -389,32 +395,6 @@ t.test('config get key', t => {
389395
})
390396
})
391397

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-
418398
t.test('config get private key', t => {
419399
config(['get', '//private-reg.npmjs.org/:_authThoken'], (err) => {
420400
t.match(

0 commit comments

Comments
 (0)