|
1 | | -const { mkdir, readFile, writeFile } = require('fs/promises') |
2 | | -const { dirname, resolve } = require('path') |
3 | | -const { spawn } = require('child_process') |
4 | | -const { EOL } = require('os') |
5 | | -const ini = require('ini') |
| 1 | +const { resolve } = require('path') |
6 | 2 | const localeCompare = require('@isaacs/string-locale-compare')('en') |
7 | 3 | const rpj = require('read-package-json-fast') |
8 | 4 | const log = require('../utils/log-shim.js') |
9 | | -const { keys: configKeys, NerfDarts, definitions, defaults } = require('@npmcli/config') |
| 5 | +const { NerfDarts, definitionKeys, definitions } = require('@npmcli/config') |
10 | 6 |
|
11 | 7 | // take an array of `[key, value, k2=v2, k3, v3, ...]` and turn into |
12 | 8 | // { key: value, k2: v2, k3: v3 } |
@@ -88,7 +84,7 @@ class Config extends BaseCommand { |
88 | 84 | case 'get': |
89 | 85 | case 'delete': |
90 | 86 | case 'rm': |
91 | | - return configKeys |
| 87 | + return definitionKeys |
92 | 88 | case 'edit': |
93 | 89 | case 'list': |
94 | 90 | case 'ls': |
@@ -189,60 +185,9 @@ class Config extends BaseCommand { |
189 | 185 | } |
190 | 186 |
|
191 | 187 | async edit () { |
192 | | - const e = this.npm.flatOptions.editor |
| 188 | + const { editor } = this.npm.flatOptions |
193 | 189 | const where = this.npm.flatOptions.location |
194 | | - const { file } = this.npm.config.data.get(where) |
195 | | - |
196 | | - // save first, just to make sure it's synced up |
197 | | - // this also removes all the comments from the last time we edited it. |
198 | | - await this.npm.config.save(where) |
199 | | - |
200 | | - const data = ( |
201 | | - await readFile(file, 'utf8').catch(() => '') |
202 | | - ).replace(/\r\n/g, '\n') |
203 | | - const entries = Object.entries(defaults) |
204 | | - const defData = entries.reduce((str, [key, val]) => { |
205 | | - const obj = { [key]: val } |
206 | | - const i = ini.stringify(obj) |
207 | | - .replace(/\r\n/g, '\n') // normalizes output from ini.stringify |
208 | | - .replace(/\n$/m, '') |
209 | | - .replace(/^/g, '; ') |
210 | | - .replace(/\n/g, '\n; ') |
211 | | - .split('\n') |
212 | | - return str + '\n' + i |
213 | | - }, '') |
214 | | - |
215 | | - const tmpData = `;;;; |
216 | | -; npm ${where}config file: ${file} |
217 | | -; this is a simple ini-formatted file |
218 | | -; lines that start with semi-colons are comments |
219 | | -; run \`npm help 7 config\` for documentation of the various options |
220 | | -; |
221 | | -; Configs like \`@scope:registry\` map a scope to a given registry url. |
222 | | -; |
223 | | -; Configs like \`//<hostname>/:_authToken\` are auth that is restricted |
224 | | -; to the registry host specified. |
225 | | -
|
226 | | -${data.split('\n').sort(localeCompare).join('\n').trim()} |
227 | | -
|
228 | | -;;;; |
229 | | -; all available options shown below with default values |
230 | | -;;;; |
231 | | -
|
232 | | -${defData} |
233 | | -`.split('\n').join(EOL) |
234 | | - await mkdir(dirname(file), { recursive: true }) |
235 | | - await writeFile(file, tmpData, 'utf8') |
236 | | - await new Promise((res, rej) => { |
237 | | - const [bin, ...args] = e.split(/\s+/) |
238 | | - const editor = spawn(bin, [...args, file], { stdio: 'inherit' }) |
239 | | - editor.on('exit', (code) => { |
240 | | - if (code) { |
241 | | - return rej(new Error(`editor process exited with code: ${code}`)) |
242 | | - } |
243 | | - return res() |
244 | | - }) |
245 | | - }) |
| 190 | + await this.npm.config.edit(where, { editor }) |
246 | 191 | } |
247 | 192 |
|
248 | 193 | async fix () { |
|
0 commit comments