2022-10-19 20:39:31 +00:00
|
|
|
const asar = require('@electron/asar');
|
2023-06-22 14:21:42 +00:00
|
|
|
const assert = require('node:assert');
|
2024-06-19 14:10:16 +00:00
|
|
|
const fs = require('node:fs');
|
2023-06-22 14:21:42 +00:00
|
|
|
const os = require('node:os');
|
|
|
|
const path = require('node:path');
|
2019-02-05 20:10:15 +00:00
|
|
|
|
|
|
|
const getArgGroup = (name) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const group = [];
|
|
|
|
let inGroup = false;
|
2019-02-05 20:10:15 +00:00
|
|
|
for (const arg of process.argv) {
|
|
|
|
// At the next flag we stop being in the current group
|
2020-03-20 20:28:31 +00:00
|
|
|
if (arg.startsWith('--')) inGroup = false;
|
2019-02-05 20:10:15 +00:00
|
|
|
// Push all args in the group
|
2020-03-20 20:28:31 +00:00
|
|
|
if (inGroup) group.push(arg);
|
2019-02-05 20:10:15 +00:00
|
|
|
// If we find the start flag, start pushing
|
2020-03-20 20:28:31 +00:00
|
|
|
if (arg === `--${name}`) inGroup = true;
|
2019-02-05 20:10:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return group;
|
|
|
|
};
|
2019-02-05 20:10:15 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const base = getArgGroup('base');
|
|
|
|
const files = getArgGroup('files');
|
|
|
|
const out = getArgGroup('out');
|
2019-02-05 20:10:15 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
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');
|
2019-02-05 20:10:15 +00:00
|
|
|
|
|
|
|
// Ensure all files are inside the base dir
|
|
|
|
for (const file of files) {
|
|
|
|
if (!file.startsWith(base[0])) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`Expected all files to be inside the base dir but "${file}" was not in "${base[0]}"`);
|
|
|
|
process.exit(1);
|
2019-02-05 20:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const tmpPath = fs.mkdtempSync(path.resolve(os.tmpdir(), 'electron-gn-asar-'));
|
2019-02-05 20:10:15 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Copy all files to a tmp dir to avoid including scrap files in the ASAR
|
|
|
|
for (const file of files) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const newLocation = path.resolve(tmpPath, path.relative(base[0], file));
|
2024-06-19 14:10:16 +00:00
|
|
|
fs.mkdirSync(path.dirname(newLocation), { recursive: true });
|
2020-03-20 20:28:31 +00:00
|
|
|
fs.writeFileSync(newLocation, fs.readFileSync(file));
|
2019-02-05 20:10:15 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('Unexpected error while generating ASAR', err);
|
2024-06-19 14:10:16 +00:00
|
|
|
fs.promises.rm(tmpPath, { force: true, recursive: true })
|
2019-03-26 01:34:03 +00:00
|
|
|
.then(() => process.exit(1))
|
2020-03-20 20:28:31 +00:00
|
|
|
.catch(() => process.exit(1));
|
|
|
|
return;
|
2019-02-05 20:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the ASAR archive
|
2019-02-26 02:25:00 +00:00
|
|
|
asar.createPackageWithOptions(tmpPath, out[0], {})
|
|
|
|
.catch(err => {
|
2019-03-26 01:34:03 +00:00
|
|
|
const exit = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('Unexpected error while generating ASAR', err);
|
|
|
|
process.exit(1);
|
|
|
|
};
|
2024-06-19 14:10:16 +00:00
|
|
|
fs.promises.rm(tmpPath, { force: true, recursive: true }).then(exit).catch(exit);
|
|
|
|
}).then(() => fs.promises.rm(tmpPath, { force: true, recursive: true }));
|