Skip to content

Commit f8ff516

Browse files
feat: rewrite in typescript
1 parent 0e7c3c6 commit f8ff516

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1501
-1243
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ third_party
99
.disable-auto-updates
1010
/*.yml
1111
generated.env.sh
12+
dist
13+
build-tools-spec*

.vscode/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"cSpell.words": [
3+
"gerrit",
4+
"osid",
5+
"REAPI",
6+
"SDKROOT",
7+
"SISO"
8+
]
9+
}

package.json

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
"engines": {
55
"node": ">= 18.0.0"
66
},
7+
"type": "module",
78
"main": "null",
89
"private": true,
910
"scripts": {
1011
"lint:markdown": "electron-markdownlint \"**/*.md\"",
11-
"lint:js": "prettier --check \"src/**/*.js\" \"tests/*.js\" \"src/e\"",
12+
"lint:js": "prettier --check \"src/**/*.{js,ts}\" \"tests/*.{js,ts}\"",
1213
"lint": "npm run lint:js && npm run lint:markdown",
13-
"prettier:write": "prettier --write \"src/**/*.js\" \"tests/*.js\" \"src/e\"",
14+
"prettier:write": "prettier --write \"src/**/*.{js,ts}\" \"tests/*.{js,ts}\"",
1415
"prepare": "husky",
16+
"pretest": "tsc",
1517
"test": "nyc --reporter=lcov --reporter=text-summary vitest run --reporter=verbose --exclude tests/bootstrap.spec.mjs",
16-
"test:all": "nyc --reporter=lcov --reporter=text-summary vitest run --reporter=verbose"
18+
"test:all": "nyc --reporter=lcov --reporter=text-summary vitest run --reporter=verbose",
19+
"postinstall": "tsc"
1720
},
1821
"repository": "https://github.com/electron/build-tools",
1922
"author": "Electron Authors",
@@ -22,35 +25,43 @@
2225
"@marshallofsound/chrome-cookies-secure": "^2.1.1",
2326
"@octokit/auth-oauth-device": "^3.1.1",
2427
"@octokit/rest": "^18.5.2",
25-
"ajv": "^8.11.0",
26-
"ajv-formats": "^2.1.1",
27-
"chalk": "^2.4.1",
28+
"@types/node": "^22.7.1",
29+
"@types/progress": "^2.0.7",
30+
"@types/semver": "^7.7.0",
31+
"@types/tar": "^6.1.13",
32+
"ajv": "^8.17.1",
33+
"ajv-formats": "^3.0.1",
34+
"chalk": "^5.4.1",
2835
"command-exists": "^1.2.8",
29-
"commander": "^9.0.0",
36+
"commander": "^9.5.0",
3037
"debug": "^4.3.1",
3138
"extract-zip": "^2.0.1",
3239
"inquirer": "^8.2.4",
3340
"node-gyp": "^10.0.1",
3441
"open": "^6.4.0",
35-
"path-key": "^3.1.0",
3642
"progress": "^2.0.3",
3743
"readline-sync": "^1.4.10",
3844
"semver": "^7.6.0",
3945
"tar": "^6.2.1",
46+
"typescript": "^5.8.3",
4047
"vscode-uri": "^3.0.7",
41-
"which": "^2.0.2",
4248
"yaml": "^2.4.5"
4349
},
4450
"devDependencies": {
4551
"@electron/lint-roller": "^1.13.0",
52+
"@tsconfig/node22": "^22.0.2",
53+
"@types/command-exists": "^1.2.3",
54+
"@types/inquirer": "^8.0.0",
55+
"@types/readline-sync": "^1.4.8",
4656
"husky": "^9.1.6",
57+
"json-schema-to-typescript": "^15.0.4",
4758
"lint-staged": "^15.2.10",
4859
"nyc": "^17.1.0",
4960
"prettier": "^3.3.3",
5061
"vitest": "^3.0.6"
5162
},
5263
"lint-staged": {
53-
"*.js": [
64+
"*.{js,ts}": [
5465
"prettier --write"
5566
],
5667
"e": [

src/download.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/download.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import fs from 'node:fs';
2+
import stream from 'node:stream';
3+
import { pipeline } from 'node:stream/promises';
4+
5+
import { fatal } from './utils/logging.js';
6+
import { progressStream } from './utils/download.js';
7+
8+
const write = fs.createWriteStream(process.argv[3]);
9+
10+
async function tryDownload(attemptsLeft = 3) {
11+
const response = await fetch(process.argv[2]);
12+
const total = parseInt(response.headers.get('content-length') || '1', 10);
13+
14+
let promise: Promise<void>;
15+
if (process.env.CI) {
16+
promise = pipeline(stream.Readable.fromWeb(response.body!), write);
17+
} else {
18+
const progress = progressStream(total, '[:bar] :mbRateMB/s :percent :etas');
19+
promise = pipeline(stream.Readable.fromWeb(response.body!), progress, write);
20+
}
21+
22+
await promise.catch((err) => {
23+
if (attemptsLeft === 0) {
24+
return fatal(err);
25+
}
26+
27+
console.log('Download failed, trying', attemptsLeft, 'more times');
28+
tryDownload(attemptsLeft - 1);
29+
});
30+
}
31+
32+
tryDownload();

src/e-auto-update.js renamed to src/e-auto-update.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env node
22

3-
const chalk = require('chalk');
4-
const cp = require('child_process');
5-
const fs = require('fs');
6-
const path = require('path');
7-
const program = require('commander');
8-
const semver = require('semver');
3+
import chalk from 'chalk';
4+
import cp from 'node:child_process';
5+
import fs from 'node:fs';
6+
import path from 'node:path';
7+
import { program } from 'commander';
8+
import semver from 'semver';
99

10-
const { color, fatal } = require('./utils/logging');
10+
import { color, fatal } from './utils/logging.js';
1111

1212
const BUILD_TOOLS_INSTALLER_MIN_VERSION = '1.1.0';
1313

14-
const markerFilePath = path.join(__dirname, '..', '.disable-auto-updates');
14+
const markerFilePath = path.join(import.meta.dirname, '..', '.disable-auto-updates');
1515

1616
program
1717
.description('Check for build-tools updates or enable/disable automatic updates')
@@ -27,7 +27,7 @@ program
2727
}
2828
console.log('Automatic updates enabled');
2929
} catch (e) {
30-
fatal(e);
30+
fatal(e as Error);
3131
}
3232
});
3333

