-
Notifications
You must be signed in to change notification settings - Fork 697
Expand file tree
/
Copy pathPnpmWorkspaceFile.ts
More file actions
160 lines (137 loc) · 5.49 KB
/
Copy pathPnpmWorkspaceFile.ts
File metadata and controls
160 lines (137 loc) · 5.49 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
// 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 { escapePath as globEscape } from 'fast-glob';
import { Sort, Import, Path } from '@rushstack/node-core-library';
import { BaseWorkspaceFile } from '../base/BaseWorkspaceFile';
import { PNPM_SHRINKWRAP_YAML_FORMAT } from './PnpmYamlCommon';
const yamlModule: typeof import('js-yaml') = Import.lazy('js-yaml', require);
/**
* This interface represents the raw pnpm-workspace.YAML file
* Example:
* {
* "packages": [
* "../../apps/project1"
* ],
* "catalogs": {
* "default": {
* "react": "^18.0.0"
* }
* },
* "allowBuilds": {
* "esbuild": true,
* "fsevents": false
* }
* }
*/
interface IPnpmWorkspaceYaml {
/** The list of local package directories */
packages: string[];
/** Catalog definitions for centralized version management */
catalogs?: Record<string, Record<string, string>>;
/**
* Controls which packages are allowed to run build scripts. A value of `true` means the
* package is allowed to run build scripts; `false` means it is explicitly denied.
* Packages with build scripts not listed here will cause pnpm to fail with ERR_PNPM_IGNORED_BUILDS.
* (SUPPORTED ONLY IN PNPM 11.0.0 AND NEWER)
*/
allowBuilds?: Record<string, boolean>;
/**
* The minimum number of minutes that must pass after a version is published before pnpm will install it.
* (SUPPORTED ONLY IN PNPM 10.16.0 AND NEWER)
*/
minimumReleaseAge?: number;
/**
* List of package names or patterns that are excluded from the minimumReleaseAge check.
* (SUPPORTED ONLY IN PNPM 10.16.0 AND NEWER)
*/
minimumReleaseAgeExclude?: string[];
}
export class PnpmWorkspaceFile extends BaseWorkspaceFile {
/**
* The filename of the workspace file.
*/
public readonly workspaceFilename: string;
private _workspacePackages: Set<string>;
private _catalogs: Record<string, Record<string, string>> | undefined;
private _allowBuilds: Record<string, boolean> | undefined;
private _minimumReleaseAge: number | undefined;
private _minimumReleaseAgeExclude: string[] | undefined;
/**
* The PNPM workspace file is used to specify the location of workspaces relative to the root
* of your PNPM install.
*/
public constructor(workspaceYamlFilename: string) {
super();
this.workspaceFilename = workspaceYamlFilename;
// Ignore any existing file since this file is generated and we need to handle deleting packages
// If we need to support manual customization, that should be an additional parameter for "base file"
this._workspacePackages = new Set<string>();
this._catalogs = undefined;
this._allowBuilds = undefined;
this._minimumReleaseAge = undefined;
this._minimumReleaseAgeExclude = undefined;
}
/**
* Sets the catalog definitions for the workspace.
* @param catalogs - A map of catalog name to package versions
*/
public setCatalogs(catalogs: Record<string, Record<string, string>> | undefined): void {
this._catalogs = catalogs;
}
/**
* Sets the allowBuilds definitions for the workspace.
* This controls which packages are allowed to run build scripts in pnpm 11+.
* @param allowBuilds - A map of package name to boolean (true = allowed, false = denied)
*/
public setAllowBuilds(allowBuilds: Record<string, boolean> | undefined): void {
this._allowBuilds = allowBuilds;
}
/**
* Sets the minimumReleaseAge setting for the workspace.
* The minimum number of minutes that must pass after a version is published before pnpm will install it.
* (SUPPORTED ONLY IN PNPM 10.16.0 AND NEWER)
*/
public setMinimumReleaseAge(minimumReleaseAge: number | undefined): void {
this._minimumReleaseAge = minimumReleaseAge;
}
/**
* Sets the minimumReleaseAgeExclude setting for the workspace.
* List of package names or patterns that are excluded from the minimumReleaseAge check.
* (SUPPORTED ONLY IN PNPM 10.16.0 AND NEWER)
*/
public setMinimumReleaseAgeExclude(minimumReleaseAgeExclude: string[] | undefined): void {
this._minimumReleaseAgeExclude = minimumReleaseAgeExclude;
}
/** @override */
public addPackage(packagePath: string): void {
// Ensure the path is relative to the pnpm-workspace.yaml file
if (path.isAbsolute(packagePath)) {
packagePath = path.relative(path.dirname(this.workspaceFilename), packagePath);
}
// Glob can't handle Windows paths
const globPath: string = Path.convertToSlashes(packagePath);
this._workspacePackages.add(globEscape(globPath));
}
/** @override */
protected serialize(): string {
// Ensure stable sort order when serializing
Sort.sortSet(this._workspacePackages);
const workspaceYaml: IPnpmWorkspaceYaml = {
packages: Array.from(this._workspacePackages)
};
if (this._catalogs && Object.keys(this._catalogs).length > 0) {
workspaceYaml.catalogs = this._catalogs;
}
if (this._allowBuilds && Object.keys(this._allowBuilds).length > 0) {
workspaceYaml.allowBuilds = this._allowBuilds;
}
if (this._minimumReleaseAge !== undefined) {
workspaceYaml.minimumReleaseAge = this._minimumReleaseAge;
}
if (this._minimumReleaseAgeExclude && this._minimumReleaseAgeExclude.length > 0) {
workspaceYaml.minimumReleaseAgeExclude = this._minimumReleaseAgeExclude;
}
return yamlModule.dump(workspaceYaml, PNPM_SHRINKWRAP_YAML_FORMAT);
}
}