66 mkdtemp , readdir , readFile , rm , stat ,
77} from 'node:fs/promises'
88import http from 'node:http'
9+ import { createRequire } from 'node:module'
910import { tmpdir } from 'node:os'
1011import path from 'node:path'
1112import { performance } from 'node:perf_hooks'
@@ -15,8 +16,12 @@ import {promisify} from 'node:util'
1516const exec = promisify ( execFile )
1617const project = fileURLToPath ( new URL ( '..' , import . meta. url ) )
1718const { 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' )
1824const temporary = await mkdtemp ( path . join ( tmpdir ( ) , 'hackmd-cli-package-' ) )
19- const npm = process . platform === 'win32' ? 'npm.cmd' : 'npm'
2025const requests = [ ]
2126const 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