forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseScriptAction.ts
More file actions
90 lines (81 loc) · 3.36 KB
/
BaseScriptAction.ts
File metadata and controls
90 lines (81 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { CommandLineParameter } from '@microsoft/ts-command-line';
import { BaseRushAction, IBaseRushActionOptions } from '../actions/BaseRushAction';
import { CommandLineConfiguration } from '../../api/CommandLineConfiguration';
import { RushConstants } from '../../logic/RushConstants';
/**
* Constructor parameters for BaseScriptAction
*/
export interface IBaseScriptActionOptions extends IBaseRushActionOptions {
commandLineConfiguration: CommandLineConfiguration | undefined;
}
/**
* Base class for command-line actions that are implemented using user-defined scripts.
*
* @remarks
* Compared to the normal built-in actions, these actions are special because (1) they
* can be discovered dynamically via common/config/command-line.json, and (2)
* user-defined command-line parameters can be passed through to the script.
*
* The two subclasses are BulkScriptAction and GlobalScriptAction.
*/
export abstract class BaseScriptAction extends BaseRushAction {
protected readonly _commandLineConfiguration: CommandLineConfiguration | undefined;
protected readonly customParameters: CommandLineParameter[] = [];
constructor(
options: IBaseScriptActionOptions
) {
super(options);
this._commandLineConfiguration = options.commandLineConfiguration;
}
protected defineScriptParameters(): void {
if (!this._commandLineConfiguration) {
return;
}
// Find any parameters that are associated with this command
for (const parameterJson of this._commandLineConfiguration.parameters) {
let associated: boolean = false;
for (const associatedCommand of parameterJson.associatedCommands) {
if (associatedCommand === this.actionName) {
associated = true;
}
}
if (associated) {
let customParameter: CommandLineParameter | undefined;
switch (parameterJson.parameterKind) {
case 'flag':
customParameter = this.defineFlagParameter({
parameterShortName: parameterJson.shortName,
parameterLongName: parameterJson.longName,
description: parameterJson.description
});
break;
case 'choice':
customParameter = this.defineChoiceParameter({
parameterShortName: parameterJson.shortName,
parameterLongName: parameterJson.longName,
description: parameterJson.description,
alternatives: parameterJson.alternatives.map(x => x.name),
defaultValue: parameterJson.defaultValue
});
break;
case 'string':
customParameter = this.defineStringParameter({
parameterLongName: parameterJson.longName,
parameterShortName: parameterJson.shortName,
description: parameterJson.description,
argumentName: parameterJson.argumentName
});
break;
default:
throw new Error(`${RushConstants.commandLineFilename} defines a parameter "${parameterJson!.longName}"`
+ ` using an unsupported parameter kind "${parameterJson!.parameterKind}"`);
}
if (customParameter) {
this.customParameters.push(customParameter);
}
}
}
}
}