forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoinstallerPluginLoader.ts
More file actions
171 lines (148 loc) · 6.01 KB
/
AutoinstallerPluginLoader.ts
File metadata and controls
171 lines (148 loc) · 6.01 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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 'node:path';
import {
FileSystem,
JsonFile,
NewlineKind,
PosixModeBits,
type JsonObject,
type JsonSchema
} from '@rushstack/node-core-library';
import type { IRushPluginConfiguration } from '../../api/RushPluginsConfiguration';
import { Autoinstaller } from '../../logic/Autoinstaller';
import { RushConstants } from '../../logic/RushConstants';
import {
type IPluginLoaderOptions,
type IRushPluginManifest,
type IRushPluginManifestJson,
PluginLoaderBase
} from './PluginLoaderBase';
import type { RushGlobalFolder } from '../../api/RushGlobalFolder';
interface IAutoinstallerPluginLoaderOptions extends IPluginLoaderOptions<IRushPluginConfiguration> {
restrictConsoleOutput: boolean;
rushGlobalFolder: RushGlobalFolder;
}
/**
* @beta
*/
export class AutoinstallerPluginLoader extends PluginLoaderBase<IRushPluginConfiguration> {
public readonly packageFolder: string;
public readonly autoinstaller: Autoinstaller;
public constructor(options: IAutoinstallerPluginLoaderOptions) {
super(options);
this.autoinstaller = new Autoinstaller({
autoinstallerName: options.pluginConfiguration.autoinstallerName,
rushConfiguration: this._rushConfiguration,
restrictConsoleOutput: options.restrictConsoleOutput,
rushGlobalFolder: options.rushGlobalFolder
});
this.packageFolder = path.join(this.autoinstaller.folderFullPath, 'node_modules', this.packageName);
}
/**
* The folder where rush plugins static files are stored.
* Example: `C:\MyRepo\common\autoinstallers\<autoinstaller_name>\rush-plugins`
*/
public static getPluginAutoinstallerStorePath(autoinstaller: Autoinstaller): string {
return path.join(autoinstaller.folderFullPath, 'rush-plugins');
}
public update(): void {
const packageName: string = this.packageName;
const pluginName: string = this.pluginName;
const packageFolder: string = this.packageFolder;
const manifestPath: string = path.join(packageFolder, RushConstants.rushPluginManifestFilename);
// validate
const manifest: IRushPluginManifestJson = JsonFile.loadAndValidate(
manifestPath,
AutoinstallerPluginLoader._jsonSchema
);
const destinationManifestPath: string = this._getManifestPath();
// Use read+write instead of copy to ensure line endings are normalized
const manifestContent: string = FileSystem.readFile(manifestPath);
FileSystem.writeFile(destinationManifestPath, manifestContent, {
convertLineEndings: NewlineKind.Lf
});
// Make permission consistent since it will be committed to Git
FileSystem.changePosixModeBits(
destinationManifestPath,
// eslint-disable-next-line no-bitwise
PosixModeBits.AllRead | PosixModeBits.UserWrite
);
const pluginManifest: IRushPluginManifest | undefined = manifest.plugins.find(
(item) => item.pluginName === pluginName
);
if (!pluginManifest) {
throw new Error(
`A plugin named "${pluginName}" is not provided by the Rush plugin package "${packageName}"`
);
}
const commandLineJsonFilePath: string | undefined = pluginManifest.commandLineJsonFilePath;
if (commandLineJsonFilePath) {
const commandLineJsonFullFilePath: string = path.join(packageFolder, commandLineJsonFilePath);
if (!FileSystem.exists(commandLineJsonFullFilePath)) {
this._terminal.writeErrorLine(
`The Rush plugin "${pluginName}" from "${packageName}" specifies a commandLineJsonFilePath` +
` ${commandLineJsonFilePath} that does not exist.`
);
}
const destinationCommandLineJsonFilePath: string = this._getCommandLineJsonFilePath();
// Use read+write instead of copy to ensure line endings are normalized
const commandLineContent: string = FileSystem.readFile(commandLineJsonFullFilePath);
FileSystem.writeFile(destinationCommandLineJsonFilePath, commandLineContent, {
convertLineEndings: NewlineKind.Lf
});
// Make permission consistent since it will be committed to Git
FileSystem.changePosixModeBits(
destinationCommandLineJsonFilePath,
// eslint-disable-next-line no-bitwise
PosixModeBits.AllRead | PosixModeBits.UserWrite
);
}
}
protected override _getCommandLineAdditionalPathFolders(): string[] {
const additionalPathFolders: string[] = super._getCommandLineAdditionalPathFolders();
additionalPathFolders.push(
// Example: `common/autoinstaller/plugins/node_modules/.bin`
path.join(this.autoinstaller.folderFullPath, 'node_modules', '.bin')
);
return additionalPathFolders;
}
protected override _getPluginOptions(): JsonObject {
const optionsJsonFilePath: string = this._getPluginOptionsJsonFilePath();
const optionsSchema: JsonSchema | undefined = this._getRushPluginOptionsSchema();
let pluginOptions: JsonObject = {};
try {
pluginOptions = JsonFile.load(optionsJsonFilePath);
} catch (e) {
if (FileSystem.isFileDoesNotExistError(e as Error)) {
if (optionsSchema) {
throw new Error(
`Plugin options are required by ${this.pluginName} from package ${this.packageName}, please create it at ${optionsJsonFilePath}.`
);
} else {
return {};
}
}
throw e;
}
if (optionsSchema) {
optionsSchema.validateObject(pluginOptions, optionsJsonFilePath);
}
return pluginOptions;
}
protected override _getManifestPath(): string {
return path.join(
AutoinstallerPluginLoader.getPluginAutoinstallerStorePath(this.autoinstaller),
this.packageName,
RushConstants.rushPluginManifestFilename
);
}
protected override _getCommandLineJsonFilePath(): string {
return path.join(
AutoinstallerPluginLoader.getPluginAutoinstallerStorePath(this.autoinstaller),
this.packageName,
this.pluginName,
RushConstants.commandLineFilename
);
}
}