2023-03-16 17:58:21 +03:00
|
|
|
#!/usr/bin/env zx
|
|
|
|
import 'zx/globals';
|
|
|
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
|
|
|
import * as esbuild from 'esbuild';
|
|
|
|
|
2023-04-11 21:42:36 +03:00
|
|
|
import commonFn from './common.mjs';
|
2023-03-16 17:58:21 +03:00
|
|
|
|
|
|
|
const repoRootDir = path.join(__dirname, '..', '..', '..');
|
|
|
|
const electronRootDir = path.join(__dirname, '..');
|
|
|
|
const publicDistDir = path.join(electronRootDir, 'resources');
|
|
|
|
const affineWebDir = path.join(repoRootDir, 'apps', 'web');
|
|
|
|
const affineWebOutDir = path.join(affineWebDir, 'out');
|
|
|
|
const publicAffineOutDir = path.join(publicDistDir, `web-static`);
|
|
|
|
|
|
|
|
console.log('build with following dir', {
|
|
|
|
repoRootDir,
|
|
|
|
electronRootDir,
|
|
|
|
publicDistDir,
|
|
|
|
affineSrcDir: affineWebDir,
|
|
|
|
affineSrcOutDir: affineWebOutDir,
|
|
|
|
publicAffineOutDir,
|
|
|
|
});
|
|
|
|
|
|
|
|
// copy web dist files to electron dist
|
|
|
|
|
|
|
|
// step 0: clean up
|
|
|
|
await cleanup();
|
2023-03-27 09:59:36 +03:00
|
|
|
echo('Clean up done');
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-04-15 20:24:57 +03:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
$.shell = 'powershell.exe';
|
|
|
|
$.prefix = '';
|
|
|
|
}
|
2023-03-16 17:58:21 +03:00
|
|
|
// step 1: build web (nextjs) dist
|
2023-04-18 10:23:00 +03:00
|
|
|
process.env.ENABLE_LEGACY_PROVIDER = 'false';
|
2023-03-16 17:58:21 +03:00
|
|
|
cd(repoRootDir);
|
2023-03-27 09:59:36 +03:00
|
|
|
await $`yarn add`;
|
|
|
|
await $`yarn build`;
|
|
|
|
await $`yarn export`;
|
2023-04-13 16:37:50 +03:00
|
|
|
|
|
|
|
// step 1.5: amend sourceMappingURL to allow debugging in devtools
|
|
|
|
await glob('**/*.{js,css}', { cwd: affineWebOutDir }).then(files => {
|
|
|
|
return files.map(async file => {
|
|
|
|
const dir = path.dirname(file);
|
|
|
|
const fullpath = path.join(affineWebOutDir, file);
|
|
|
|
let content = await fs.readFile(fullpath, 'utf-8');
|
|
|
|
// replace # sourceMappingURL=76-6370cd185962bc89.js.map
|
|
|
|
// to # sourceMappingURL=assets://./{dir}/76-6370cd185962bc89.js.map
|
|
|
|
content = content.replace(/# sourceMappingURL=(.*)\.map/g, (_, p1) => {
|
|
|
|
return `# sourceMappingURL=assets://./${dir}/${p1}.map`;
|
|
|
|
});
|
|
|
|
await fs.writeFile(fullpath, content);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-16 17:58:21 +03:00
|
|
|
await fs.move(affineWebOutDir, publicAffineOutDir, { overwrite: true });
|
|
|
|
|
|
|
|
// step 2: build electron resources
|
|
|
|
await buildLayers();
|
2023-03-27 09:59:36 +03:00
|
|
|
echo('Build layers done');
|
2023-03-16 17:58:21 +03:00
|
|
|
|
|
|
|
/// --------
|
|
|
|
/// --------
|
|
|
|
/// --------
|
|
|
|
async function cleanup() {
|
|
|
|
await fs.emptyDir(publicAffineOutDir);
|
|
|
|
await fs.emptyDir(path.join(electronRootDir, 'layers', 'main', 'dist'));
|
|
|
|
await fs.emptyDir(path.join(electronRootDir, 'layers', 'preload', 'dist'));
|
|
|
|
await fs.remove(path.join(electronRootDir, 'out'));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildLayers() {
|
2023-04-11 21:42:36 +03:00
|
|
|
const common = commonFn();
|
|
|
|
await esbuild.build(common.preload);
|
2023-03-16 17:58:21 +03:00
|
|
|
|
|
|
|
await esbuild.build({
|
2023-04-11 21:42:36 +03:00
|
|
|
...common.main,
|
2023-03-16 17:58:21 +03:00
|
|
|
define: {
|
2023-04-11 21:42:36 +03:00
|
|
|
...common.main.define,
|
2023-03-16 17:58:21 +03:00
|
|
|
'process.env.NODE_ENV': `"production"`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|