Files
electron/script/gen-filenames.ts
Samuel Attard 2c94aac330 build: add oxfmt for JS/TS formatting and import sorting (#50692)
* build: add oxfmt for code formatting and import sorting

Adds oxfmt as a devDependency alongside oxlint and wires it into the
lint pipeline. The .oxfmtrc.json config matches Electron's current JS
style (single quotes, semicolons, 2-space indent, trailing commas off,
printWidth 100) and configures sortImports with custom groups that
mirror the import/order pathGroups previously enforced by ESLint:
@electron/internal, @electron/*, and {electron,electron/**} each get
their own ordered group ahead of external modules.

- `yarn lint:fmt` runs `oxfmt --check` over JS/TS sources and is
  chained into `yarn lint` so CI enforces it automatically.
- `yarn format` runs `oxfmt --write` for local fix-up.
- lint-staged invokes `oxfmt --write` on staged .js/.ts/.mjs/.cjs
  files before oxlint, so formatting is applied at commit time.

The next commit applies the formatter to the existing codebase so the
check actually passes.

* chore: apply oxfmt formatting to JS and TS sources

Runs `yarn format` across lib/, spec/, script/, build/, default_app/,
and npm/ to bring the codebase in line with the .oxfmtrc.json settings
added in the previous commit. This is a pure formatting pass: import
statements are sorted into the groups defined by the config, method
chains longer than printWidth are broken, single-quoted strings
containing apostrophes are switched to double quotes, and a handful of
single-statement `if` bodies are re-wrapped and get braces added by
`oxlint --fix` to satisfy the `curly: multi-line` rule.

No behavior changes.
2026-04-12 02:03:04 -07:00

137 lines
3.8 KiB
TypeScript

import * as cp from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
const rootPath = path.resolve(__dirname, '..');
const gniPath = path.resolve(__dirname, '../filenames.auto.gni');
const allDocs = fs
.readdirSync(path.resolve(__dirname, '../docs/api'))
.map((doc) => `docs/api/${doc}`)
.concat(fs.readdirSync(path.resolve(__dirname, '../docs/api/structures')).map((doc) => `docs/api/structures/${doc}`));
const typingFiles = fs.readdirSync(path.resolve(__dirname, '../typings')).map((child) => `typings/${child}`);
const main = async () => {
const webpackTargets = [
{
name: 'sandbox_bundle_deps',
config: 'webpack.config.sandboxed_renderer.js'
},
{
name: 'isolated_bundle_deps',
config: 'webpack.config.isolated_renderer.js'
},
{
name: 'browser_bundle_deps',
config: 'webpack.config.browser.js'
},
{
name: 'renderer_bundle_deps',
config: 'webpack.config.renderer.js'
},
{
name: 'worker_bundle_deps',
config: 'webpack.config.worker.js'
},
{
name: 'node_bundle_deps',
config: 'webpack.config.node.js'
},
{
name: 'utility_bundle_deps',
config: 'webpack.config.utility.js'
},
{
name: 'preload_realm_bundle_deps',
config: 'webpack.config.preload_realm.js'
}
];
const webpackTargetsWithDeps = await Promise.all(
webpackTargets.map(async (webpackTarget) => {
const tmpDir = await fs.promises.mkdtemp(path.resolve(os.tmpdir(), 'electron-filenames-'));
const child = cp.spawn(
'node',
[
'./node_modules/webpack-cli/bin/cli.js',
'--config',
`./build/webpack/${webpackTarget.config}`,
'--stats',
'errors-only',
'--output-path',
tmpDir,
'--output-filename',
`${webpackTarget.name}.measure.js`,
'--env',
'PRINT_WEBPACK_GRAPH'
],
{
cwd: path.resolve(__dirname, '..')
}
);
let output = '';
child.stdout.on('data', (chunk) => {
output += chunk.toString();
});
child.stderr.on('data', (chunk) => console.error(chunk.toString()));
await new Promise<void>((resolve, reject) =>
child.on('exit', (code) => {
if (code !== 0) {
console.error(output);
return reject(new Error(`Failed to list webpack dependencies for entry: ${webpackTarget.name}`));
}
resolve();
})
);
const webpackTargetWithDeps = {
...webpackTarget,
dependencies: (JSON.parse(output) as string[])
// Remove whitespace
.map((line) => line.trim())
// Get the relative path
.map((line) => path.relative(rootPath, line).replace(/\\/g, '/'))
// Only care about files in //electron
.filter((line) => !line.startsWith('..'))
// Only care about our own files
.filter((line) => !line.startsWith('node_modules'))
// All webpack builds depend on the tsconfig and package json files
.concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json', ...typingFiles])
// Make the generated list easier to read
.sort()
};
await fs.promises.rm(tmpDir, { force: true, recursive: true });
return webpackTargetWithDeps;
})
);
fs.writeFileSync(
gniPath,
`# THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT BY HAND
auto_filenames = {
api_docs = [
${allDocs.map((doc) => ` "${doc}",`).join('\n')}
]
${webpackTargetsWithDeps
.map(
(target) => ` ${target.name} = [
${target.dependencies.map((dep) => ` "${dep}",`).join('\n')}
]`
)
.join('\n\n')}
}
`
);
};
if (require.main === module) {
main().catch((err) => {
console.error(err);
process.exit(1);
});
}