-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
edit.js
36 lines (28 loc) · 1.02 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// npm edit <pkg>
// open the package folder in the $EDITOR
const { resolve } = require('path')
const fs = require('graceful-fs')
const { spawn } = require('child_process')
const npm = require('./npm.js')
const usageUtil = require('./utils/usage.js')
const splitPackageNames = require('./utils/split-package-names.js')
const usage = usageUtil('edit', 'npm edit <pkg>[/<subpkg>...]')
const completion = require('./utils/completion/installed-shallow.js')
function edit (args, cb) {
if (args.length !== 1)
return cb(usage)
const path = splitPackageNames(args[0])
const dir = resolve(npm.dir, path)
fs.lstat(dir, (err) => {
if (err)
return cb(err)
const [bin, ...args] = npm.config.get('editor').split(/\s+/)
const editor = spawn(bin, [...args, dir], { stdio: 'inherit' })
editor.on('exit', (code) => {
if (code)
return cb(new Error(`editor process exited with code: ${code}`))
npm.commands.rebuild([dir], cb)
})
})
}
module.exports = Object.assign(edit, { completion, usage })