forked from angular/dev-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-module-bazel.ts
More file actions
239 lines (199 loc) · 7.32 KB
/
sync-module-bazel.ts
File metadata and controls
239 lines (199 loc) · 7.32 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @license
* Copyright Google LLC
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Log} from '../../utils/logging';
export interface PackageJson {
engines?: {
pnpm?: string;
node?: string;
};
dependencies?: {
typescript?: string;
};
devDependencies?: {
typescript?: string;
};
}
interface RepositoryInfo {
filename: string;
sha: string;
type: string;
}
const REPOSITORY_TYPES: Record<string, string> = {
'darwin-arm64.tar.gz': 'darwin_arm64',
'darwin-x64.tar.gz': 'darwin_amd64',
'linux-x64.tar.xz': 'linux_amd64',
'linux-arm64.tar.xz': 'linux_arm64',
'linux-s390x.tar.xz': 'linux_s390x',
'win-x64.zip': 'windows_amd64',
'linux-ppc64le.tar.xz': 'linux_ppc64le',
};
/** RegExp that matches the pnpm version assignment in MODULE.bazel. */
const PNPM_VERSION_REGEXP = /pnpm_version(?:_from)? = ".*?"/;
/** RegExp that matches the pnpm integrity assignment in MODULE.bazel. */
const PNPM_INTEGRITY_REGEXP = /pnpm_version_integrity = ".*?"/;
/** RegExp that matches the TypeScript version assignment in MODULE.bazel. */
const TS_VERSION_REGEXP = /ts_version(?:_from)? = ".*?"/;
/** RegExp that matches the TypeScript integrity assignment in MODULE.bazel. */
const TS_INTEGRITY_REGEXP = /ts_integrity = ".*?"/;
/** RegExp that matches the Node.js version assignment in MODULE.bazel. */
const NODE_VERSION_REGEXP = /node_version = "(.*?)"/;
/** RegExp that matches the Node.js repositories assignment in MODULE.bazel. */
const NODE_REPOSITORIES_REGEXP = /node_repositories = \{[\s\S]*?\}/;
/** Fetches the integrity for a given package version from the npm registry. */
async function getNpmPackageIntegrity(pkg: string, version: string): Promise<string> {
const response = await fetch(`https://registry.npmjs.org/${pkg}/${version}`);
if (!response.ok) {
throw new Error(`Failed to request ${pkg}@${version}: ${response.statusText}`);
}
const {dist} = (await response.json()) as {dist: {integrity: string}};
return dist.integrity;
}
/** Fetches the repository information for a given Node.js version. */
async function getNodeJsRepositories(version: string): Promise<RepositoryInfo[]> {
const response = await fetch(`https://nodejs.org/dist/v${version}/SHASUMS256.txt`);
if (!response.ok) {
throw new Error(`Failed to get SHASUMS for Node.js v${version}: ${response.statusText}`);
}
const text = await response.text();
return text
.split('\n')
.filter(Boolean)
.map((line: string): RepositoryInfo | undefined => {
const [sha, filename] = line.trim().split(/\s+/);
if (!filename) return undefined;
const fileTypeSuffix = filename.replace(/^node-v[\d.]+-/, '');
const type = REPOSITORY_TYPES[fileTypeSuffix];
return type ? {filename, sha, type} : undefined;
})
.filter((repo): repo is RepositoryInfo => repo !== undefined);
}
/** Updates the version and integrity in the MODULE.bazel content for PNPM/TS. */
function updateVersionAndIntegrity(
content: string,
version: string,
integrity: string,
versionRegExp: RegExp,
integrityRegExp: RegExp,
versionKey: string,
integrityKey: string,
): string {
const newVal = `${versionKey} = "${version}"`;
const result = content.replace(versionRegExp, newVal);
return integrityRegExp.test(result)
? result.replace(integrityRegExp, `${integrityKey} = "${integrity}"`)
: result.replace(newVal, `${newVal},\n ${integrityKey} = "${integrity}"`);
}
/**
* Processes a `node.toolchain` block args string and updates it with
* the correct versions and repositories.
*/
async function processNodeToolchainArgs(
args: string,
nvmrcVersion: string | undefined,
): Promise<string> {
const useVersionFromNvm = args.includes('node_version_from_nvmrc');
const versionMatch = args.match(NODE_VERSION_REGEXP);
let effectiveVersion = versionMatch?.[1];
if (useVersionFromNvm) {
if (!nvmrcVersion) {
throw new Error('node_version_from_nvmrc used but .nvmrc not found');
}
effectiveVersion = nvmrcVersion;
} else if (effectiveVersion && effectiveVersion !== nvmrcVersion) {
args = args.replace(NODE_VERSION_REGEXP, `node_version = "${nvmrcVersion}"`);
effectiveVersion = nvmrcVersion;
}
if (!effectiveVersion) {
return args;
}
Log.info(`Resolving Node.js repositories for v${effectiveVersion}...`);
const repositories = await getNodeJsRepositories(effectiveVersion);
const lines = repositories.map(({filename, sha, type}) => {
const strippedFilename = filename.replace(/(\.tar)?\.[^.]+$/, '');
return ` "${effectiveVersion}-${type}": ("${filename}", "${strippedFilename}", "${sha}"),`;
});
const reposDict = `{\n${lines.join('\n')}\n }`;
if (NODE_REPOSITORIES_REGEXP.test(args)) {
return args.replace(NODE_REPOSITORIES_REGEXP, `node_repositories = ${reposDict}`);
}
const separator = args.trim().endsWith(',') ? '' : ',';
return `${args.trim()}${separator}\n node_repositories = ${reposDict}\n`;
}
/** Synchronizes the PNPM version and integrity in MODULE.bazel. */
export async function syncPnpm(content: string, version: string): Promise<string> {
if (!PNPM_VERSION_REGEXP.test(content)) {
return content;
}
Log.info(`Resolving integrity for pnpm@${version}...`);
const pnpmIntegrity = await getNpmPackageIntegrity('pnpm', version);
return updateVersionAndIntegrity(
content,
version,
pnpmIntegrity,
PNPM_VERSION_REGEXP,
PNPM_INTEGRITY_REGEXP,
'pnpm_version',
'pnpm_version_integrity',
);
}
/** Synchronizes the TypeScript version and integrity in MODULE.bazel. */
export async function syncTypeScript(content: string, version: string): Promise<string> {
if (!TS_VERSION_REGEXP.test(content)) {
return content;
}
Log.info(`Resolving integrity for typescript@${version}...`);
const tsIntegrity = await getNpmPackageIntegrity('typescript', version);
return updateVersionAndIntegrity(
content,
version,
tsIntegrity,
TS_VERSION_REGEXP,
TS_INTEGRITY_REGEXP,
'ts_version',
'ts_integrity',
);
}
/** Finds the index of the closing parenthesis for a balanced block. */
function findClosedParenIndex(content: string, startIndex: number): number {
let balance = 1;
for (let i = startIndex + 1; i < content.length; i++) {
if (content[i] === '(') {
balance++;
} else if (content[i] === ')') {
balance--;
}
if (balance === 0) {
return i;
}
}
return -1;
}
/** Synchronizes the Node.js toolchain versions and repositories in MODULE.bazel. */
export async function syncNodeJs(
content: string,
nvmrcVersion: string | undefined,
): Promise<string> {
const parts: string[] = [];
let lastIndex = 0;
let startIndex = 0;
while ((startIndex = content.indexOf('node.toolchain(', startIndex)) !== -1) {
const openParenIndex = startIndex + 'node.toolchain('.length - 1;
const endIndex = findClosedParenIndex(content, openParenIndex);
if (endIndex === -1) {
break;
}
parts.push(content.slice(lastIndex, startIndex));
const args = content.slice(openParenIndex + 1, endIndex);
const updatedArgs = await processNodeToolchainArgs(args, nvmrcVersion);
parts.push(`node.toolchain(${updatedArgs})`);
lastIndex = endIndex + 1;
startIndex = lastIndex;
}
parts.push(content.slice(lastIndex));
return parts.join('');
}