1// npm edit <pkg> 2// open the package folder in the $EDITOR 3 4module.exports = edit 5edit.usage = 'npm edit <pkg>[/<subpkg>...]' 6 7edit.completion = require('./utils/completion/installed-shallow.js') 8 9var npm = require('./npm.js') 10var path = require('path') 11var fs = require('graceful-fs') 12var editor = require('editor') 13var noProgressTillDone = require('./utils/no-progress-while-running').tillDone 14 15function 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 // combine scoped parts 26 .reduce(function (parts, part) { 27 if (parts.length === 0) { 28 return [part] 29 } 30 var lastPart = parts[parts.length - 1] 31 // check if previous part is the first part of a scoped package 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} 50