@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
88 lines • 4.65 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { ScopedCommandLineAction } from '@rushstack/ts-command-line';
import { AlreadyReportedError } from '@rushstack/node-core-library';
import { Selection } from '../../utilities/Selection';
import { HeftActionRunner } from '../HeftActionRunner';
import { Constants } from '../../utilities/Constants';
export function expandPhases(onlyParameter, toParameter, toExceptParameter, internalHeftSession, terminal) {
const onlyPhases = evaluatePhaseParameter(onlyParameter, internalHeftSession, terminal);
const toPhases = evaluatePhaseParameter(toParameter, internalHeftSession, terminal);
const toExceptPhases = evaluatePhaseParameter(toExceptParameter, internalHeftSession, terminal);
const expandFn = (phase) => phase.dependencyPhases;
const selectedPhases = Selection.union(Selection.recursiveExpand(toPhases, expandFn), Selection.recursiveExpand(Selection.directDependenciesOf(toExceptPhases, expandFn), expandFn), onlyPhases);
if (selectedPhases.size === 0) {
throw new Error('No phases were selected. Provide at least one phase to the ' +
`${JSON.stringify(Constants.toParameterLongName)}, ` +
`${JSON.stringify(Constants.toExceptParameterLongName)}, or ` +
`${JSON.stringify(Constants.onlyParameterLongName)} parameters.`);
}
return selectedPhases;
}
function evaluatePhaseParameter(phaseParameter, internalHeftSession, terminal) {
const parameterName = phaseParameter.longName;
const selection = new Set();
for (const rawSelector of phaseParameter.values) {
const phase = internalHeftSession.phasesByName.get(rawSelector);
if (!phase) {
terminal.writeErrorLine(`The phase name ${JSON.stringify(rawSelector)} passed to ${JSON.stringify(parameterName)} does ` +
'not exist in heft.json.');
throw new AlreadyReportedError();
}
selection.add(phase);
}
return selection;
}
export function definePhaseScopingParameters(action) {
return {
toParameter: action.defineStringListParameter({
parameterLongName: Constants.toParameterLongName,
description: `The phase to ${action.actionName} to, including all transitive dependencies.`,
argumentName: 'PHASE',
parameterGroup: ScopedCommandLineAction.ScopingParameterGroup
}),
toExceptParameter: action.defineStringListParameter({
parameterLongName: Constants.toExceptParameterLongName,
description: `The phase to ${action.actionName} to (but not include), including all transitive dependencies.`,
argumentName: 'PHASE',
parameterGroup: ScopedCommandLineAction.ScopingParameterGroup
}),
onlyParameter: action.defineStringListParameter({
parameterLongName: Constants.onlyParameterLongName,
description: `The phase to ${action.actionName}.`,
argumentName: 'PHASE',
parameterGroup: ScopedCommandLineAction.ScopingParameterGroup
})
};
}
export class RunAction extends ScopedCommandLineAction {
constructor(options) {
var _a;
super({
actionName: `run${options.watch ? '-watch' : ''}`,
documentation: `Run a provided selection of Heft phases${options.watch ? ' in watch mode.' : ''}.`,
summary: `Run a provided selection of Heft phases${options.watch ? ' in watch mode.' : ''}.`
});
this.watch = (_a = options.watch) !== null && _a !== void 0 ? _a : false;
this._terminal = options.terminal;
this._internalHeftSession = options.internalHeftSession;
const { toParameter, toExceptParameter, onlyParameter } = definePhaseScopingParameters(this);
this._toParameter = toParameter;
this._toExceptParameter = toExceptParameter;
this._onlyParameter = onlyParameter;
this._actionRunner = new HeftActionRunner({ action: this, ...options });
}
get selectedPhases() {
if (!this._selectedPhases) {
this._selectedPhases = expandPhases(this._onlyParameter, this._toParameter, this._toExceptParameter, this._internalHeftSession, this._terminal);
}
return this._selectedPhases;
}
onDefineScopedParameters(scopedParameterProvider) {
this._actionRunner.defineParameters(scopedParameterProvider);
}
async onExecuteAsync() {
await this._actionRunner.executeAsync();
}
}
//# sourceMappingURL=RunAction.js.map