Skip to content

Commit 0df738c

Browse files
committed
fix: launch npm with Node in package tests
1 parent 3975dc5 commit 0df738c

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎scripts/test-package.mjs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
mkdtemp, readdir, readFile, rm, stat,
77
} from 'node:fs/promises'
88
import http from 'node:http'
9+
import {createRequire} from 'node:module'
910
import {tmpdir} from 'node:os'
1011
import path from 'node:path'
1112
import {performance} from 'node:perf_hooks'
@@ -15,8 +16,12 @@ import {promisify} from 'node:util'
1516
const exec = promisify(execFile)
1617
const project = fileURLToPath(new URL('..', import.meta.url))
1718
const {version} = JSON.parse(await readFile(path.join(project, 'package.json'), 'utf8'))
19+
// Run npm with Node directly so Windows never needs to launch a .cmd shim.
20+
const npmPackage = createRequire(import.meta.url).resolve('npm/package.json', {
21+
paths: [path.dirname(process.execPath), path.join(path.dirname(process.execPath), '..', 'lib')],
22+
})
23+
const npmCli = path.join(path.dirname(npmPackage), 'bin', 'npm-cli.js')
1824
const temporary = await mkdtemp(path.join(tmpdir(), 'hackmd-cli-package-'))
19-
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
2025
const requests = []
2126
const note = {
2227
content: '# Test', id: 'note-1', tags: ['test'], teamPath: null, title: '中文😀', userPath: 'tester',
@@ -80,13 +85,14 @@ try {
8085
XDG_CACHE_HOME: path.join(temporary, 'cache'),
8186
XDG_CONFIG_HOME: path.join(temporary, '.config'),
8287
}
83-
const packed = await exec(npm, ['pack', '--pack-destination', temporary], {cwd: project, maxBuffer: 10 * 1024 * 1024})
88+
const packed = await exec(process.execPath, [npmCli, 'pack', '--pack-destination', temporary], {cwd: project, maxBuffer: 10 * 1024 * 1024})
8489
const filename = path.join(temporary, packed.stdout.trim().split('\n').at(-1))
8590
const results = []
8691
for (const ignoreScripts of [true, false]) {
8792
const prefix = path.join(temporary, ignoreScripts ? 'ignore-scripts' : 'with-scripts')
8893
const started = performance.now()
89-
const installed = await exec(npm, [
94+
const installed = await exec(process.execPath, [
95+
npmCli,
9096
'install',
9197
'--global',
9298
filename,

0 commit comments

Comments
 (0)