From 1affe0056b1729c9c9d7eb2dd3c7c23a2c1df224 Mon Sep 17 00:00:00 2001 From: Sebastien Dionne Date: Mon, 9 Oct 2023 08:32:02 -0400 Subject: [PATCH] Add build support for different OS --- package.json | 5 +- packages/bruno-electron/package.json | 3 + scripts/build-electron.js | 98 ++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 scripts/build-electron.js diff --git a/package.json b/package.json index 9aaaf23f2..bba854916 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "jest": "^29.2.0", "pretty-quick": "^3.1.3", "randomstring": "^1.2.2", - "ts-jest": "^29.0.5" + "ts-jest": "^29.0.5", + "fs-extra": "^11.1.1" }, "scripts": { "dev:web": "npm run dev --workspace=packages/bruno-app", @@ -31,7 +32,7 @@ "dev:electron": "npm run dev --workspace=packages/bruno-electron", "build:bruno-query": "npm run build --workspace=packages/bruno-query", "build:graphql-docs": "npm run build --workspace=packages/bruno-graphql-docs", - "build:electron": "./scripts/build-electron.sh", + "build:electron": "node ./scripts/build-electron.js", "test:e2e": "npx playwright test", "test:report": "npx playwright show-report", "prepare": "husky install" diff --git a/packages/bruno-electron/package.json b/packages/bruno-electron/package.json index fdbd339c9..e26e518d1 100644 --- a/packages/bruno-electron/package.json +++ b/packages/bruno-electron/package.json @@ -10,6 +10,9 @@ "clean": "rimraf dist", "dev": "electron .", "dist": "electron-builder --mac --config electron-builder-config.js", + "dist-mac": "electron-builder --mac --config electron-builder-config.js", + "dist-win": "electron-builder --win --config electron-builder-config.js", + "dist-linux": "electron-builder --linux --config electron-builder-config.js", "pack": "electron-builder --dir", "test": "jest" }, diff --git a/scripts/build-electron.js b/scripts/build-electron.js new file mode 100644 index 000000000..363fdf4d2 --- /dev/null +++ b/scripts/build-electron.js @@ -0,0 +1,98 @@ +const os = require('os'); +const fs = require('fs-extra'); +const util = require('util'); +const exec = util.promisify(require('child_process').exec); + + +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}`); + } +} + +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'); + content = content.replace(/\/_next\//g, '/_next/'); + await fs.writeFile(`packages/bruno-electron/web/${file}`, content); + } + } + + // Remove sourcemaps + await removeSourceMapFiles('packages/bruno-electron/web') + + // 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'; + } + + await exec(`npm run dist-${osArg} --workspace=packages/bruno-electron`); + + } catch (error) { + console.error('An error occurred:', error); + } +} + +main();