@@ -39,7 +39,7 @@ program
3939
fs.closeSync(fs.openSync(markerFilePath, 'w'));
4040
console.log('Automatic updates disabled');
4141
} catch (e) {
42-
fatal(e);
42+
fatal(e as Error);
4343
}
4444
});
4545

@@ -72,7 +72,7 @@ function checkForUpdates() {
7272
);
7373

7474
try {
75-
packageJson = JSON.parse(fs.readFileSync(buildToolsInstallerPackage));
75+
packageJson = JSON.parse(fs.readFileSync(buildToolsInstallerPackage, 'utf-8'));
7676
} catch {
7777
continue;
7878
}
@@ -87,8 +87,8 @@ function checkForUpdates() {
8787
break;
8888
}
8989

90-
const execOpts = { cwd: path.resolve(__dirname, '..') };
91-
const git = (args) => cp.execSync(`git ${args}`, execOpts).toString('utf8').trim();
90+
const execOpts = { cwd: path.resolve(import.meta.dirname, '..') };
91+
const git = (args: string) => cp.execSync(`git ${args}`, execOpts).toString('utf8').trim();
9292

9393
const headCmd = 'rev-parse --verify HEAD';
9494
const headBefore = git(headCmd);
@@ -127,7 +127,7 @@ function checkForUpdates() {
127127
console.log('build-tools updated to latest version!');
128128
}
129129
} catch (e) {
130-
fatal(e);
130+
fatal(e as Error);
131131
}
132132
}
133133

src/e-backport.js renamed to src/e-backport.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env node
22

3-
const { Octokit } = require('@octokit/rest');
4-
const chalk = require('chalk').default;
5-
const program = require('commander');
6-
const inquirer = require('inquirer');
7-
const path = require('path');
3+
import { Octokit } from '@octokit/rest';
4+
import chalk from 'chalk';
5+
import { program } from 'commander';
6+
import inquirer from 'inquirer';
7+
import path from 'node:path';
88

