Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions server/src/node/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ function isWindows(): boolean {
return process.platform === 'win32';
}

function runGlobalPackageManagerCommand(command: string, args: string[], options: SpawnSyncOptionsWithStringEncoding) {
if (isWindows()) {
const comSpec = process.env.ComSpec ?? 'cmd.exe';
return spawnSync(comSpec, ['/d', '/s', '/c', `${command}.cmd`, ...args], options);
}
return spawnSync(command, args, options);
}

export function resolve(moduleName: string, nodePath: string | undefined, cwd: string | undefined, tracer: (message: string, verbose?: string) => void): Promise<string> {
interface Message {
c: string;
Expand Down Expand Up @@ -124,23 +132,18 @@ export function resolve(moduleName: string, nodePath: string | undefined, cwd: s
* @param tracer the tracer to use
*/
export function resolveGlobalNodePath(tracer?: (message: string) => void): string | undefined {
let npmCommand = 'npm';
const env: typeof process.env = Object.create(null);
Object.keys(process.env).forEach(key => env[key] = process.env[key]);
env['NO_UPDATE_NOTIFIER'] = 'true';
const options: SpawnSyncOptionsWithStringEncoding = {
encoding: 'utf8',
env
};
if (isWindows()) {
npmCommand = 'npm.cmd';
options.shell = true;
}

const handler = () => {};
try {
process.on('SIGPIPE', handler);
const stdout = spawnSync(npmCommand, ['config', 'get', 'prefix'], options).stdout;
const stdout = runGlobalPackageManagerCommand('npm', ['config', 'get', 'prefix'], options).stdout;

if (!stdout) {
if (tracer) {
Expand Down Expand Up @@ -180,20 +183,14 @@ interface YarnJsonFormat {
* @param tracer the tracer to use
*/
export function resolveGlobalYarnPath(tracer?: (message: string) => void): string | undefined {
let yarnCommand = 'yarn';
const options: SpawnSyncOptionsWithStringEncoding = {
encoding: 'utf8'
};

if (isWindows()) {
yarnCommand = 'yarn.cmd';
options.shell = true;
}

const handler = () => {};
try {
process.on('SIGPIPE', handler);
const results = spawnSync(yarnCommand, ['global', 'dir', '--json'], options);
const results = runGlobalPackageManagerCommand('yarn', ['global', 'dir', '--json'], options);

const stdout = results.stdout;
if (!stdout) {
Expand Down Expand Up @@ -268,4 +265,4 @@ export function resolveModulePath(workspaceRoot: string, moduleName: string, nod
} else {
return resolve(moduleName, resolveGlobalNodePath(tracer), workspaceRoot, tracer);
}
}
}