Skip to content

Commit 487acf1

Browse files
committed
Extract ProjectCommandSet into its own file
1 parent c843f35 commit 487acf1

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

apps/rush-lib/src/api/Rush.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IPackageJson } from '@microsoft/node-core-library';
88

99
import { RushCommandLineParser } from '../cli/RushCommandLineParser';
1010
import { RushConstants } from './RushConstants';
11-
import { RushX } from './RushX';
11+
import { RushXCommandLine } from '../cli/RushXCommandLine';
1212
import { CommandLineMigrationAdvisor } from '../cli/CommandLineMigrationAdvisor';
1313

1414
/**
@@ -55,7 +55,7 @@ export class Rush {
5555
public static launchRushX(launcherVersion: string, isManaged: boolean): void {
5656
Rush._printStartupBanner(isManaged);
5757

58-
RushX.launchRushX(launcherVersion, isManaged);
58+
RushXCommandLine.launchRushX(launcherVersion, isManaged);
5959
}
6060

6161
/**

apps/rush-lib/src/cli/RushXCommandLine.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,12 @@ import * as path from 'path';
99
import {
1010
PackageJsonLookup,
1111
IPackageJson,
12-
Text,
13-
IPackageJsonScriptTable
12+
Text
1413
} from '@microsoft/node-core-library';
1514
import { Utilities } from '../utilities/Utilities';
15+
import { ProjectCommandSet } from '../logic/ProjectCommandSet';
1616

17-
/**
18-
* Parses the "scripts" section from package.json
19-
*/
20-
class ProjectCommandSet {
21-
public readonly malformedScriptNames: string[] = [];
22-
public readonly commandNames: string[] = [];
23-
private readonly _scriptsByName: Map<string, string> = new Map<string, string>();
24-
25-
public constructor(packageJson: IPackageJson) {
26-
const scripts: IPackageJsonScriptTable = packageJson.scripts || { };
27-
28-
for (const scriptName of Object.keys(scripts)) {
29-
if (scriptName[0] === '-' || scriptName.length === 0) {
30-
this.malformedScriptNames.push(scriptName);
31-
} else {
32-
this.commandNames.push(scriptName);
33-
this._scriptsByName.set(scriptName, scripts[scriptName]);
34-
}
35-
}
36-
37-
this.commandNames.sort();
38-
}
39-
40-
public tryGetScriptBody(commandName: string): string | undefined {
41-
return this._scriptsByName.get(commandName);
42-
}
43-
44-
public getScriptBody(commandName: string): string {
45-
const result: string | undefined = this.tryGetScriptBody(commandName);
46-
if (result === undefined) {
47-
throw new Error(`The command "${commandName}" was not found`);
48-
}
49-
return result;
50-
}
51-
}
52-
53-
export class RushX {
17+
export class RushXCommandLine {
5418
public static launchRushX(launcherVersion: string, isManaged: boolean): void {
5519
// NodeJS can sometimes accidentally terminate with a zero exit code (e.g. for an uncaught
5620
// promise exception), so we start with the assumption that the exit code is 1
@@ -82,7 +46,7 @@ export class RushX {
8246
// rush -h
8347
// rush --unrecognized-option
8448
if (args.length === 0 || args[0][0] === '-') {
85-
RushX._showUsage(packageJson, projectCommandSet);
49+
RushXCommandLine._showUsage(packageJson, projectCommandSet);
8650
return;
8751
}
8852

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import {
5+
IPackageJson,
6+
IPackageJsonScriptTable
7+
} from '@microsoft/node-core-library';
8+
9+
/**
10+
* Parses the "scripts" section from package.json and provides support for executing scripts.
11+
*/
12+
export class ProjectCommandSet {
13+
public readonly malformedScriptNames: string[] = [];
14+
public readonly commandNames: string[] = [];
15+
private readonly _scriptsByName: Map<string, string> = new Map<string, string>();
16+
17+
public constructor(packageJson: IPackageJson) {
18+
const scripts: IPackageJsonScriptTable = packageJson.scripts || { };
19+
20+
for (const scriptName of Object.keys(scripts)) {
21+
if (scriptName[0] === '-' || scriptName.length === 0) {
22+
this.malformedScriptNames.push(scriptName);
23+
} else {
24+
this.commandNames.push(scriptName);
25+
this._scriptsByName.set(scriptName, scripts[scriptName]);
26+
}
27+
}
28+
29+
this.commandNames.sort();
30+
}
31+
32+
public tryGetScriptBody(commandName: string): string | undefined {
33+
return this._scriptsByName.get(commandName);
34+
}
35+
36+
public getScriptBody(commandName: string): string {
37+
const result: string | undefined = this.tryGetScriptBody(commandName);
38+
if (result === undefined) {
39+
throw new Error(`The command "${commandName}" was not found`);
40+
}
41+
return result;
42+
}
43+
}

0 commit comments

Comments
 (0)