Skip to content

Commit 63ecedb

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
Migrate scripts that run against NodeJS to ESM
NodeJS v24 allows us to run ESM even with no type:module in package.json. We do get a warning but that is harmless. Bug: 411053158 Change-Id: I0fdbfa0e323ebfc53a0f8763fc279ca0a3446595 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7216014 Commit-Queue: Simon Zünd <[email protected]> Auto-Submit: Nikolay Vitkov <[email protected]> Reviewed-by: Simon Zünd <[email protected]>
1 parent ed67c95 commit 63ecedb

23 files changed

+498
-357
lines changed

front_end/Images/generate-css-vars.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright 2020 The Chromium Authors
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
const path = require('node:path');
4+
import * as path from 'node:path';
5+
6+
import {writeIfChanged} from '../../scripts/build/ninja/write-if-changed.js';
57

6-
const {writeIfChanged} = require('../../scripts/build/ninja/write-if-changed.js');
78
const [, , buildTimestamp, targetGenDir, targetName, ...imageSources] = process.argv;
89

910
/**
@@ -15,7 +16,7 @@ function generateCSSVariableDefinition(fileName) {
1516
// We have to remove the `src/` part from any SVG file names for the CSS variables, while
1617
// we still use the full URL for the meta.url import. That's because the rollup-plugin-import-meta-assets
1718
// rewrites the image URLs to the new location, but doesn't modify the generated CSS variable name
18-
fileName.replace('src\/', '').replace(path.extname(fileName), '')}', 'url(\\"' + new URL('./${
19+
fileName.replace('src/', '').replace(path.extname(fileName), '')}', 'url(\\"' + new URL('./${
1920
fileName}', import.meta.url).toString() + '\\")');`;
2021
}
2122

@@ -45,4 +46,7 @@ const tsconfigContent = `{
4546
}
4647
`;
4748

48-
writeIfChanged(path.join(targetGenDir, `${targetName}-tsconfig.json`), tsconfigContent);
49+
writeIfChanged(
50+
path.join(targetGenDir, `${targetName}-tsconfig.json`),
51+
tsconfigContent,
52+
);

front_end/core/i18n/collect-ui-strings.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
const glob = require('glob');
6-
const path = require('node:path');
7-
const yargs = require('yargs');
8-
const {hideBin} = require('yargs/helpers');
5+
import glob from 'glob';
6+
import * as path from 'node:path';
7+
import yargs from 'yargs';
8+
import {hideBin} from 'yargs/helpers';
99

10-
const {writeIfChanged} = require('../../../scripts/build/ninja/write-if-changed.js');
11-
const {bakePlaceholders} = require('../../../third_party/i18n/bake-ctc-to-lhl.js');
12-
const {collectAllStringsInDir, createPsuedoLocaleStrings, IGNORED_PATH_COMPONENTS} =
13-
require('../../../third_party/i18n/collect-strings.js');
10+
import {writeIfChanged} from '../../../scripts/build/ninja/write-if-changed.js';
11+
import {bakePlaceholders} from '../../../third_party/i18n/bake-ctc-to-lhl.js';
12+
import {
13+
collectAllStringsInDir,
14+
createPsuedoLocaleStrings,
15+
IGNORED_PATH_COMPONENTS,
16+
} from '../../../third_party/i18n/collect-strings.js';
1417

1518
/** @typedef {import('../../../third_party/i18n/bake-ctc-to-lhl.js').CtcMessage} CtcMessage */
1619

