2018-11-16 11:36:17 -08:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// <reference types='node'/>
|
|
|
|
|
import {spawn, spawnSync} from 'child_process';
|
|
|
|
|
import {Observable, Subject} from 'rxjs';
|
|
|
|
|
|
|
|
|
|
export type Executable = 'bazel' | 'ibazel';
|
|
|
|
|
export type Command = 'build' | 'test' | 'run' | 'coverage' | 'query';
|
|
|
|
|
|
|
|
|
|
export function runBazel(
|
|
|
|
|
projectDir: string, executable: Executable, command: Command, workspaceTarget: string,
|
|
|
|
|
flags: string[]): Observable<void> {
|
|
|
|
|
const doneSubject = new Subject<void>();
|
2019-01-30 17:11:03 -08:00
|
|
|
const bin = require.resolve(`@bazel/${executable}`);
|
2019-01-22 14:32:06 -08:00
|
|
|
const buildProcess = spawn(bin, [command, workspaceTarget, ...flags], {
|
2018-11-16 11:36:17 -08:00
|
|
|
cwd: projectDir,
|
|
|
|
|
stdio: 'inherit',
|
|
|
|
|
shell: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buildProcess.once('close', (code: number) => {
|
|
|
|
|
if (code === 0) {
|
|
|
|
|
doneSubject.next();
|
|
|
|
|
} else {
|
|
|
|
|
doneSubject.error(`${executable} failed with code ${code}.`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return doneSubject.asObservable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function checkInstallation(executable: Executable, projectDir: string) {
|
2019-01-30 17:11:03 -08:00
|
|
|
let bin: string;
|
|
|
|
|
try {
|
|
|
|
|
bin = require.resolve(`@bazel/${executable}`);
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-22 14:32:06 -08:00
|
|
|
const child = spawnSync(bin, ['version'], {
|
2018-11-16 11:36:17 -08:00
|
|
|
cwd: projectDir,
|
|
|
|
|
shell: false,
|
|
|
|
|
});
|
|
|
|
|
return child.status === 0;
|
|
|
|
|
}
|