forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConnectPackageAction.ts
More file actions
77 lines (66 loc) · 3.07 KB
/
BaseConnectPackageAction.ts
File metadata and controls
77 lines (66 loc) · 3.07 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { CommandLineStringListParameter, CommandLineStringParameter } from '@rushstack/ts-command-line';
import path from 'path';
import type { RushConfigurationProject } from '../../api/RushConfigurationProject';
import { RushConnect } from '../../utilities/RushConnect';
import { BaseRushAction, type IBaseRushActionOptions } from './BaseRushAction';
import { Async } from '@rushstack/node-core-library';
export abstract class BaseConnectPackageAction extends BaseRushAction {
protected readonly _projectList: CommandLineStringListParameter;
protected readonly _pathParameter: CommandLineStringParameter;
protected constructor(options: IBaseRushActionOptions) {
super(options);
this._pathParameter = this.defineStringParameter({
parameterLongName: '--path',
argumentName: 'PATH',
required: true,
description:
'The folder path of a locally built project, whose installation will be simulated using node_modules' +
' symlinks. This folder will be the target of the symlinks.'
});
this._projectList = this.defineStringListParameter({
parameterLongName: '--project',
required: false,
argumentName: 'PROJECT',
description:
'A list of Rush project names to connect to the external package. ' +
'If not specified, uses the project in the current working directory.'
});
}
protected abstract connectPackageAsync(
consumerPackage: RushConfigurationProject,
linkedPackagePath: string,
rushConnect: RushConnect
): Promise<void>;
protected async getProjectsToLinkAsync(): Promise<Set<RushConfigurationProject>> {
const projectsToLink: Set<RushConfigurationProject> = new Set();
const projectNames: readonly string[] = this._projectList.values;
if (projectNames.length > 0) {
for (const projectName of projectNames) {
const project: RushConfigurationProject | undefined =
this.rushConfiguration.getProjectByName(projectName);
if (!project) {
throw new Error(`The project "${projectName}" was not found in the "rush.json"`);
}
projectsToLink.add(project);
}
} else {
const currentProject: RushConfigurationProject | undefined =
this.rushConfiguration.tryGetProjectForPath(process.cwd());
if (!currentProject) {
throw new Error(`No Rush project was found in the current working directory`);
}
projectsToLink.add(currentProject);
}
return projectsToLink;
}
protected async runAsync(): Promise<void> {
const rushConnect: RushConnect = RushConnect.loadFromLinkStateFile(this.rushConfiguration);
const linkedPackagePath: string = path.resolve(this._pathParameter.value!);
const projectsToLink: Set<RushConfigurationProject> = await this.getProjectsToLinkAsync();
await Async.forEachAsync(projectsToLink, async (project) => {
await this.connectPackageAsync(project, linkedPackagePath, rushConnect);
});
}
}