2023-06-20 13:59:25 +00:00
|
|
|
import vue from '@vitejs/plugin-vue';
|
2023-06-21 06:30:22 +00:00
|
|
|
import builder from 'electron-builder';
|
2023-06-20 13:59:25 +00:00
|
|
|
import esbuild from 'esbuild';
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import path from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import * as vite from 'vite';
|
|
|
|
import { getMainProcessCommonConfig } from './helpers.mjs';
|
2023-06-21 06:30:22 +00:00
|
|
|
import yargs from 'yargs';
|
|
|
|
import { hideBin } from 'yargs/helpers';
|
2023-06-20 13:59:25 +00:00
|
|
|
|
|
|
|
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
const root = path.join(dirname, '..', '..');
|
2023-06-21 06:30:22 +00:00
|
|
|
const buildDirPath = path.join(root, 'dist_electron', 'build');
|
|
|
|
const packageDirPath = path.join(root, 'dist_electron', 'bundled');
|
|
|
|
const mainFileName = 'main.js';
|
|
|
|
const commonConfig = getMainProcessCommonConfig(root);
|
2023-06-20 13:59:25 +00:00
|
|
|
|
2023-06-21 06:30:22 +00:00
|
|
|
updatePaths();
|
2023-06-20 13:59:25 +00:00
|
|
|
await buildMainProcessSource();
|
|
|
|
await buildRendererProcessSource();
|
2023-06-21 06:30:22 +00:00
|
|
|
copyPackageJson();
|
|
|
|
await packageApp();
|
|
|
|
|
|
|
|
function updatePaths() {
|
|
|
|
fs.removeSync(buildDirPath);
|
|
|
|
fs.mkdirSync(buildDirPath);
|
|
|
|
fs.removeSync(packageDirPath);
|
|
|
|
fs.mkdirSync(packageDirPath);
|
|
|
|
fs.ensureDirSync(path.join(buildDirPath, 'node_modules'));
|
|
|
|
}
|
2023-06-20 13:59:25 +00:00
|
|
|
|
|
|
|
async function buildMainProcessSource() {
|
|
|
|
const result = await esbuild.build({
|
|
|
|
...commonConfig,
|
2023-06-21 06:30:22 +00:00
|
|
|
outfile: path.join(buildDirPath, mainFileName),
|
2023-06-20 13:59:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result.errors.length) {
|
|
|
|
console.error('app build failed due to main process source build');
|
|
|
|
result.errors.forEach((err) => console.error(err));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildRendererProcessSource() {
|
|
|
|
const base = 'app://';
|
2023-06-21 06:30:22 +00:00
|
|
|
const outDir = path.join(buildDirPath, 'src');
|
2023-06-20 13:59:25 +00:00
|
|
|
await vite.build({
|
|
|
|
base: `/${base}`,
|
|
|
|
root: path.join(root, 'src'),
|
|
|
|
build: { outDir },
|
|
|
|
plugins: [vue()],
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
vue: 'vue/dist/vue.esm-bundler.js',
|
|
|
|
fyo: path.join(root, 'fyo'),
|
|
|
|
src: path.join(root, 'src'),
|
|
|
|
schemas: path.join(root, 'schemas'),
|
|
|
|
backend: path.join(root, 'backend'),
|
|
|
|
models: path.join(root, 'models'),
|
|
|
|
utils: path.join(root, 'utils'),
|
|
|
|
regional: path.join(root, 'regional'),
|
|
|
|
reports: path.join(root, 'reports'),
|
|
|
|
dummy: path.join(root, 'dummy'),
|
|
|
|
fixtures: path.join(root, 'fixtures'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
removeBaseLeadingSlash(outDir, base);
|
|
|
|
}
|
|
|
|
|
2023-06-21 06:30:22 +00:00
|
|
|
/**
|
|
|
|
* Copies the package.json file to the build folder with the
|
|
|
|
* following changes:
|
|
|
|
* - Irrelevant fields are removed.
|
|
|
|
* - Non-external deps (those that are bundled) and devDeps are removed.
|
|
|
|
* - Main file is updated to the bundled main process JS file.
|
|
|
|
*/
|
|
|
|
function copyPackageJson() {
|
|
|
|
const packageJsonText = fs.readFileSync(path.join(root, 'package.json'), {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
});
|
|
|
|
|
|
|
|
const packageJson = JSON.parse(packageJsonText);
|
|
|
|
const keys = [
|
|
|
|
'name',
|
|
|
|
'version',
|
|
|
|
'description',
|
|
|
|
'author',
|
|
|
|
'homepage',
|
|
|
|
'repository',
|
|
|
|
'license',
|
|
|
|
];
|
|
|
|
const modifiedPackageJson = {};
|
|
|
|
for (const key of keys) {
|
|
|
|
modifiedPackageJson[key] = packageJson[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiedPackageJson.main = mainFileName;
|
|
|
|
modifiedPackageJson.dependencies = {};
|
|
|
|
|
|
|
|
for (const dep of commonConfig.external) {
|
|
|
|
modifiedPackageJson.dependencies[dep] = packageJson.dependencies[dep];
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(buildDirPath, 'package.json'),
|
|
|
|
JSON.stringify(modifiedPackageJson, null, 2),
|
|
|
|
{
|
|
|
|
encoding: 'utf-8',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Packages the app using electron builder.
|
|
|
|
*
|
|
|
|
* Note: this also handles signing and notarization if the
|
|
|
|
* appropriate flags are set.
|
|
|
|
*
|
|
|
|
* Electron builder cli [commands](https://www.electron.build/cli)
|
|
|
|
* are passed on as builderArgs.
|
|
|
|
*/
|
|
|
|
async function packageApp() {
|
|
|
|
const { configureBuildCommand } = await await import(
|
|
|
|
'electron-builder/out/builder.js'
|
|
|
|
);
|
|
|
|
|
|
|
|
const rawArgs = hideBin(process.argv);
|
|
|
|
const builderArgs = yargs(rawArgs)
|
|
|
|
.command(['build', '*'], 'Build', configureBuildCommand)
|
|
|
|
.parse();
|
|
|
|
|
|
|
|
const buildOptions = {
|
|
|
|
config: {
|
|
|
|
directories: { output: packageDirPath, app: buildDirPath },
|
|
|
|
files: ['**'],
|
|
|
|
extends: null,
|
|
|
|
},
|
|
|
|
...builderArgs,
|
|
|
|
};
|
|
|
|
|
|
|
|
await builder.build(buildOptions);
|
|
|
|
}
|
|
|
|
|
2023-06-20 13:59:25 +00:00
|
|
|
/**
|
|
|
|
* Removes leading slash from all renderer files
|
|
|
|
* electron uses a custom registered protocol to load the
|
|
|
|
* files: "app://"
|
|
|
|
*
|
|
|
|
* @param {string} dir
|
|
|
|
* @param {string} base
|
|
|
|
*/
|
|
|
|
function removeBaseLeadingSlash(dir, base) {
|
|
|
|
for (const file of fs.readdirSync(dir)) {
|
|
|
|
const filePath = path.join(dir, file);
|
|
|
|
if (fs.lstatSync(filePath).isDirectory()) {
|
|
|
|
removeBaseLeadingSlash(filePath, base);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contents = fs.readFileSync(filePath).toString('utf-8');
|
|
|
|
fs.writeFileSync(filePath, contents.replaceAll('/' + base, base));
|
|
|
|
}
|
|
|
|
}
|