Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGar <gar+gh@danger.computer>2022-03-29 20:12:42 +0300
committerLuke Karrys <luke@lukekarrys.com>2022-03-30 03:23:41 +0300
commitd8d374d23d34c17e22b52afc1cfb5247cc7c3e1d (patch)
tree2bd4541cf4bcd87b7e6716892496bc737ff5c0e5 /lib
parent52dfaf239a3f66a05ee9d6148bc41a46b5adffd6 (diff)
fix: consolidate split-package-names
It was only ever used by `npm edit` so it's inline now. Rewrote `npm edit` tests to be real
Diffstat (limited to 'lib')
-rw-r--r--lib/commands/edit.js27
-rw-r--r--lib/utils/split-package-names.js25
2 files changed, 24 insertions, 28 deletions
diff --git a/lib/commands/edit.js b/lib/commands/edit.js
index ce74ff79b..0256f4f3a 100644
--- a/lib/commands/edit.js
+++ b/lib/commands/edit.js
@@ -3,11 +3,32 @@
const { resolve } = require('path')
const fs = require('graceful-fs')
-const { spawn } = require('child_process')
-const splitPackageNames = require('../utils/split-package-names.js')
+const cp = require('child_process')
const completion = require('../utils/completion/installed-shallow.js')
const BaseCommand = require('../base-command.js')
+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')
+}
+
class Edit extends BaseCommand {
static description = 'Edit an installed package'
static name = 'edit'
@@ -36,7 +57,7 @@ class Edit extends BaseCommand {
return reject(err)
}
const [bin, ...args] = this.npm.config.get('editor').split(/\s+/)
- const editor = spawn(bin, [...args, dir], { stdio: 'inherit' })
+ const editor = cp.spawn(bin, [...args, dir], { stdio: 'inherit' })
editor.on('exit', (code) => {
if (code) {
return reject(new Error(`editor process exited with code: ${code}`))
diff --git a/lib/utils/split-package-names.js b/lib/utils/split-package-names.js
deleted file mode 100644
index 395c2517d..000000000
--- a/lib/utils/split-package-names.js
+++ /dev/null
@@ -1,25 +0,0 @@
-'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