private async npmCommandPath()

in src/docgen/view/_npm.ts [57:95]


  private async npmCommandPath(): Promise<string> {
    if (this.#npmCommand) {
      return this.#npmCommand;
    }

    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'],
        chunksToObject,
      );
      if (exitCode === 0 && major((stdout as any).npm) >= 7) {
        return this.#npmCommand = 'npm';
      }
    } catch (e) {
      this.logger('Could not determine version of npm in $PATH:', e);
    }

    // npm@8 is needed so that we also install peerDependencies - they are needed to construct
    // the full type system.
    this.logger('The npm in $PATH is not >= v7. Installing npm@8 locally...');
    const result= await this.runCommand(
      'npm',
      ['install', 'npm@8', '--no-package-lock', '--no-save', '--json'],
      chunksToObject,
      {
        cwd: this.workingDirectory,
        shell: true,
      },
    );
    assertSuccess(result);

    this.#npmCommand = join(this.workingDirectory, 'node_modules', '.bin', 'npm');
    this.logger(`Done installing npm@8 at ${this.#npmCommand}`);
    return this.#npmCommand;
  }