All files edit.js

100% Statements 22/22
100% Branches 6/6
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 37      1x 1x 1x 1x 1x 1x   1x 1x     4x 1x   3x 3x   3x 3x 1x   2x 2x 2x 2x 1x   1x         1x  
// 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 })