forked from kuaifan/dootask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
198 lines (193 loc) · 7.39 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const os = require('os')
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path')
const inquirer = require('inquirer');
const child_process = require('child_process');
const ora = require('ora');
const axios = require('axios');
const FormData =require('form-data');
const utils = require('./utils');
const config = require('../package.json')
const argv = process.argv;
const env = require('dotenv').config({ path: './.env' })
const electronDir = path.resolve(__dirname, "public");
const nativeCachePath = path.resolve(__dirname, ".native");
const devloadCachePath = path.resolve(__dirname, ".devload");
const packageFile = path.resolve(__dirname, "package.json");
const packageBakFile = path.resolve(__dirname, "package-bak.json");
const platform = ["build-mac", "build-win"];
const comSuffix = os.type() == 'Windows_NT' ? '.cmd' : '';
// 克隆 Drawio
function cloneDrawio(systemInfo) {
child_process.spawnSync("git", ["submodule", "update", "--quiet", "--init", "--depth=1"], {stdio: "inherit"});
const drawioSrcDir = path.resolve(__dirname, "../resources/drawio/src/main/webapp");
const drawioCoverDir = path.resolve(__dirname, "../docker/drawio/webapp");
const drawioDestDir = path.resolve(electronDir, "drawio/webapp");
fse.copySync(drawioSrcDir, drawioDestDir)
fse.copySync(drawioCoverDir, drawioDestDir)
//
const preConfigFile = path.resolve(drawioDestDir, "js/PreConfig.js");
if (!fse.existsSync(preConfigFile)) {
console.log("clone drawio error!");
process.exit()
}
let preConfigString = fs.readFileSync(preConfigFile, 'utf8');
preConfigString += "\nwindow.systemInfo = " + JSON.stringify(systemInfo) + ";\n";
preConfigString += fs.readFileSync(path.resolve(__dirname, "drawio.js"), 'utf8');
fs.writeFileSync(preConfigFile, preConfigString, 'utf8');
}
// 通用发布
function genericPublish(url, version) {
const filePath = path.resolve(__dirname, "dist")
fs.readdir(filePath, async (err, files) => {
if (err) {
console.warn(err)
} else {
for (const filename of files) {
const localFile = path.join(filePath, filename)
const fileStat = fs.statSync(localFile)
if (fileStat.isFile()) {
const uploadOra = ora(`${filename} uploading...`).start()
const formData = new FormData()
formData.append("file", fs.createReadStream(localFile));
await axios({
method: 'post',
url: url,
data: formData,
maxContentLength: Infinity,
maxBodyLength: Infinity,
headers: {
'Generic-Version': version,
'Content-Type': 'multipart/form-data;boundary=' + formData.getBoundary(),
}
}).then(_ => {
uploadOra.succeed(`${filename} upload successful`)
}).catch(_ => {
uploadOra.fail(`${filename} upload fail`)
})
}
}
}
});
}
// 生成配置、编译应用
function startBuild(data, publish) {
// information
console.log("Name: " + data.name);
console.log("AppId: " + data.id);
console.log("Version: " + config.version);
console.log("Publish: " + (publish ? 'Yes' : 'No'));
let systemInfo = {
title: data.name,
version: config.version,
origin: "./",
homeUrl: utils.formatUrl(data.url),
apiUrl: utils.formatUrl(data.url) + "api/",
}
// drawio
cloneDrawio(systemInfo)
// config.js
fs.writeFileSync(electronDir + "/config.js", "window.systemInfo = " + JSON.stringify(systemInfo), 'utf8');
fs.writeFileSync(nativeCachePath, utils.formatUrl(data.url));
fs.writeFileSync(devloadCachePath, "", 'utf8');
// index.html
let indexFile = path.resolve(electronDir, "index.html");
let indexString = fs.readFileSync(indexFile, 'utf8');
indexString = indexString.replace(/<title>(.*?)<\/title>/g, `<title>${data.name}</title>`);
fs.writeFileSync(indexFile, indexString, 'utf8');
// package.json Backup
fse.copySync(packageFile, packageBakFile)
// package.json Generated
const econfig = require('./package.json')
econfig.name = data.name;
econfig.version = config.version;
econfig.build.appId = data.id;
econfig.build.artifactName = utils.getDomain(data.url) + "-v${version}-${os}-${arch}.${ext}";
econfig.build.nsis.artifactName = utils.getDomain(data.url) + "-v${version}-${os}-${arch}.${ext}";
econfig.build.pkg.mustClose = [data.id];
if (!process.env.APPLEID || !process.env.APPLEIDPASS) {
delete econfig.build.afterSign;
}
if (process.env.RELEASE_BODY) {
econfig.build.releaseInfo.releaseNotes = process.env.RELEASE_BODY
}
if (utils.isJson(data.publish)) {
econfig.build.publish = data.publish
}
fs.writeFileSync(packageFile, JSON.stringify(econfig, null, 2), 'utf8');
// build
child_process.spawnSync("npm" + comSuffix, ["run", data.platform + (publish === true ? "-publish" : "")], {stdio: "inherit", cwd: "electron"});
// package.json Recovery
fse.copySync(packageBakFile, packageFile)
// generic publish
if (publish === true && econfig.build.publish.provider === "generic") {
genericPublish(econfig.build.publish.url, config.version)
}
}
if (["dev"].includes(argv[2])) {
// 开发模式
fs.writeFileSync(devloadCachePath, utils.formatUrl("127.0.0.1:" + env.parsed.APP_PORT), 'utf8');
child_process.spawn("npx", ["mix", "watch", "--hot", "--", "--env", "--electron"], {stdio: "inherit"});
child_process.spawn("npm", ["run", "start-quiet"], {stdio: "inherit", cwd: "electron"});
} else if (platform.includes(argv[2])) {
// 自动编译
let provider = process.env.PROVIDER === "generic" ? "generic" : "github"
config.app.forEach(data => {
if (data.publish.provider === provider) {
data.platform = argv[2];
startBuild(data, true)
}
})
} else {
// 自定义编译
let appChoices = [];
config.app.forEach(data => {
appChoices.push({
name: data.name,
value: data
})
})
const questions = [
{
type: 'list',
name: 'app',
message: "选择编译应用",
choices: appChoices
},
{
type: 'list',
name: 'platform',
message: "选择编译系统",
choices: [{
name: "MacOS",
value: [platform[0]]
}, {
name: "Window",
value: [platform[1]]
}, {
name: "All platforms",
value: platform
}]
},
{
type: 'list',
name: 'publish',
message: "选择是否要发布",
choices: [{
name: "No",
value: false
}, {
name: "Yes",
value: true
}]
}
];
inquirer.prompt(questions).then(answers => {
answers.platform.forEach(platform => {
let data = answers.app;
data.platform = platform
startBuild(data, answers.publish)
});
});
}