-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathupdate_pw.mjs
More file actions
executable file
·205 lines (174 loc) · 7.32 KB
/
Copy pathupdate_pw.mjs
File metadata and controls
executable file
·205 lines (174 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
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
const dirname = path.dirname(new URL(import.meta.url).pathname);
/**
* @param {string} folder
* @param {import('child_process').ExecSyncOptions} options
*/
const execSyncAndLog = (command, options) => {
console.log(`Running: ${command}`);
execSync(command, { stdio: 'inherit', ...options });
};
/**
* @param {string} folder
*/
async function updateDependencies(folder) {
const cwd = path.join(dirname, folder)
execSyncAndLog('npx -y npm-check-updates -u', { cwd });
execSyncAndLog('npm install', { cwd });
}
/**
* @param {string} packageName
* @param {string} file
* @returns {Promise<string>}
*/
async function getNpmFile(packageName, file) {
const response = await fetch(`https://unpkg.com/${packageName}/${file}`, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
},
});
if (!response.ok) {
throw new Error(`Could not download ${packageName}/${file}. Status: ${response.status}. Body: ${await response.text()}`);
}
return await response.text();
}
/**
* Removes import statements, reference directives, copyright headers, and export statements from the beginning of a type definition file.
* This prevents issues with relative imports that don't exist in the concatenated file.
*
* @param {string} content - The file content to process
* @returns {string} - The content with imports/references/exports/copyright headers stripped
*/
function stripFileHeader(content) {
const lines = content.split('\n');
let startIndex = 0;
let inMultiLineComment = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Check for multi-line comment start
if (line.startsWith('/**') || line.startsWith('/*')) {
inMultiLineComment = true;
startIndex = i + 1;
continue;
}
// Check for multi-line comment end
if (inMultiLineComment) {
if (line.endsWith('*/')) {
inMultiLineComment = false;
startIndex = i + 1;
}
continue;
}
// Skip single-line comments
if (line.startsWith('//')) {
startIndex = i + 1;
continue;
}
// Skip empty lines
if (line === '') {
startIndex = i + 1;
continue;
}
// Skip triple-slash reference directives
if (line.startsWith('///')) {
startIndex = i + 1;
continue;
}
// Skip import statements
if (line.startsWith('import ') || line.startsWith('import{')) {
startIndex = i + 1;
continue;
}
// Skip export statements at the top (but not export type/interface declarations)
if (line.startsWith('export ') && !line.match(/^export (type|interface|class|const|function|namespace|declare)/)) {
startIndex = i + 1;
continue;
}
// We've hit actual content, stop skipping
break;
}
return lines.slice(startIndex).join('\n');
}
async function updateFrontendTypes() {
const typesFile = 'frontend/src/components/Editor/types.txt';
let typesBuffer = '';
// Add Node.js global types
typesBuffer += stripFileHeader(await getNpmFile('@types/node@18', 'globals.d.ts'));
typesBuffer += '\n';
// Add playwright-core module
typesBuffer += 'declare module \'playwright-core\' {\n';
typesBuffer += await getNpmFile(`playwright-core`, 'types/protocol.d.ts');
typesBuffer += stripFileHeader(await getNpmFile(`playwright-core`, 'types/structs.d.ts'));
typesBuffer += '\n';
typesBuffer += stripFileHeader(await getNpmFile(`playwright-core`, 'types/types.d.ts'));
typesBuffer += '}\n';
// Add playwright module (re-exports playwright-core)
typesBuffer += 'declare module \'playwright\' {\n';
typesBuffer += ' export * from \'playwright-core\';\n';
typesBuffer += '}\n';
// Add @playwright/test module
typesBuffer += 'declare module \'@playwright/test\' {\n';
const testTypes = await getNpmFile('playwright', 'types/test.d.ts');
// Fix internal reference paths that won't exist in the concatenated file
const fixedTestTypes = testTypes.split('\n')
.map(line => line.replace('@playwright/test/types/expect-types', '@playwright/test-expect'))
.join('\n');
typesBuffer += fixedTestTypes;
typesBuffer += '}\n';
fs.writeFileSync(typesFile, typesBuffer);
}
/**
* @param {string} lang
* @returns {Promise<string>}
*/
async function getVersionForLanguageBinding(lang) {
switch (lang) {
case 'js':
const npmResponse = await fetch('https://registry.npmjs.org/playwright');
const npmData = await npmResponse.json();
return npmData['dist-tags'].latest;
case 'java':
// central.sonatype.com is the authoritative source; search.maven.org's
// solr index lags and reported a stale latestVersion (e.g. 1.52.0).
const mavenResponse = await fetch('https://central.sonatype.com/api/internal/browse/component/versions?sortField=normalizedVersion&sortDirection=desc&page=0&size=1&filter=namespace%3Acom.microsoft.playwright%2Cname%3Aplaywright', {
headers: { 'Accept': 'application/json' },
});
const mavenData = await mavenResponse.json();
return mavenData.components[0].version;
case 'python':
const pypiResponse = await fetch('https://pypi.org/pypi/playwright/json');
const pypiData = await pypiResponse.json();
return pypiData.info.version;
case 'csharp':
const nugetResponse = await fetch('https://api.nuget.org/v3-flatcontainer/microsoft.playwright/index.json');
const nugetData = await nugetResponse.json();
return nugetData.versions.pop();
default:
throw new Error(`Unknown language binding ${lang}`);
}
}
async function updateWorker(workerDir, version) {
const dockerFile = `./worker-${workerDir}/Dockerfile`;
const dockerFileContent = fs.readFileSync(dockerFile).toString();
const newDockerFileContent = dockerFileContent.replace(/ARG PLAYWRIGHT_VERSION=.*/, `ARG PLAYWRIGHT_VERSION=${version}`);
await fs.promises.writeFile(dockerFile, newDockerFileContent);
}
async function updateWorkers() {
await updateWorker('csharp', await getVersionForLanguageBinding('csharp'));
await updateWorker('java', await getVersionForLanguageBinding('java'));
await updateWorker('javascript', await getVersionForLanguageBinding('js'));
await updateWorker('python', await getVersionForLanguageBinding('python'));
}
async function updateMainReadMeBadge() {
const readMeFile = path.join(dirname, 'README.md');
const readMeContent = (await fs.promises.readFile(readMeFile)).toString();
const newReadMeContent = readMeContent.replace(/Playwright-\d+\.\d+\.\d+-blue\.svg/, `Playwright-${await getVersionForLanguageBinding('js')}-blue.svg`);
await fs.promises.writeFile(readMeFile, newReadMeContent);
}
await updateDependencies('frontend');
await updateDependencies('e2e');
await updateFrontendTypes();
await updateWorkers();
await updateMainReadMeBadge();