1 |
|
2 |
|
3 |
|
4 | module.exports = edit
|
5 | edit.usage = 'npm edit <pkg>[/<subpkg>...]'
|
6 |
|
7 | edit.completion = require('./utils/completion/installed-shallow.js')
|
8 |
|
9 | var npm = require('./npm.js')
|
10 | var path = require('path')
|
11 | var fs = require('graceful-fs')
|
12 | var editor = require('editor')
|
13 | var noProgressTillDone = require('./utils/no-progress-while-running').tillDone
|
14 |
|
15 | function edit (args, cb) {
|
16 | var p = args[0]
|
17 | if (args.length !== 1 || !p) return cb(edit.usage)
|
18 | var e = npm.config.get('editor')
|
19 | if (!e) {
|
20 | return cb(new Error(
|
21 | "No editor set. Set the 'editor' config, or $EDITOR environ."
|
22 | ))
|
23 | }
|
24 | p = p.split('/')
|
25 |
|
26 | .reduce(function (parts, part) {
|
27 | if (parts.length === 0) {
|
28 | return [part]
|
29 | }
|
30 | var lastPart = parts[parts.length - 1]
|
31 |
|
32 | if (lastPart[0] === '@' && !lastPart.includes('/')) {
|
33 | parts[parts.length - 1] += '/' + part
|
34 | } else {
|
35 | parts.push(part)
|
36 | }
|
37 | return parts
|
38 | }, [])
|
39 | .join('/node_modules/')
|
40 | .replace(/(\/node_modules)+/, '/node_modules')
|
41 | var f = path.resolve(npm.dir, p)
|
42 | fs.lstat(f, function (er) {
|
43 | if (er) return cb(er)
|
44 | editor(f, { editor: e }, noProgressTillDone(function (er) {
|
45 | if (er) return cb(er)
|
46 | npm.commands.rebuild(args, cb)
|
47 | }))
|
48 | })
|
49 | }
|