9-
const evmConfig = require('./evm-config');
10-
const { spawnSync } = require('./utils/depot-tools');
11-
const { getGitHubAuthToken } = require('./utils/github-auth');
12-
const { fatal } = require('./utils/logging');
9+
import * as evmConfig from './evm-config.js';
10+
import { depotSpawnSync } from './utils/depot-tools.js';
11+
import { getGitHubAuthToken } from './utils/github-auth.js';
12+
import { fatal } from './utils/logging.js';
1313

1414
program
1515
.arguments('[pr]')
@@ -36,8 +36,8 @@ program
3636
}
3737

3838
const targetBranches = pr.labels
39-
.filter((label) => label.name.startsWith('needs-manual-bp/'))
40-
.map((label) => label.name.substring(16));
39+
.filter((label) => label.name?.startsWith('needs-manual-bp/'))
40+
.map((label) => label.name!.substring(16));
4141
if (targetBranches.length === 0) {
4242
fatal('The given pull request is not needing any manual backports yet');
4343
return;
@@ -56,24 +56,30 @@ program
5656
const gitOpts = {
5757
cwd: path.resolve(config.root, 'src', 'electron'),
5858
stdio: 'pipe',
59-
};
60-
const result = spawnSync(config, 'git', ['status', '--porcelain'], gitOpts);
59+
} as const;
60+
const result = depotSpawnSync(config, 'git', ['status', '--porcelain'], gitOpts);
6161
if (result.status !== 0 || result.stdout.toString().trim().length !== 0) {
6262
fatal(
6363
"Your current git working directory is not clean, we won't erase your local changes. Clean it up and try again",
6464
);
6565
return;
6666
}
6767

68-
spawnSync(config, 'git', ['checkout', targetBranch], gitOpts, 'Failed to checkout base branch');
69-
spawnSync(
68+
depotSpawnSync(
69+
config,
70+
'git',
71+
['checkout', targetBranch],
72+
gitOpts,
73+
'Failed to checkout base branch',
74+
);
75+
depotSpawnSync(
7076
config,
7177
'git',
7278
['pull', 'origin', targetBranch],
7379
gitOpts,
7480
'Failed to update base branch',
7581
);
76-
spawnSync(
82+
depotSpawnSync(
7783
config,
7884
'git',
7985
['fetch', 'origin', pr.base.ref],
@@ -82,22 +88,30 @@ program
8288
);
8389

8490
const manualBpBranch = `manual-bp/${user.login}/pr/${prNumber}/branch/${targetBranch}`;
85-
spawnSync(config, 'git', ['branch', '-D', manualBpBranch], gitOpts);
86-
spawnSync(
91+
depotSpawnSync(config, 'git', ['branch', '-D', manualBpBranch], gitOpts);
92+
depotSpawnSync(
8793
config,
8894
'git',
8995
['checkout', '-b', manualBpBranch],
9096
gitOpts,
9197
`Failed to checkout new branch "${manualBpBranch}"`,
9298
);
9399

94-
spawnSync(config, 'yarn', ['install'], gitOpts, `Failed to do "yarn install" on new branch`);
100+
depotSpawnSync(
101+
config,
102+
'yarn',
103+
['install'],
104+
gitOpts,
105+
`Failed to do "yarn install" on new branch`,
106+
);
95107

96-
const cherryPickResult = spawnSync(config, 'git', ['cherry-pick', pr.merge_commit_sha], {
108+
const cherryPickResult = depotSpawnSync(config, 'git', ['cherry-pick', pr.merge_commit_sha], {
97109
cwd: gitOpts.cwd,
98110
});
99111

100-
const pushCommand = chalk.yellow(!!config.remotes.electron.fork ? 'git push fork' : 'git push');
112+
const pushCommand = chalk.yellow(
113+
!!config.remotes?.electron.fork ? 'git push fork' : 'git push',
114+
);
101115
const cherryPickCommand = chalk.yellow('git cherry-pick --continue');
102116
const prCommand = chalk.yellow(`e pr --backport ${prNumber}`);
103117

0 commit comments

Comments
 (0)