-
Notifications
You must be signed in to change notification settings - Fork 587
/
PnpmfileConfiguration.ts
135 lines (115 loc) · 5.08 KB
/
PnpmfileConfiguration.ts
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
// 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 { FileSystem, Import, type IPackageJson, JsonFile, MapExtensions } from '@rushstack/node-core-library';
import type { PnpmPackageManager } from '../../api/packageManager/PnpmPackageManager';
import type { RushConfiguration } from '../../api/RushConfiguration';
import type { CommonVersionsConfiguration } from '../../api/CommonVersionsConfiguration';
import type { PnpmOptionsConfiguration } from './PnpmOptionsConfiguration';
import * as pnpmfile from './PnpmfileShim';
import { pnpmfileShimFilename, scriptsFolderPath } from '../../utilities/PathConstants';
import type { IPnpmfileContext, IPnpmfileShimSettings } from './IPnpmfile';
import type { Subspace } from '../../api/Subspace';
/**
* Loads PNPM's pnpmfile.js configuration, and invokes it to preprocess package.json files,
* optionally utilizing a pnpmfile shim to inject preferred versions.
*/
export class PnpmfileConfiguration {
private _context: IPnpmfileContext | undefined;
private constructor(context: IPnpmfileContext) {
this._context = context;
}
public static async initializeAsync(
rushConfiguration: RushConfiguration,
subspace: Subspace
): Promise<PnpmfileConfiguration> {
if (rushConfiguration.packageManager !== 'pnpm') {
throw new Error(
`PnpmfileConfiguration cannot be used with package manager "${rushConfiguration.packageManager}"`
);
}
// Set the context to swallow log output and store our settings
const context: IPnpmfileContext = {
log: (message: string) => {},
pnpmfileShimSettings: await PnpmfileConfiguration._getPnpmfileShimSettingsAsync(
rushConfiguration,
subspace
)
};
return new PnpmfileConfiguration(context);
}
public static async writeCommonTempPnpmfileShimAsync(
rushConfiguration: RushConfiguration,
targetDir: string,
subspace: Subspace
): Promise<void> {
if (rushConfiguration.packageManager !== 'pnpm') {
throw new Error(
`PnpmfileConfiguration cannot be used with package manager "${rushConfiguration.packageManager}"`
);
}
const pnpmfilePath: string = path.join(
targetDir,
(rushConfiguration.packageManagerWrapper as PnpmPackageManager).pnpmfileFilename
);
// Write the shim itself
await FileSystem.copyFileAsync({
sourcePath: `${scriptsFolderPath}/${pnpmfileShimFilename}`,
destinationPath: pnpmfilePath
});
const pnpmfileShimSettings: IPnpmfileShimSettings =
await PnpmfileConfiguration._getPnpmfileShimSettingsAsync(rushConfiguration, subspace);
// Write the settings file used by the shim
await JsonFile.saveAsync(pnpmfileShimSettings, path.join(targetDir, 'pnpmfileSettings.json'), {
ensureFolderExists: true
});
}
private static async _getPnpmfileShimSettingsAsync(
rushConfiguration: RushConfiguration,
subspace: Subspace
): Promise<IPnpmfileShimSettings> {
let allPreferredVersions: { [dependencyName: string]: string } = {};
let allowedAlternativeVersions: { [dependencyName: string]: readonly string[] } = {};
const workspaceVersions: Record<string, string> = {};
// Only workspaces shims in the common versions using pnpmfile
if ((rushConfiguration.packageManagerOptions as PnpmOptionsConfiguration).useWorkspaces) {
const commonVersionsConfiguration: CommonVersionsConfiguration = subspace.getCommonVersions();
const preferredVersions: Map<string, string> = new Map();
MapExtensions.mergeFromMap(preferredVersions, commonVersionsConfiguration.getAllPreferredVersions());
MapExtensions.mergeFromMap(
preferredVersions,
rushConfiguration.getImplicitlyPreferredVersions(subspace)
);
allPreferredVersions = MapExtensions.toObject(preferredVersions);
allowedAlternativeVersions = MapExtensions.toObject(
commonVersionsConfiguration.allowedAlternativeVersions
);
for (const project of rushConfiguration.projects) {
workspaceVersions[project.packageName] = project.packageJson.version;
}
}
const settings: IPnpmfileShimSettings = {
allPreferredVersions,
allowedAlternativeVersions,
workspaceVersions,
semverPath: Import.resolveModule({ modulePath: 'semver', baseFolderPath: __dirname })
};
// Use the provided path if available. Otherwise, use the default path.
const userPnpmfilePath: string | undefined = subspace.getPnpmfilePath();
if (userPnpmfilePath && FileSystem.exists(userPnpmfilePath)) {
settings.userPnpmfilePath = userPnpmfilePath;
}
return settings;
}
/**
* Transform a package.json file using the pnpmfile.js hook.
* @returns the transformed object, or the original input if pnpmfile.js was not found.
*/
public transform(packageJson: IPackageJson): IPackageJson {
if (!pnpmfile.hooks?.readPackage || !this._context) {
return packageJson;
} else {
return pnpmfile.hooks.readPackage(packageJson, this._context);
}
}
}