bruno/scripts/build-electron.js

116 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-10-09 14:32:02 +02:00
const os = require('os');
const fs = require('fs-extra');
const util = require('util');
2023-10-17 10:45:40 +02:00
const spawn = util.promisify(require('child_process').spawn);
2023-10-09 14:32:02 +02:00
async function deleteFileIfExists(filePath) {
try {
const exists = await fs.pathExists(filePath);
if (exists) {
await fs.remove(filePath);
console.log(`${filePath} has been successfully deleted.`);
} else {
console.log(`${filePath} does not exist.`);
}
} catch (err) {
console.error(`Error while checking the existence of ${filePath}: ${err}`);
}
}
async function copyFolderIfExists(srcPath, destPath) {
try {
const exists = await fs.pathExists(srcPath);
if (exists) {
await fs.copy(srcPath, destPath);
console.log(`${srcPath} has been successfully copied.`);
} else {
console.log(`${srcPath} was not copied as it does not exist.`);
}
} catch (err) {
console.error(`Error while checking the existence of ${srcPath}: ${err}`);
}
}
async function removeSourceMapFiles(directory) {
try {
const files = await fs.readdir(directory);
for (const file of files) {
if (file.endsWith('.map')) {
const filePath = path.join(directory, file);
await fs.remove(filePath);
console.log(`${filePath} has been successfully deleted.`);
}
}
} catch (error) {
console.error(`Error while deleting .map files: ${error}`);
}
}
2023-10-17 10:45:40 +02:00
async function execCommandWithOutput(command) {
return new Promise(async (resolve, reject) => {
const childProcess = await spawn(command, {
stdio: 'inherit',
shell: true
});
childProcess.on('error', (error) => {
reject(error);
});
childProcess.on('exit', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Command exited with code ${code}.`));
}
});
});
}
2023-10-09 14:32:02 +02:00
async function main() {
try {
// Remove out directory
await deleteFileIfExists('packages/bruno-electron/out');
// Remove web directory
await deleteFileIfExists('packages/bruno-electron/web');
// Create a new web directory
await fs.ensureDir('packages/bruno-electron/web');
console.log('The directory has been created successfully!');
// Copy build
await copyFolderIfExists('packages/bruno-app/out', 'packages/bruno-electron/web');
// Change paths in next
const files = await fs.readdir('packages/bruno-electron/web');
for (const file of files) {
if (file.endsWith('.html')) {
let content = await fs.readFile(`packages/bruno-electron/web/${file}`, 'utf8');
2023-10-17 10:46:00 +02:00
content = content.replace(/\/_next\//g, '_next/');
2023-10-09 14:32:02 +02:00
await fs.writeFile(`packages/bruno-electron/web/${file}`, content);
}
}
// Remove sourcemaps
2023-10-17 08:41:56 +02:00
await removeSourceMapFiles('packages/bruno-electron/web');
2023-10-09 14:32:02 +02:00
// Run npm dist command
console.log('Building the Electron distribution');
// Determine the OS and set the appropriate argument
let osArg;
if (os.platform() === 'win32') {
osArg = 'win';
} else if (os.platform() === 'darwin') {
osArg = 'mac';
} else {
osArg = 'linux';
}
2023-10-17 10:45:40 +02:00
await execCommandWithOutput(`npm run dist:${osArg} --workspace=packages/bruno-electron`);
2023-10-09 14:32:02 +02:00
} catch (error) {
console.error('An error occurred:', error);
}
}
main();