Files
electron/script/gn-asar.js
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

68 lines
2.1 KiB
JavaScript

const asar = require('@electron/asar');
const assert = require('node:assert');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const getArgGroup = (name) => {
const group = [];
let inGroup = false;
for (const arg of process.argv) {
// At the next flag we stop being in the current group
if (arg.startsWith('--')) inGroup = false;
// Push all args in the group
if (inGroup) group.push(arg);
// If we find the start flag, start pushing
if (arg === `--${name}`) inGroup = true;
}
return group;
};
const base = getArgGroup('base');
const files = getArgGroup('files');
const out = getArgGroup('out');
assert(base.length === 1, 'should have a single base dir');
assert(files.length >= 1, 'should have at least one input file');
assert(out.length === 1, 'should have a single out path');
// Ensure all files are inside the base dir
for (const file of files) {
if (!file.startsWith(base[0])) {
console.error(`Expected all files to be inside the base dir but "${file}" was not in "${base[0]}"`);
process.exit(1);
}
}
const tmpPath = fs.mkdtempSync(path.resolve(os.tmpdir(), 'electron-gn-asar-'));
try {
// Copy all files to a tmp dir to avoid including scrap files in the ASAR
for (const file of files) {
const newLocation = path.resolve(tmpPath, path.relative(base[0], file));
fs.mkdirSync(path.dirname(newLocation), { recursive: true });
fs.writeFileSync(newLocation, fs.readFileSync(file));
}
} catch (err) {
console.error('Unexpected error while generating ASAR', err);
fs.promises
.rm(tmpPath, { force: true, recursive: true })
.then(() => process.exit(1))
.catch(() => process.exit(1));
return;
}
// Create the ASAR archive
asar
.createPackageWithOptions(tmpPath, out[0], {})
.catch((err) => {
const exit = () => {
console.error('Unexpected error while generating ASAR', err);
process.exit(1);
};
fs.promises.rm(tmpPath, { force: true, recursive: true }).then(exit).catch(exit);
})
.then(() => fs.promises.rm(tmpPath, { force: true, recursive: true }));