Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit tests #2344

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { promisify } = require('util')
const fs = require('fs')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const editor = promisify(require('editor'))
const { spawn } = require('child_process')
const { EOL } = require('os')
const ini = require('ini')

Expand Down Expand Up @@ -138,9 +138,6 @@ const del = async key => {

const edit = async () => {
const { editor: e, global } = npm.flatOptions
if (!e)
throw new Error('No `editor` config or EDITOR environment variable set')

const where = global ? 'global' : 'user'
const file = npm.config.data.get(where).source

Expand Down Expand Up @@ -183,7 +180,15 @@ ${defData}
`.split('\n').join(EOL)
await mkdirp(dirname(file))
await writeFile(file, tmpData, 'utf8')
await editor(file, { editor: e })
await new Promise((resolve, reject) => {
const [bin, ...args] = e.split(/\s+/)
const editor = spawn(bin, [...args, file], { stdio: 'inherit' })
editor.on('exit', (code) => {
if (code)
return reject(new Error(`editor process exited with code: ${code}`))
return resolve()
})
})
}

const publicVar = k => !/^(\/\/[^:]+:)?_/.test(k)
Expand Down
72 changes: 28 additions & 44 deletions lib/edit.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,36 @@
// npm edit <pkg>
// open the package folder in the $EDITOR

module.exports = edit
edit.usage = 'npm edit <pkg>[/<subpkg>...]'
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')

edit.completion = require('./utils/completion/installed-shallow.js')

var npm = require('./npm.js')
var path = require('path')
var fs = require('graceful-fs')
var editor = require('editor')
var noProgressTillDone = require('./utils/no-progress-while-running').tillDone
const usage = usageUtil('edit', 'npm edit <pkg>[/<subpkg>...]')
const completion = require('./utils/completion/installed-shallow.js')

function edit (args, cb) {
var p = args[0]
if (args.length !== 1 || !p)
return cb(edit.usage)
var e = npm.config.get('editor')
if (!e) {
return cb(new Error(
"No editor set. Set the 'editor' config, or $EDITOR environ."
))
}
p = p.split('/')
// combine scoped parts
.reduce(function (parts, part) {
if (parts.length === 0)
return [part]

var lastPart = parts[parts.length - 1]
// check if previous part is the first part of a scoped package
if (lastPart[0] === '@' && !lastPart.includes('/'))
parts[parts.length - 1] += '/' + part
else
parts.push(part)

return parts
}, [])
.join('/node_modules/')
.replace(/(\/node_modules)+/, '/node_modules')
var f = path.resolve(npm.dir, p)
fs.lstat(f, function (er) {
if (er)
return cb(er)
editor(f, { editor: e }, noProgressTillDone(function (er) {
if (er)
return cb(er)
npm.commands.rebuild(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 })
23 changes: 23 additions & 0 deletions lib/utils/split-package-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const splitPackageNames = (path) => {
return path.split('/')
// combine scoped parts
.reduce((parts, part) => {
if (parts.length === 0)
return [part]

const lastPart = parts[parts.length - 1]
// check if previous part is the first part of a scoped package
if (lastPart[0] === '@' && !lastPart.includes('/'))
parts[parts.length - 1] += '/' + part
else
parts.push(part)

return parts
}, [])
.join('/node_modules/')
.replace(/(\/node_modules)+/, '/node_modules')
}

module.exports = splitPackageNames
21 changes: 0 additions & 21 deletions node_modules/editor/LICENSE

This file was deleted.

54 changes: 0 additions & 54 deletions node_modules/editor/README.markdown

This file was deleted.

5 changes: 0 additions & 5 deletions node_modules/editor/example/beep.json

This file was deleted.

4 changes: 0 additions & 4 deletions node_modules/editor/example/edit.js

This file was deleted.

20 changes: 0 additions & 20 deletions node_modules/editor/index.js

This file was deleted.

34 changes: 0 additions & 34 deletions node_modules/editor/package.json

This file was deleted.

13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"cli-columns": "^3.1.2",
"cli-table3": "^0.6.0",
"columnify": "~1.5.4",
"editor": "~1.0.0",
"glob": "^7.1.4",
"graceful-fs": "^4.2.3",
"hosted-git-info": "^3.0.6",
Expand Down
Loading