forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineConfiguration.ts
More file actions
76 lines (63 loc) · 2.41 KB
/
CommandLineConfiguration.ts
File metadata and controls
76 lines (63 loc) · 2.41 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import {
JsonFile,
JsonSchema,
FileSystem
} from '@microsoft/node-core-library';
import { RushConstants } from '../logic/RushConstants';
import {
CommandJson,
ICommandLineJson,
ParameterJson
} from './CommandLineJson';
/**
* Custom Commands and Options for the Rush Command Line
*/
export class CommandLineConfiguration {
private static _jsonSchema: JsonSchema = JsonSchema.fromFile(
path.join(__dirname, '../schemas/command-line.schema.json'));
public readonly commands: CommandJson[] = [];
public readonly parameters: ParameterJson[] = [];
/**
* Loads the configuration from the specified file. If the file does not exist,
* then an empty default instance is returned. If the file contains errors, then
* an exception is thrown.
*/
public static loadFromFileOrDefault(jsonFilename: string): CommandLineConfiguration {
let commandLineJson: ICommandLineJson | undefined = undefined;
if (FileSystem.exists(jsonFilename)) {
commandLineJson = JsonFile.loadAndValidate(jsonFilename, CommandLineConfiguration._jsonSchema);
}
return new CommandLineConfiguration(commandLineJson);
}
/**
* Use CommandLineConfiguration.loadFromFile()
*/
private constructor(commandLineJson: ICommandLineJson | undefined) {
if (commandLineJson) {
if (commandLineJson.commands) {
for (const command of commandLineJson.commands) {
this.commands.push(command);
}
}
if (commandLineJson.parameters) {
for (const parameter of commandLineJson.parameters) {
this.parameters.push(parameter);
// Do some basic validation
switch (parameter.parameterKind) {
case 'choice':
const alternativeNames: string[] = parameter.alternatives.map(x => x.name);
if (parameter.defaultValue && alternativeNames.indexOf(parameter.defaultValue) < 0) {
throw new Error(`In ${RushConstants.commandLineFilename}, the parameter "${parameter.longName}",`
+ ` specifies a default value "${parameter.defaultValue}"`
+ ` which is not one of the defined alternatives: "${alternativeNames.toString()}"`);
}
break;
}
}
}
}
}
}