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';
|
2024-02-07 04:17:04 +00:00
|
|
|
import frappeBooksConfig from '../../electron-builder-config.mjs';
|
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-27 06:05:31 +00:00
|
|
|
const rawArgs = yargs(hideBin(process.argv))
|
|
|
|
.option('nosign', {
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Run electron-builder without code signing',
|
|
|
|
})
|
|
|
|
.option('nopackage', {
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Only build the source files, electron-builder will not run',
|
|
|
|
});
|
|
|
|
|
|
|
|
const argv = rawArgs.argv;
|
|
|
|
if (argv.nosign) {
|
|
|
|
process.env['CSC_IDENTITY_AUTO_DISCOVERY'] = false;
|
|
|
|
}
|
|
|
|
|
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();
|
2023-06-27 06:05:31 +00:00
|
|
|
|
|
|
|
if (!argv.nopackage) {
|
|
|
|
await packageApp();
|
|
|
|
}
|
2023-06-21 06:30:22 +00:00
|
|
|
|
|
|
|
function updatePaths() {
|
|
|
|
fs.removeSync(buildDirPath);
|
2023-06-21 10:38:39 +00:00
|
|
|
fs.ensureDirSync(buildDirPath);
|
2023-06-21 06:30:22 +00:00
|
|
|
fs.removeSync(packageDirPath);
|
2023-06-21 10:38:39 +00:00
|
|
|
fs.ensureDirSync(packageDirPath);
|
2023-06-21 06:30:22 +00:00
|
|
|
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-07-10 07:34:14 +00:00
|
|
|
outdir: path.join(buildDirPath),
|
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'),
|
2023-06-27 09:59:37 +00:00
|
|
|
build: { outDir, sourcemap: true },
|
2023-06-20 13:59:25 +00:00
|
|
|
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
|
2023-06-27 06:05:31 +00:00
|
|
|
* appropriate flags are set.
|
|
|
|
*
|
2023-06-21 06:30:22 +00:00
|
|
|
* 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'
|
|
|
|
);
|
|
|
|
|
2023-06-27 06:05:31 +00:00
|
|
|
const builderArgs = rawArgs
|
2023-06-21 06:30:22 +00:00
|
|
|
.command(['build', '*'], 'Build', configureBuildCommand)
|
|
|
|
.parse();
|
|
|
|
|
2023-06-27 06:05:31 +00:00
|
|
|
for (const opt of ['nosign', 'nopackage']) {
|
|
|
|
delete builderArgs[opt];
|
|
|
|
}
|
|
|
|
|
2024-02-07 04:17:04 +00:00
|
|
|
|
|
|
|
|
2024-01-25 02:39:18 +00:00
|
|
|
let buildOptions = {
|
2024-02-07 04:17:04 +00:00
|
|
|
config: frappeBooksConfig,
|
2023-06-21 06:30:22 +00:00
|
|
|
...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));
|
|
|
|
}
|
|
|
|
}
|