Skip to content

Commit

Permalink
make npm helper windows aware
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Apr 29, 2024
1 parent 5c2416d commit 540d744
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/docgen/view/_npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ export class Npm {
return this.#npmCommand;
}

// Get the platform specific npm command
const npm = npmPlatformAwareCommand();

try {
// If the npm in $PATH is >= v7, we can use that directly. The
// `npm version --json` command returns a JSON object containing the
// versions of several components (npm, node, v8, etc...). We are only
// interested in the `npm` key here.
const { exitCode, stdout } = await this.runCommand(
'npm', ['version', '--json'],
npm, ['version', '--json'],
chunksToObject,
);
if (exitCode === 0 && major((stdout as any).npm) >= 7) {
return this.#npmCommand = 'npm';
return this.#npmCommand = npm;
}
} catch (e) {
this.logger('Could not determine version of npm in $PATH:', e);
Expand All @@ -152,7 +155,7 @@ export class Npm {
// the full type system.
this.logger('The npm in $PATH is not >= v7. Installing npm@8 locally...');
const result = await this.runCommand(
'npm',
npm,
['install', 'npm@8', '--no-package-lock', '--no-save', '--json'],
chunksToObject,
{
Expand All @@ -162,7 +165,7 @@ export class Npm {
);
assertSuccess(result);

this.#npmCommand = join(this.workingDirectory, 'node_modules', '.bin', 'npm');
this.#npmCommand = join(this.workingDirectory, 'node_modules', '.bin', npm);
this.logger(`Done installing npm@8 at ${this.#npmCommand}`);
return this.#npmCommand;
}
Expand Down Expand Up @@ -330,3 +333,15 @@ type ResponseObject =
| { readonly error: { readonly code: string; readonly summary: string; readonly detail: string } }
// The successful objects are treated as opaque blobs here
| { readonly error: undefined; readonly [key: string]: unknown };

/**
* Get the npm binary path depending on the platform.
* @returns "npm.exe" on Windows, otherwise "npm"
*/
function npmPlatformAwareCommand() {
if (process.platform === 'win32') {
return 'npm.cmd';
}

return 'npm';
}

0 comments on commit 540d744

Please sign in to comment.