@@ -39,7 +42,9 @@ function convertCtcToLhLAndSave(outputDirectory, locale, strings) {
3942

4043
/** @type {Record<string, CtcMessage>} */
4144
const sortedCtcStrings = {};
42-
const sortedEntries = Object.entries(strings).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
45+
const sortedEntries = Object.entries(strings).sort(
46+
([keyA], [keyB]) => keyA.localeCompare(keyB),
47+
);
4348
for (const [key, defn] of sortedEntries) {
4449
sortedCtcStrings[key] = defn;
4550
}
@@ -73,7 +78,11 @@ for (const directory of inputDirectories) {
7378
const outputDirectory = yargsObject['output-directory'];
7479
convertCtcToLhLAndSave(outputDirectory, 'en-US', collectedStrings);
7580
if (yargsObject['include-en-xl']) {
76-
convertCtcToLhLAndSave(outputDirectory, 'en-XL', createPsuedoLocaleStrings(collectedStrings));
81+
convertCtcToLhLAndSave(
82+
outputDirectory,
83+
'en-XL',
84+
createPsuedoLocaleStrings(collectedStrings),
85+
);
7786
}
7887

7988
// Write the depfile. This is necessary to properly rebuild en-US.json/en-XL.json

front_end/core/i18n/generate-locales-js.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
const path = require('node:path');
6-
const yargs = require('yargs');
7-
const {hideBin} = require('yargs/helpers');
5+
import * as path from 'node:path';
6+
import yargs from 'yargs';
7+
import {hideBin} from 'yargs/helpers';
88

9-
const {writeIfChanged} = require('../../../scripts/build/ninja/write-if-changed.js');
9+
import {writeIfChanged} from '../../../scripts/build/ninja/write-if-changed.js';
1010

1111
const yargsObject = yargs(hideBin(process.argv))
1212
.option('target-gen-dir', {

front_end/panels/timeline/enable-easter-egg.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
const path = require('node:path');
5+
import * as path from 'node:path';
6+
7+
import {writeIfChanged} from '../../../scripts/build/ninja/write-if-changed.js';
68

7-
const {writeIfChanged} = require('../../../scripts/build/ninja/write-if-changed.js');
89
const [, , targetGenDir] = process.argv;
910

1011
let value = 'false';
@@ -13,4 +14,7 @@ if (process.argv.includes('--should-enable')) {
1314
value = 'true';
1415
}
1516

16-
writeIfChanged(path.join(targetGenDir, 'EasterEgg.js'), `export const SHOULD_SHOW_EASTER_EGG = ${value};`);
17+
writeIfChanged(
18+
path.join(targetGenDir, 'EasterEgg.js'),
19+
`export const SHOULD_SHOW_EASTER_EGG = ${value};`,
20+
);

scripts/check_esbuild_versions.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
// Copyright 2022 The Chromium Authors
44
// Use of this source code is governed by a BSD-style license that can be
55
// found in the LICENSE file.
6-
'use strict';
76

8-
const fs = require('node:fs');
9-
const path = require('node:path');
7+
import fs from 'node:fs';
8+
import path from 'node:path';
109

11-
const {
12-
devtoolsRootPath,
13-
} = require('./devtools_paths.js');
10+
import {devtoolsRootPath} from './devtools_paths.js';
1411

1512
function bail(message) {
1613
console.error(message);
@@ -20,7 +17,9 @@ function bail(message) {
2017
function findVersionFromDepsFile() {
2118
const filePath = path.join(devtoolsRootPath(), 'DEPS');
2219
const contents = fs.readFileSync(filePath, 'utf8').split('\n');
23-
const esbuildPackageLine = contents.findIndex(line => line.match(/infra\/3pp\/tools\/esbuild/));
20+
const esbuildPackageLine = contents.findIndex(
21+
line => line.match(/infra\/3pp\/tools\/esbuild/),
22+
);
2423
if (esbuildPackageLine === -1) {
2524
bail('Could not find ESBuild within DEPS file.');
2625
}

scripts/check_experiments.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright 2020 The Chromium Authors
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
'use strict';
54

6-
const espree = require('@typescript-eslint/parser');
7-
const fs = require('node:fs');
8-
const path = require('node:path');
5+
import espree from '@typescript-eslint/parser';
6+
import fs from 'node:fs';
7+
import path from 'node:path';
98

109
const parseOptions = {
1110
ecmaVersion: 'latest',
@@ -253,7 +252,7 @@ function compareExperimentLists(mainImplList, userMetricsList) {
253252

254253
function main() {
255254
const mainImplPath = path.resolve(
256-
__dirname,
255+
import.meta.dirname,
257256
'..',
258257
'front_end',
259258
'entrypoints',
@@ -263,7 +262,7 @@ function main() {
263262
const mainImplFile = fs.readFileSync(mainImplPath, 'utf-8');
264263

265264
const userMetricsPath = path.resolve(
266-
__dirname,
265+
import.meta.dirname,
267266
'..',
268267
'front_end',
269268
'core',
@@ -273,7 +272,7 @@ function main() {
273272
const userMetricsFile = fs.readFileSync(userMetricsPath, 'utf-8');
274273

275274
const runtimePath = path.resolve(
276-
__dirname,
275+
import.meta.dirname,
277276
'..',
278277
'front_end',
279278
'core',

scripts/check_external_links.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
'use strict';
6-
7-
const fs = require('node:fs');
8-
const https = require('node:https');
9-
const path = require('node:path');
10-
const ts = require('typescript');
5+
import fs from 'node:fs';
6+
import https from 'node:https';
7+
import path from 'node:path';
8+
import ts from 'typescript';
119

1210
const readDirAsync = fs.promises.readdir;
1311
const readFileAsync = fs.promises.readFile;
@@ -18,13 +16,9 @@ const ORIGIN_PATTERNS_TO_CHECK = [
1816
new RegExp('^https://developer[s]?.chrome.com'),
1917
];
2018

21-
const DIRECTORIES_TO_CHECK = [
22-
'front_end',
23-
];
19+
const DIRECTORIES_TO_CHECK = ['front_end'];
2420

25-
const EXCLUDE_DIRECTORIES = [
26-
'front_end/third_party',
27-
];
21+
const EXCLUDE_DIRECTORIES = ['front_end/third_party'];
2822

2923
const REQUEST_TIMEOUT = 5000;
3024

@@ -34,25 +28,29 @@ const REDIRECTS_CONSIDERED_ERROR = new Set([
3428
/* Permament redirect */ 308,
3529
]);
3630

37-
const ROOT_REPOSITORY_PATH = path.resolve(__dirname, '..');
38-
const DIRECTORIES_TO_CHECK_PATHS = DIRECTORIES_TO_CHECK.map(directory => path.resolve(ROOT_REPOSITORY_PATH, directory));
31+
const ROOT_REPOSITORY_PATH = path.resolve(import.meta.dirname, '..');
32+
const DIRECTORIES_TO_CHECK_PATHS = DIRECTORIES_TO_CHECK.map(
33+
directory => path.resolve(ROOT_REPOSITORY_PATH, directory),
34+
);
3935

4036
async function findAllSourceFiles(directory) {
4137
if (EXCLUDE_DIRECTORIES.includes(path.relative(ROOT_REPOSITORY_PATH, directory))) {
4238
return [];
4339
}
4440

4541
const dirEntries = await readDirAsync(directory, {withFileTypes: true});
46-
const files = await Promise.all(dirEntries.map(dirEntry => {
47-
const resolvedPath = path.resolve(directory, dirEntry.name);
48-
if (dirEntry.isDirectory()) {
49-
return findAllSourceFiles(resolvedPath);
50-
}
51-
if (dirEntry.isFile() && /\.(js|ts)$/.test(dirEntry.name)) {
52-
return resolvedPath;
53-
}
54-
return []; // Let Array#flat filter out files we are not interested in.
55-
}));
42+
const files = await Promise.all(
43+
dirEntries.map(dirEntry => {
44+
const resolvedPath = path.resolve(directory, dirEntry.name);
45+
if (dirEntry.isDirectory()) {
46+
return findAllSourceFiles(resolvedPath);
47+
}
48+
if (dirEntry.isFile() && /\.(js|ts)$/.test(dirEntry.name)) {
49+
return resolvedPath;
50+
}
51+
return []; // Let Array#flat filter out files we are not interested in.
52+
}),
53+
);
5654
return files.flat();
5755
}
5856

@@ -63,7 +61,9 @@ function collectUrlsToCheck(node) {
6361
const currentNode = nodesToVisit.shift();
6462
if (currentNode.kind === ts.SyntaxKind.StringLiteral ||
6563
currentNode.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
66-
const checkUrl = ORIGIN_PATTERNS_TO_CHECK.some(originPattern => originPattern.test(currentNode.text));
64+
const checkUrl = ORIGIN_PATTERNS_TO_CHECK.some(
65+
originPattern => originPattern.test(currentNode.text),
66+
);
6767
if (checkUrl) {
6868
urlsToCheck.push(currentNode.text);
6969
}
@@ -75,25 +75,33 @@ function collectUrlsToCheck(node) {
7575

7676
async function collectUrlsToCheckFromFile(filePath) {
7777
const content = await readFileAsync(filePath, 'utf8');
78-
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.ESNext, true);
78+
const sourceFile = ts.createSourceFile(
79+
filePath,
80+
content,
81+
ts.ScriptTarget.ESNext,
82+
true,
83+
);
7984
return collectUrlsToCheck(sourceFile);
8085
}
8186

8287
async function checkUrls(urls) {
8388
// clang-format off
84-
const requestPromises = urls.map(url => new Promise(resolve => {
85-
const request = https.request(url, {method: 'HEAD'}, response => {
86-
resolve({url, statusCode: response.statusCode});
87-
});
88-
89-
request.on('error', err => {
90-
resolve({url, error: err});
91-
});
92-
request.setTimeout(REQUEST_TIMEOUT, _ => {
93-
resolve({url, error: `Timed out after ${REQUEST_TIMEOUT}`});
94-
});
95-
request.end();
96-
}));
89+
const requestPromises = urls.map(
90+
url =>
91+
new Promise(resolve => {
92+
const request = https.request(url, { method: 'HEAD' }, response => {
93+
resolve({ url, statusCode: response.statusCode });
94+
});
95+
96+
request.on('error', err => {
97+
resolve({ url, error: err });
98+
});
99+
request.setTimeout(REQUEST_TIMEOUT, _ => {
100+
resolve({ url, error: `Timed out after ${REQUEST_TIMEOUT}` });
101+
});
102+
request.end();
103+
}),
104+
);
97105
// clang-format on
98106

99107
return Promise.all(requestPromises);
@@ -122,9 +130,13 @@ function printSelectedRequestResults(requestResults) {
122130
if (requestResult.error) {
123131
console.error(`[Failure] ${requestResult.error} - ${requestResult.url}`);
124132
} else if (isErrorStatusCode(requestResult.statusCode)) {
125-
console.error(`[Failure] Status Code: ${requestResult.statusCode} - ${requestResult.url}`);
133+
console.error(
134+
`[Failure] Status Code: ${requestResult.statusCode} - ${requestResult.url}`,
135+
);
126136
} else {
127-
console.log(`Status Code: ${requestResult.statusCode} - ${requestResult.url}`);
137+
console.log(
138+
`Status Code: ${requestResult.statusCode} - ${requestResult.url}`,
139+
);
128140
}
129141
}
130142
}

0 commit comments

Comments
 (0)