|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as childProcess from 'child_process'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as os from 'os'; |
| 7 | + |
| 8 | +import { |
| 9 | + JsonFile, |
| 10 | + IPackageJson, |
| 11 | + FileSystem, |
| 12 | + PackageJsonLookup |
| 13 | +} from '@microsoft/node-core-library'; |
| 14 | +import { Constants } from './Constants'; |
| 15 | + |
| 16 | +export interface IBaseCmdTaskConfig { |
| 17 | + /** |
| 18 | + * Optional list of custom args to pass to the tool |
| 19 | + */ |
| 20 | + customArgs?: string[]; |
| 21 | + |
| 22 | + /** |
| 23 | + * The path to the package if the task should override the version of the package. |
| 24 | + */ |
| 25 | + overridePackagePath?: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * The directory in which the tool should be invoked. |
| 29 | + */ |
| 30 | + buildDirectory?: string; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Options for a CmdTask. |
| 35 | + * @public |
| 36 | + */ |
| 37 | +export interface IBaseTaskOptions<TTaskConfig> { |
| 38 | + /** |
| 39 | + * The initial config of the task. |
| 40 | + */ |
| 41 | + initialTaskConfig?: TTaskConfig; |
| 42 | + |
| 43 | + /** |
| 44 | + * The name of the package to resolve. |
| 45 | + */ |
| 46 | + packageName: string; |
| 47 | + |
| 48 | + /** |
| 49 | + * The path to the binary to invoke inside the package. |
| 50 | + */ |
| 51 | + packageBinPath: string; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * This base task provides support for finding and then executing a binary in a node package. |
| 56 | + * |
| 57 | + * @alpha |
| 58 | + */ |
| 59 | +export abstract class BaseCmdTask<TTaskConfig extends IBaseCmdTaskConfig> { |
| 60 | + private static __nodePath: string | undefined; // tslint:disable-line:variable-name |
| 61 | + private static get _nodePath(): string | undefined { |
| 62 | + if (!BaseCmdTask.__nodePath) { |
| 63 | + try { |
| 64 | + if (os.platform() === 'win32') { |
| 65 | + // We're on Windows |
| 66 | + const whereOutput: string = childProcess.execSync('where node', { stdio: [] }).toString(); |
| 67 | + const lines: string[] = whereOutput.split(os.EOL).filter((line) => !!line); |
| 68 | + |
| 69 | + // take the first result, see https://github.com/Microsoft/web-build-tools/issues/759 |
| 70 | + BaseCmdTask.__nodePath = lines[0]; |
| 71 | + } else { |
| 72 | + // We aren't on Windows - assume we're on *NIX or Darwin |
| 73 | + BaseCmdTask.__nodePath = childProcess.execSync('which node', { stdio: [] }).toString(); |
| 74 | + } |
| 75 | + } catch (e) { |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + |
| 79 | + BaseCmdTask.__nodePath = BaseCmdTask.__nodePath.trim(); |
| 80 | + if (!FileSystem.exists(BaseCmdTask.__nodePath)) { |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return BaseCmdTask.__nodePath; |
| 86 | + } |
| 87 | + |
| 88 | + protected _constants: Constants; |
| 89 | + |
| 90 | + private _packageName: string; |
| 91 | + private _packageBinPath: string; |
| 92 | + private _errorHasBeenLogged: boolean; |
| 93 | + |
| 94 | + public static getPackagePath(packageName: string): string | undefined { |
| 95 | + const packageJsonPath: string | undefined = BaseCmdTask._getPackageJsonPath(packageName); |
| 96 | + return packageJsonPath ? path.dirname(packageJsonPath) : undefined; |
| 97 | + } |
| 98 | + |
| 99 | + private static _getPackageJsonPath(packageName: string): string | undefined { |
| 100 | + const lookup: PackageJsonLookup = new PackageJsonLookup(); |
| 101 | + const mainEntryPath: string = require.resolve(packageName); |
| 102 | + return lookup.tryGetPackageJsonFilePathFor(mainEntryPath); |
| 103 | + } |
| 104 | + |
| 105 | + constructor(constants: Constants, packageName: string, packageBinPath: string) { |
| 106 | + this._constants = constants; |
| 107 | + } |
| 108 | + |
| 109 | + protected invokeCmd(): Promise<void> { |
| 110 | + let packageJsonPath: string | undefined = BaseCmdTask._getPackageJsonPath(this._packageName); |
| 111 | + |
| 112 | + if (!packageJsonPath) { |
| 113 | + return Promise.reject(new Error(`Unable to find the package.json file for ${this._packageName}.`)); |
| 114 | + } |
| 115 | + |
| 116 | + const binaryPackagePath: string = path.dirname(packageJsonPath); |
| 117 | + |
| 118 | + // Print the version |
| 119 | + const packageJson: IPackageJson = JsonFile.load(packageJsonPath); |
| 120 | + console.log(`${this._packageName} version: ${packageJson.version}`); |
| 121 | + |
| 122 | + const binaryPath: string = path.resolve(binaryPackagePath, this._packageBinPath); |
| 123 | + if (!FileSystem.exists(binaryPath)) { |
| 124 | + return Promise.reject(new Error( |
| 125 | + `The binary is missing. This indicates that ${this._packageName} is not ` + |
| 126 | + 'installed correctly.' |
| 127 | + )); |
| 128 | + } |
| 129 | + |
| 130 | + return new Promise((resolve: () => void, reject: (error: Error) => void) => { |
| 131 | + const nodePath: string | undefined = BaseCmdTask._nodePath; |
| 132 | + if (!nodePath) { |
| 133 | + reject(new Error('Unable to find node executable')); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Invoke the tool and watch for log messages |
| 138 | + const spawnResult: childProcess.ChildProcess = childProcess.spawn( |
| 139 | + nodePath, |
| 140 | + [binaryPath, ...this._getArgs()], |
| 141 | + { |
| 142 | + cwd: this._constants.projectFolderPath, |
| 143 | + env: process.env, |
| 144 | + stdio: 'pipe' |
| 145 | + } |
| 146 | + ); |
| 147 | + |
| 148 | + spawnResult.stdout.on('data', this._onData.bind(this)); |
| 149 | + spawnResult.stderr.on('data', (data: Buffer) => { |
| 150 | + this._errorHasBeenLogged = true; |
| 151 | + this._onError(data); |
| 152 | + }); |
| 153 | + |
| 154 | + spawnResult.on('close', (code) => this._onClose(code, this._errorHasBeenLogged, resolve, reject)); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + protected _onData(data: Buffer): void { |
| 159 | + console.log(data.toString().trim()); |
| 160 | + } |
| 161 | + |
| 162 | + protected _onError(data: Buffer): void { |
| 163 | + console.error(data.toString().trim()); |
| 164 | + } |
| 165 | + |
| 166 | + protected _onClose(code: number, hasErrors: boolean, resolve: () => void, reject: (error: Error) => void): void { |
| 167 | + if (code !== 0 || hasErrors) { |
| 168 | + reject(new Error(`exited with code ${code}`)); |
| 169 | + } else { |
| 170 | + resolve(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + protected _getArgs(): string[] { |
| 175 | + return []; |
| 176 | + } |
| 177 | +} |
0 commit comments