|
1 | 1 | // npm edit <pkg> |
2 | 2 | // open the package folder in the $EDITOR |
3 | 3 |
|
4 | | -module.exports = edit |
5 | | -edit.usage = 'npm edit <pkg>[/<subpkg>...]' |
| 4 | +const { resolve } = require('path') |
| 5 | +const fs = require('graceful-fs') |
| 6 | +const { spawn } = require('child_process') |
| 7 | +const npm = require('./npm.js') |
| 8 | +const usageUtil = require('./utils/usage.js') |
| 9 | +const splitPackageNames = require('./utils/split-package-names.js') |
6 | 10 |
|
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 |
| 11 | +const usage = usageUtil('edit', 'npm edit <pkg>[/<subpkg>...]') |
| 12 | +const completion = require('./utils/completion/installed-shallow.js') |
14 | 13 |
|
15 | 14 | function edit (args, cb) { |
16 | | - var p = args[0] |
17 | | - if (args.length !== 1 || !p) |
18 | | - return cb(edit.usage) |
19 | | - var e = npm.config.get('editor') |
20 | | - if (!e) { |
21 | | - return cb(new Error( |
22 | | - "No editor set. Set the 'editor' config, or $EDITOR environ." |
23 | | - )) |
24 | | - } |
25 | | - p = p.split('/') |
26 | | - // combine scoped parts |
27 | | - .reduce(function (parts, part) { |
28 | | - if (parts.length === 0) |
29 | | - return [part] |
30 | | - |
31 | | - var lastPart = parts[parts.length - 1] |
32 | | - // check if previous part is the first part of a scoped package |
33 | | - if (lastPart[0] === '@' && !lastPart.includes('/')) |
34 | | - parts[parts.length - 1] += '/' + part |
35 | | - else |
36 | | - parts.push(part) |
37 | | - |
38 | | - return parts |
39 | | - }, []) |
40 | | - .join('/node_modules/') |
41 | | - .replace(/(\/node_modules)+/, '/node_modules') |
42 | | - var f = path.resolve(npm.dir, p) |
43 | | - fs.lstat(f, function (er) { |
44 | | - if (er) |
45 | | - return cb(er) |
46 | | - editor(f, { editor: e }, noProgressTillDone(function (er) { |
47 | | - if (er) |
48 | | - return cb(er) |
49 | | - npm.commands.rebuild(args, cb) |
50 | | - })) |
| 15 | + if (args.length !== 1) |
| 16 | + return cb(usage) |
| 17 | + |
| 18 | + const path = splitPackageNames(args[0]) |
| 19 | + const dir = resolve(npm.dir, path) |
| 20 | + |
| 21 | + fs.lstat(dir, (err) => { |
| 22 | + if (err) |
| 23 | + return cb(err) |
| 24 | + |
| 25 | + const [bin, ...args] = npm.config.get('editor').split(/\s+/) |
| 26 | + const editor = spawn(bin, [...args, dir], { stdio: 'inherit' }) |
| 27 | + editor.on('exit', (code) => { |
| 28 | + if (code) |
| 29 | + return cb(new Error(`editor process exited with code: ${code}`)) |
| 30 | + |
| 31 | + npm.commands.rebuild([dir], cb) |
| 32 | + }) |
51 | 33 | }) |
52 | 34 | } |
| 35 | + |
| 36 | +module.exports = Object.assign(edit, { completion, usage }) |
0 commit comments