forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectCommandSet.ts
More file actions
43 lines (36 loc) · 1.38 KB
/
ProjectCommandSet.ts
File metadata and controls
43 lines (36 loc) · 1.38 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
IPackageJson,
IPackageJsonScriptTable
} from '@rushstack/node-core-library';
/**
* Parses the "scripts" section from package.json and provides support for executing scripts.
*/
export class ProjectCommandSet {
public readonly malformedScriptNames: string[] = [];
public readonly commandNames: string[] = [];
private readonly _scriptsByName: Map<string, string> = new Map<string, string>();
public constructor(packageJson: IPackageJson) {
const scripts: IPackageJsonScriptTable = packageJson.scripts || { };
for (const scriptName of Object.keys(scripts)) {
if (scriptName[0] === '-' || scriptName.length === 0) {
this.malformedScriptNames.push(scriptName);
} else {
this.commandNames.push(scriptName);
this._scriptsByName.set(scriptName, scripts[scriptName]);
}
}
this.commandNames.sort();
}
public tryGetScriptBody(commandName: string): string | undefined {
return this._scriptsByName.get(commandName);
}
public getScriptBody(commandName: string): string {
const result: string | undefined = this.tryGetScriptBody(commandName);
if (result === undefined) {
throw new Error(`The command "${commandName}" was not found`);
}
return result;
}
}