@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
94 lines • 5.11 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunAction = void 0;
exports.expandPhases = expandPhases;
exports.definePhaseScopingParameters = definePhaseScopingParameters;
const ts_command_line_1 = require("@rushstack/ts-command-line");
const node_core_library_1 = require("@rushstack/node-core-library");
const Selection_1 = require("../../utilities/Selection");
const HeftActionRunner_1 = require("../HeftActionRunner");
const Constants_1 = require("../../utilities/Constants");
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_1.Selection.union(Selection_1.Selection.recursiveExpand(toPhases, expandFn), Selection_1.Selection.recursiveExpand(Selection_1.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_1.Constants.toParameterLongName)}, ` +
`${JSON.stringify(Constants_1.Constants.toExceptParameterLongName)}, or ` +
`${JSON.stringify(Constants_1.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 node_core_library_1.AlreadyReportedError();
}
selection.add(phase);
}
return selection;
}
function definePhaseScopingParameters(action) {
return {
toParameter: action.defineStringListParameter({
parameterLongName: Constants_1.Constants.toParameterLongName,
description: `The phase to ${action.actionName} to, including all transitive dependencies.`,
argumentName: 'PHASE',
parameterGroup: ts_command_line_1.ScopedCommandLineAction.ScopingParameterGroup
}),
toExceptParameter: action.defineStringListParameter({
parameterLongName: Constants_1.Constants.toExceptParameterLongName,
description: `The phase to ${action.actionName} to (but not include), including all transitive dependencies.`,
argumentName: 'PHASE',
parameterGroup: ts_command_line_1.ScopedCommandLineAction.ScopingParameterGroup
}),
onlyParameter: action.defineStringListParameter({
parameterLongName: Constants_1.Constants.onlyParameterLongName,
description: `The phase to ${action.actionName}.`,
argumentName: 'PHASE',
parameterGroup: ts_command_line_1.ScopedCommandLineAction.ScopingParameterGroup
})
};
}
class RunAction extends ts_command_line_1.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_1.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();
}
}
exports.RunAction = RunAction;
//# sourceMappingURL=RunAction.js.map