-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.js
137 lines (113 loc) · 3.8 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const electronBuilder = require('electron-builder');
const appPath = path.join(__dirname, 'app/');
const nodeModulesPath = path.join(__dirname, 'node_modules');
const toModulePath = path.join(appPath, 'modules');
const except = ['walletPages', 'walletPages-test'];
const no_build = process.env.NO_BUILD === 'true';
const startBuild = async () => {
traversing(appPath, (fPath, folderLevel, next) => {
let stats = fs.statSync(fPath);
if (stats.isDirectory()) {
next(fPath, folderLevel + '../');
return;
}
if (stats.isFile()) {
formatFile(fPath, folderLevel);
}
}, './');
// no_build && copyFolder('./vite-web-wallet/dist', path.join(appPath, 'walletPages'));
copyIcon();
writePackage();
if (!no_build) {
let target = process.env.p === 'WIN' ? electronBuilder.Platform.WINDOWS : electronBuilder.Platform.MAC;
electronBuilder.build({
targets: electronBuilder.createTargets([
target
], null, 'all'),
projectDir: path.join(__dirname, './app'),
publish: process.env.RELEASE === 'true' ? 'always' : 'never'
}).catch(err => {
throw new Error(err);
});
}
}
startBuild().catch(err => {
console.error(err);
process.exit(1);
});
function formatFile(filePath, folderLevel) {
let result = fs.existsSync(filePath);
// Not exists
if (!result) {
console.error(new Error(`${filePath} is not exists.`));
return ;
}
let file = fs.readFileSync(filePath, {
encoding: 'utf8'
});
if (!file.match(/(~app)/)) {
return;
}
let modules = file.match(/(\'~app\/modules\/\S+\')/g);
!fs.existsSync(toModulePath) && fs.mkdirSync(toModulePath);
if (modules && modules.length) {
modules.forEach((mPath) => {
let module = mPath.split('\'~app\/modules\/')[1].replace(/\'/, '');
copyFolder(
path.join(nodeModulesPath, module),
path.join(toModulePath, module)
);
});
}
fs.writeFileSync(filePath, file.replace(/(~app\/)/g, folderLevel), 'utf8');
}
function writePackage() {
let packageFile = require('./package.json');
packageFile.main = 'main.js';
let build = require('./electron.build.json');
packageFile.build = build;
fs.writeFileSync('./app/package.json', JSON.stringify(packageFile), 'utf8');
}
function copyIcon() {
copyFolder(
path.join(__dirname, '/walletSrc/icon'),
path.join(appPath, '/icon')
);
}
// Base function
function traversing (startPath, cb, folderLevel) {
function readdirSync (startPath, folderLevel) {
let files = fs.readdirSync(startPath);
files.forEach((val) => {
if (except.indexOf(val) !== -1) {
return;
}
let fPath = path.join(startPath, val);
cb && cb(fPath, folderLevel, readdirSync, val);
});
}
readdirSync(startPath, folderLevel);
}
function copyFolder (currentPath, targetPath) {
if ( !fs.existsSync(currentPath) ) {
console.error(new Error(`${currentPath} is not exist.`));
return;
}
!fs.existsSync(targetPath) && fs.mkdirSync(targetPath);
traversing(currentPath, (fPath, targetPath, next, val) => {
let stats = fs.statSync(fPath);
let toPath = path.join(targetPath, val);
if (stats.isDirectory()) {
!fs.existsSync(toPath) && fs.mkdirSync(toPath);
next(fPath, toPath);
return;
}
if (stats.isFile()) {
let file = fs.readFileSync(fPath);
fs.writeFileSync(toPath, file);
}
}, targetPath);
}