Skip to content

Commit 6124aa8

Browse files
committed
fix(perf): lazy loading optimizations
1 parent 8eae4b3 commit 6124aa8

16 files changed

+73
-89
lines changed

lib/base-command.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// Base class for npm commands
2-
3-
const { relative } = require('path')
4-
5-
const { definitions } = require('@npmcli/config/lib/definitions')
6-
const { aliases: cmdAliases } = require('./utils/cmd-list')
71
const { log, output } = require('proc-log')
82

93
class BaseCommand {
@@ -18,6 +12,8 @@ class BaseCommand {
1812
// this is a static so that we can read from it without instantiating a command
1913
// which would require loading the config
2014
static get describeUsage () {
15+
const { definitions } = require('@npmcli/config/lib/definitions')
16+
const { aliases: cmdAliases } = require('./utils/cmd-list')
2117
const seenExclusive = new Set()
2218
const wrapWidth = 80
2319
const { description, usage = [''], name, params } = this
@@ -161,6 +157,8 @@ class BaseCommand {
161157
}
162158

163159
async setWorkspaces () {
160+
const { relative } = require('node:path')
161+
164162
const includeWorkspaceRoot = this.isArboristCmd
165163
? false
166164
: this.npm.config.get('include-workspace-root')

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const validateEngines = require('./es6/validate-engines.js')
2-
const cliEntry = require('path').resolve(__dirname, 'cli-entry.js')
2+
const cliEntry = require('node:path').resolve(__dirname, 'cli-entry.js')
33

44
module.exports = (process) => validateEngines(process, () => require(cliEntry))

lib/commands/config.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const { mkdir, readFile, writeFile } = require('fs/promises')
2-
const { dirname, resolve } = require('path')
3-
const { spawn } = require('child_process')
4-
const { EOL } = require('os')
5-
const ini = require('ini')
1+
const { mkdir, readFile, writeFile } = require('node:fs/promises')
2+
const { dirname, resolve } = require('node:path')
3+
const { spawn } = require('node:child_process')
4+
const { EOL } = require('node:os')
65
const localeCompare = require('@isaacs/string-locale-compare')('en')
76
const pkgJson = require('@npmcli/package-json')
87
const { defaults, definitions } = require('@npmcli/config/lib/definitions')
@@ -201,6 +200,7 @@ class Config extends BaseCommand {
201200
}
202201

203202
async edit () {
203+
const ini = require('ini')
204204
const e = this.npm.flatOptions.editor
205205
const where = this.npm.flatOptions.location
206206
const file = this.npm.config.data.get(where).source

lib/commands/help-search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { readFile } = require('fs/promises')
2-
const path = require('path')
1+
const { readFile } = require('node:fs/promises')
2+
const path = require('node:path')
33
const { glob } = require('glob')
44
const { output } = require('proc-log')
55
const BaseCommand = require('../base-command.js')

lib/commands/install.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
/* eslint-disable camelcase */
2-
const fs = require('fs')
3-
const util = require('util')
4-
const readdir = util.promisify(fs.readdir)
5-
const reifyFinish = require('../utils/reify-finish.js')
1+
const { readdir } = require('node:fs/promises')
2+
const { resolve, join } = require('node:path')
63
const { log } = require('proc-log')
7-
const { resolve, join } = require('path')
84
const runScript = require('@npmcli/run-script')
95
const pacote = require('pacote')
106
const checks = require('npm-install-checks')
7+
const reifyFinish = require('../utils/reify-finish.js')
118

129
const ArboristWorkspaceCmd = require('../arborist-cmd.js')
1310
class Install extends ArboristWorkspaceCmd {

lib/commands/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { resolve } = require('path')
3+
const { resolve } = require('node:path')
44
const BaseCommand = require('../base-command.js')
55
const { log, output } = require('proc-log')
66

lib/commands/run-script.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
const runScript = require('@npmcli/run-script')
2-
const { isServerPackage } = runScript
3-
const pkgJson = require('@npmcli/package-json')
41
const { log, output } = require('proc-log')
5-
const didYouMean = require('../utils/did-you-mean.js')
6-
const { isWindowsShell } = require('../utils/is-windows.js')
7-
8-
const cmdList = [
9-
'publish',
10-
'install',
11-
'uninstall',
12-
'test',
13-
'stop',
14-
'start',
15-
'restart',
16-
'version',
17-
].reduce((l, p) => l.concat(['pre' + p, p, 'post' + p]), [])
2+
const pkgJson = require('@npmcli/package-json')
183

194
const BaseCommand = require('../base-command.js')
205
class RunScript extends BaseCommand {
@@ -64,9 +49,7 @@ class RunScript extends BaseCommand {
6449
}
6550

6651
async run ([event, ...args], { path = this.npm.localPrefix, pkg } = {}) {
67-
// this || undefined is because runScript will be unhappy with the default
68-
// null value
69-
const scriptShell = this.npm.config.get('script-shell') || undefined
52+
const runScript = require('@npmcli/run-script')
7053

7154
if (!pkg) {
7255
const { content } = await pkgJson.normalize(path)
@@ -77,19 +60,21 @@ class RunScript extends BaseCommand {
7760
if (event === 'restart' && !scripts.restart) {
7861
scripts.restart = 'npm stop --if-present && npm start'
7962
} else if (event === 'env' && !scripts.env) {
63+
const { isWindowsShell } = require('../utils/is-windows.js')
8064
scripts.env = isWindowsShell ? 'SET' : 'env'
8165
}
8266

8367
pkg.scripts = scripts
8468

8569
if (
8670
!Object.prototype.hasOwnProperty.call(scripts, event) &&
87-
!(event === 'start' && (await isServerPackage(path)))
71+
!(event === 'start' && (await runScript.isServerPackage(path)))
8872
) {
8973
if (this.npm.config.get('if-present')) {
9074
return
9175
}
9276

77+
const didYouMean = require('../utils/did-you-mean.js')
9378
const suggestions = await didYouMean(path, event)
9479
throw new Error(
9580
`Missing script: "${event}"${suggestions}\n\nTo see a list of scripts, run:\n npm run`
@@ -111,7 +96,9 @@ class RunScript extends BaseCommand {
11196
for (const [ev, evArgs] of events) {
11297
await runScript({
11398
path,
114-
scriptShell,
99+
// this || undefined is because runScript will be unhappy with the
100+
// default null value
101+
scriptShell: this.npm.config.get('script-shell') || undefined,
115102
stdio: 'inherit',
116103
pkg,
117104
event: ev,
@@ -147,6 +134,17 @@ class RunScript extends BaseCommand {
147134
return allScripts
148135
}
149136

137+
// TODO this is missing things like prepare, prepublishOnly, and dependencies
138+
const cmdList = [
139+
'preinstall', 'install', 'postinstall',
140+
'prepublish', 'publish', 'postpublish',
141+
'prerestart', 'restart', 'postrestart',
142+
'prestart', 'start', 'poststart',
143+
'prestop', 'stop', 'poststop',
144+
'pretest', 'test', 'posttest',
145+
'preuninstall', 'uninstall', 'postuninstall',
146+
'preversion', 'version', 'postversion',
147+
]
150148
const indent = '\n '
151149
const prefix = ' '
152150
const cmds = []

lib/commands/version.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
const libnpmversion = require('libnpmversion')
2-
const { resolve } = require('path')
3-
const { promisify } = require('util')
1+
const { resolve } = require('node:path')
2+
const { readFile } = require('node:fs/promises')
43
const { output } = require('proc-log')
5-
const readFile = promisify(require('fs').readFile)
64

7-
const updateWorkspaces = require('../workspaces/update-workspaces.js')
85
const BaseCommand = require('../base-command.js')
96

107
class Version extends BaseCommand {
@@ -74,6 +71,7 @@ class Version extends BaseCommand {
7471
}
7572

7673
async change (args) {
74+
const libnpmversion = require('libnpmversion')
7775
const prefix = this.npm.config.get('tag-version-prefix')
7876
const version = await libnpmversion(args[0], {
7977
...this.npm.flatOptions,
@@ -83,20 +81,33 @@ class Version extends BaseCommand {
8381
}
8482

8583
async changeWorkspaces (args) {
84+
const updateWorkspaces = require('../workspaces/update-workspaces.js')
85+
const libnpmversion = require('libnpmversion')
8686
const prefix = this.npm.config.get('tag-version-prefix')
87+
const {
88+
config,
89+
flatOptions,
90+
localPrefix,
91+
} = this.npm
8792
await this.setWorkspaces()
8893
const updatedWorkspaces = []
8994
for (const [name, path] of this.workspaces) {
9095
output.standard(name)
9196
const version = await libnpmversion(args[0], {
92-
...this.npm.flatOptions,
97+
...flatOptions,
9398
'git-tag-version': false,
9499
path,
95100
})
96101
updatedWorkspaces.push(name)
97102
output.standard(`${prefix}${version}`)
98103
}
99-
return this.update(updatedWorkspaces)
104+
return updateWorkspaces({
105+
config,
106+
flatOptions,
107+
localPrefix,
108+
npm: this.npm,
109+
workspaces: updatedWorkspaces,
110+
})
100111
}
101112

102113
async list (results = {}) {
@@ -136,22 +147,6 @@ class Version extends BaseCommand {
136147
}
137148
return this.list(results)
138149
}
139-
140-
async update (workspaces) {
141-
const {
142-
config,
143-
flatOptions,
144-
localPrefix,
145-
} = this.npm
146-
147-
await updateWorkspaces({
148-
config,
149-
flatOptions,
150-
localPrefix,
151-
npm: this.npm,
152-
workspaces,
153-
})
154-
}
155150
}
156151

157152
module.exports = Version

lib/npm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { resolve, dirname, join } = require('path')
1+
const { resolve, dirname, join } = require('node:path')
22
const Config = require('@npmcli/config')
33
const which = require('which')
4-
const fs = require('fs/promises')
4+
const fs = require('node:fs/promises')
55

66
// Patch the global fs module here at the app level
77
require('graceful-fs').gracefulify(require('fs'))

lib/package-url-cmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Base command for opening urls from a package manifest (bugs, docs, repo)
22

33
const pacote = require('pacote')
4-
const hostedGitInfo = require('hosted-git-info')
54

65
const openUrl = require('./utils/open-url.js')
76
const { log } = require('proc-log')
@@ -52,6 +51,7 @@ class PackageUrlCommand extends BaseCommand {
5251
// repository (if a string) or repository.url (if an object) returns null
5352
// if it's not a valid repo, or not a known hosted repo
5453
hostedFromMani (mani) {
54+
const hostedGitInfo = require('hosted-git-info')
5555
const r = mani.repository
5656
const rurl = !r ? null
5757
: typeof r === 'string' ? r

0 commit comments

Comments
 (0)