2024-05-30 04:21:05 -07:00
|
|
|
const { resolve } = require('node:path')
|
|
|
|
const { lstat } = require('node:fs/promises')
|
|
|
|
const cp = require('node:child_process')
|
2024-04-30 23:53:22 -07:00
|
|
|
const completion = require('../utils/installed-shallow.js')
|
|
|
|
const BaseCommand = require('../base-cmd.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
const splitPackageNames = (path) => path.split('/')
|
|
|
|
// combine scoped parts
|
|
|
|
.reduce((parts, part) => {
|
|
|
|
if (parts.length === 0) {
|
|
|
|
return [part]
|
|
|
|
}
|
2022-03-31 22:43:17 +00:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
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)
|
|
|
|
}
|
2022-03-31 22:43:17 +00:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
return parts
|
|
|
|
}, [])
|
|
|
|
.join('/node_modules/')
|
|
|
|
.replace(/(\/node_modules)+/, '/node_modules')
|
2022-03-31 22:43:17 +00:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
// npm edit <pkg>
|
|
|
|
// open the package folder in the $EDITOR
|
2021-03-11 17:54:23 -05:00
|
|
|
class Edit extends BaseCommand {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Edit an installed package'
|
|
|
|
static name = 'edit'
|
|
|
|
static usage = ['<pkg>[/<subpkg>...]']
|
|
|
|
static params = ['editor']
|
2022-03-03 21:38:08 +00:00
|
|
|
static ignoreImplicitWorkspace = false
|
2021-05-20 15:54:50 -04:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
// TODO
|
|
|
|
/* istanbul ignore next */
|
2023-06-08 05:24:49 -07:00
|
|
|
static async completion (opts, npm) {
|
|
|
|
return completion(npm, opts)
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec (args) {
|
2021-11-18 20:58:02 +00:00
|
|
|
if (args.length !== 1) {
|
2021-11-04 20:42:47 +00:00
|
|
|
throw this.usageError()
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
|
|
|
|
const path = splitPackageNames(args[0])
|
|
|
|
const dir = resolve(this.npm.dir, path)
|
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
await lstat(dir)
|
2023-01-16 22:38:23 -05:00
|
|
|
await new Promise((res, rej) => {
|
2024-04-30 23:53:22 -07:00
|
|
|
const [bin, ...spawnArgs] = this.npm.config.get('editor').split(/\s+/)
|
|
|
|
const editor = cp.spawn(bin, [...spawnArgs, dir], { stdio: 'inherit' })
|
|
|
|
editor.on('exit', async (code) => {
|
|
|
|
if (code) {
|
|
|
|
return rej(new Error(`editor process exited with code: ${code}`))
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
await this.npm.exec('rebuild', [dir]).then(res).catch(rej)
|
2021-03-04 17:40:28 -05:00
|
|
|
})
|
2020-12-16 14:12:26 -05:00
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Edit
|