electron/script/gn-asar.js
trop[bot] e480e29cfc
build: remove fs-extra devdep (#42708)
* build: remove fs-extra dependency from script/gen-filenames.ts

* build: remove fs-extra dependency from script/spec-runner.js

* build: remove fs-extra dependency from script/gn-asar.js

* build: remove fs-extra dependency from spec/api-autoupdater-darwin-spec.ts

* build: remove fs-extra dependency from spec/api-safe-storage-spec.ts

* build: remove fs-extra dependency from spec/lib/codesign-helpers.ts

* build: remove fs-extra dependency from spec/api-app-spec.ts

* build: remove fs-extra dependency from spec/esm-spec.ts

* build: remove fs-extra dependency from spec/lib/fs-helpers.ts

* build: remove fs-extra dependency from spec/lib/api-shell-spec.ts

* build: remove fs-extra dependency from spec/api-context-bridge-spec.ts

* build: remove fs-extra dependency from spec/asar-integrity-spec.ts

* build: remove fs-extra dependency from spec/node-spec.ts

* build: remove fs-extra devdiv

* fixup! build: remove fs-extra dependency from spec/api-context-bridge-spec.ts

* fix: use force: true when removing directories

* chore: reduce diffs to main

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2024-06-28 12:13:46 -05:00

63 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 }));