This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
102 lines (89 loc) · 2.73 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const packager = require('electron-packager');
const fs = require('fs-extra');
const path = require('path');
const child_process = require('child_process');
(async function () {
//todo: choose platform
// await build('darwin', false);
// await build('linux', false);
await build('win32', false);
})().then(() => undefined).catch(() => undefined)
function build(platform, install = false) {
return new Promise(async (reoslve, reject) => {
console.log('building for ' + platform);
if (install) {
console.log('npm install...');
await exec('npm install', path.join(process.cwd(), 'main'));
await exec('npm install', path.join(process.cwd(), 'renderer'));
}
console.log('main...');
await exec('npm run build:prod', path.join(process.cwd(), 'main'));
console.log('renderer...');
await exec('npm run build', path.join(process.cwd(), 'renderer'));
console.log('packaging');
await electronPackager(platform);
console.log('copy renderer');
copyRenderer(platform);
})
}
function electronPackager(platform) {
return new Promise((resolve) => {
packager({
dir: './main',
overwrite: true,
platform: platform,
arch: 'x64',
name: 'causal-canvas',
icon: platform === 'win32' ? './logo.ico' : platform === 'darwin' ? './logo.icns' : undefined
})
.then(appPaths => {
resolve();
})
})
}
function copyRenderer(platform) {
const rendererBuildPath = path.join(__dirname, 'renderer', 'build');
let rendererDestPath = '';
if (platform === 'win32')
rendererDestPath = path.join(__dirname, 'causal-canvas-win32-x64', 'resources', 'app', 'dist');
else if (platform === 'darwin')
rendererDestPath = path.join(__dirname, 'causal-canvas-darwin-x64', 'causal-canvas.app', 'Contents', 'Resources', 'app', 'dist');
else if (platform === 'linux')
rendererDestPath = path.join(__dirname, 'causal-canvas-linux-x64', 'resources', 'app', 'dist');
else return;
const existsRenderrBuild = fs.existsSync(rendererBuildPath);
if (!existsRenderrBuild) {
console.log("RENDERER BUILD NOT FOUND");
return;
}
copyDir(rendererBuildPath, rendererDestPath);
}
function copyDir(src, dest) {
const files = fs.readdirSync(src);
for (const file of files) {
const srcFilePath = path.join(src, file);
const destFilePath = path.join(dest, file);
const stats = fs.statSync(srcFilePath);
if (stats.isDirectory()) {
fs.mkdirSync(destFilePath);
copyDir(srcFilePath, destFilePath);
} else {
fs.copyFileSync(srcFilePath, destFilePath)
}
}
}
function exec(command, cwd) {
return new Promise((resolve, reject) => {
child_process.exec(command, {
cwd,
}, (error, stout, sterr) => {
if (error) {
// console.log(sterr);
console.log(error);
return reject(error);
}
// console.log(stout);
resolve();
});
})
}