electron/script/gen-filenames.js

106 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const cp = require('child_process');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
2020-03-20 20:28:31 +00:00
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}`)
2020-03-20 20:28:31 +00:00
);
2020-05-30 09:56:54 +00:00
const typingFiles = fs.readdirSync(path.resolve(__dirname, '../typings')).map(child => `typings/${child}`);
const main = async () => {
2019-06-02 20:03:03 +00:00
const webpackTargets = [
{
2019-06-02 20:03:03 +00:00
name: 'sandbox_bundle_deps',
config: 'webpack.config.sandboxed_renderer.js'
},
{
2019-06-02 20:03:03 +00:00
name: 'isolated_bundle_deps',
config: 'webpack.config.isolated_renderer.js'
},
2019-06-02 20:03:03 +00:00
{
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: 'asar_bundle_deps',
config: 'webpack.config.asar.js'
}
2020-03-20 20:28:31 +00:00
];
2019-06-02 20:03:03 +00:00
await Promise.all(webpackTargets.map(async webpackTarget => {
2020-03-20 20:28:31 +00:00
const tmpDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-filenames-'));
const child = cp.spawn('node', [
2019-06-02 20:03:03 +00:00
'build/webpack/get-outputs.js',
`./${webpackTarget.config}`,
path.resolve(tmpDir, `${webpackTarget.name}.measure.js`)
], {
cwd: path.resolve(__dirname, '..')
2020-03-20 20:28:31 +00:00
});
let output = '';
child.stdout.on('data', chunk => {
2020-03-20 20:28:31 +00:00
output += chunk.toString();
});
child.stderr.on('data', chunk => console.error(chunk.toString()));
await new Promise((resolve, reject) => child.on('exit', (code) => {
if (code !== 0) {
2020-03-20 20:28:31 +00:00
console.error(output);
return reject(new Error(`Failed to list webpack dependencies for entry: ${webpackTarget.name}`));
}
2020-03-20 20:28:31 +00:00
resolve();
}));
2019-06-02 20:03:03 +00:00
webpackTarget.dependencies = JSON.parse(output)
// 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'))
2019-06-02 20:03:03 +00:00
// All webpack builds depend on the tsconfig and package json files
2020-05-30 09:56:54 +00:00
.concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json', ...typingFiles])
// Make the generated list easier to read
2020-03-20 20:28:31 +00:00
.sort();
await fs.remove(tmpDir);
}));
fs.writeFileSync(
gniPath,
`# THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT BY HAND
auto_filenames = {
api_docs = [
${allDocs.map(doc => ` "${doc}",`).join('\n')}
]
2019-06-02 20:03:03 +00:00
${webpackTargets.map(target => ` ${target.name} = [
${target.dependencies.map(dep => ` "${dep}",`).join('\n')}
]`).join('\n\n')}
}
2020-03-20 20:28:31 +00:00
`);
};
if (process.mainModule === module) {
main().catch((err) => {
2020-03-20 20:28:31 +00:00
console.error(err);
process.exit(1);
});
}