2023-10-18 09:13:47 +03:00
|
|
|
import { spawnSync } from 'node:child_process';
|
2023-05-18 03:36:59 +03:00
|
|
|
import { createRequire } from 'node:module';
|
2023-03-16 17:58:21 +03:00
|
|
|
import path from 'node:path';
|
2023-10-18 09:13:47 +03:00
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import { glob } from 'glob';
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-05-18 03:36:59 +03:00
|
|
|
const require = createRequire(import.meta.url);
|
2023-10-18 09:13:47 +03:00
|
|
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
2023-05-18 03:36:59 +03:00
|
|
|
|
2024-09-14 09:28:46 +03:00
|
|
|
const repoRootDir = path.join(__dirname, '..', '..', '..', '..', '..');
|
2023-03-16 17:58:21 +03:00
|
|
|
const electronRootDir = path.join(__dirname, '..');
|
|
|
|
const publicDistDir = path.join(electronRootDir, 'resources');
|
2024-04-01 10:51:35 +03:00
|
|
|
const webDir = path.join(
|
|
|
|
repoRootDir,
|
|
|
|
'packages',
|
|
|
|
'frontend',
|
2024-09-14 09:28:46 +03:00
|
|
|
'apps',
|
2024-04-01 10:51:35 +03:00
|
|
|
'electron',
|
|
|
|
'renderer'
|
|
|
|
);
|
2024-03-19 10:48:56 +03:00
|
|
|
const affineWebOutDir = path.join(webDir, 'dist');
|
2023-03-16 17:58:21 +03:00
|
|
|
const publicAffineOutDir = path.join(publicDistDir, `web-static`);
|
2023-05-17 20:37:28 +03:00
|
|
|
const releaseVersionEnv = process.env.RELEASE_VERSION || '';
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-12-21 13:00:12 +03:00
|
|
|
console.log('build with following variables', {
|
2023-03-16 17:58:21 +03:00
|
|
|
repoRootDir,
|
|
|
|
electronRootDir,
|
|
|
|
publicDistDir,
|
2024-03-19 10:48:56 +03:00
|
|
|
affineSrcDir: webDir,
|
|
|
|
affineSrcOutDir: affineWebOutDir,
|
2023-03-16 17:58:21 +03:00
|
|
|
publicAffineOutDir,
|
2023-12-21 13:00:12 +03:00
|
|
|
releaseVersionEnv,
|
2023-03-16 17:58:21 +03:00
|
|
|
});
|
|
|
|
|
2023-05-17 20:37:28 +03:00
|
|
|
// step 0: check version match
|
2023-05-18 03:36:59 +03:00
|
|
|
const electronPackageJson = require(`${electronRootDir}/package.json`);
|
2023-05-17 20:37:28 +03:00
|
|
|
if (releaseVersionEnv && electronPackageJson.version !== releaseVersionEnv) {
|
|
|
|
throw new Error(
|
|
|
|
`Version mismatch, expected ${releaseVersionEnv} but got ${electronPackageJson.version}`
|
|
|
|
);
|
|
|
|
}
|
2023-03-16 17:58:21 +03:00
|
|
|
// copy web dist files to electron dist
|
|
|
|
|
2023-10-18 09:13:47 +03:00
|
|
|
process.env.DISTRIBUTION = 'desktop';
|
2023-08-14 18:40:00 +03:00
|
|
|
|
2023-10-18 09:13:47 +03:00
|
|
|
const cwd = repoRootDir;
|
2023-03-16 17:58:21 +03:00
|
|
|
|
2023-10-18 09:13:47 +03:00
|
|
|
// step 1: build web dist
|
2023-04-21 13:06:54 +03:00
|
|
|
if (!process.env.SKIP_WEB_BUILD) {
|
2024-09-18 08:09:42 +03:00
|
|
|
spawnSync('yarn', ['build'], {
|
2023-10-18 09:13:47 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
env: process.env,
|
|
|
|
cwd,
|
2024-04-16 09:57:17 +03:00
|
|
|
shell: true,
|
2023-10-18 09:13:47 +03:00
|
|
|
});
|
2023-09-01 06:34:18 +03:00
|
|
|
|
2023-10-18 09:13:47 +03:00
|
|
|
spawnSync('yarn', ['workspace', '@affine/electron', 'build'], {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: process.env,
|
|
|
|
cwd,
|
2024-04-16 09:57:17 +03:00
|
|
|
shell: true,
|
2023-10-18 09:13:47 +03:00
|
|
|
});
|
2023-10-17 04:15:19 +03:00
|
|
|
|
2023-09-01 06:34:18 +03:00
|
|
|
// step 1.5: amend sourceMappingURL to allow debugging in devtools
|
2024-03-19 10:48:56 +03:00
|
|
|
await glob('**/*.{js,css}', { cwd: affineWebOutDir }).then(files => {
|
2024-08-13 09:26:26 +03:00
|
|
|
return Promise.all(
|
|
|
|
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
|
2024-11-01 09:33:27 +03:00
|
|
|
// to # sourceMappingURL=assets://./{dir}/76-6370cd185962bc89.js.map
|
2024-08-13 09:26:26 +03:00
|
|
|
content = content.replace(/# sourceMappingURL=(.*)\.map/g, (_, p1) => {
|
2024-11-01 09:33:27 +03:00
|
|
|
return `# sourceMappingURL=assets://./${dir}/${p1}.map`;
|
2024-08-13 09:26:26 +03:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
await fs.writeFile(fullpath, content);
|
|
|
|
console.log('amended sourceMappingURL for', fullpath);
|
|
|
|
} catch (e) {
|
|
|
|
// do not crash the build
|
|
|
|
console.error('error writing file', fullpath, e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2023-09-01 06:34:18 +03:00
|
|
|
});
|
|
|
|
|
2024-03-19 10:48:56 +03:00
|
|
|
await fs.move(affineWebOutDir, publicAffineOutDir, { overwrite: true });
|
2023-04-21 13:06:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-14 13:40:13 +03:00
|
|
|
// step 2: update app-updater.yml content with build type in resources folder
|
2023-05-16 06:34:54 +03:00
|
|
|
if (process.env.BUILD_TYPE === 'internal') {
|
|
|
|
const appUpdaterYml = path.join(publicDistDir, 'app-update.yml');
|
|
|
|
const appUpdaterYmlContent = await fs.readFile(appUpdaterYml, 'utf-8');
|
|
|
|
const newAppUpdaterYmlContent = appUpdaterYmlContent.replace(
|
|
|
|
'AFFiNE',
|
|
|
|
'AFFiNE-Releases'
|
|
|
|
);
|
|
|
|
await fs.writeFile(appUpdaterYml, newAppUpdaterYmlContent);
|
|
|
|
}
|