-
Notifications
You must be signed in to change notification settings - Fork 12
/
publish.ts
214 lines (188 loc) · 5.75 KB
/
publish.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { join } from 'path';
import { createReadStream, copySync, removeSync } from 'fs-extra';
import http from 'http';
import FormData from 'form-data';
import ProgressBar from 'progress';
import AdmZip from 'adm-zip';
import webpackPaths from '../configs/webpack.paths';
import { version } from '../../release/app/package.json';
import { build } from '../../package.json';
const host = '127.0.0.1',
port = 3000;
const { PUBLISH_TYPE } = process.env;
if (!PUBLISH_TYPE) {
console.log();
process.exit(0);
}
const { productName } = build;
const { platform, archs } = process.env;
const isDarwin = platform === 'darwin';
console.log('--------- Start to publish --------');
function getFormLength(form: FormData): Promise<number> {
return new Promise((resolve) => {
form.getLength(function (_err, size) {
resolve(size);
});
});
}
(async () => {
if (PUBLISH_TYPE === 'full') {
// Full publish
const form = new FormData({ maxDataSize: 200 * 1024 * 1024 });
form.append('platform', isDarwin ? 'macos' : 'windows');
// MacOS does not distinguish between x64 and arm64
// packages for both architectures are packaged under the same file.
form.append('archs', isDarwin ? 'mac' : archs === 'x64' ? archs : 'x32');
form.append('version', version);
console.log('files:');
const ymlPath = join(
webpackPaths.buildPath,
isDarwin ? 'latest-mac.yml' : 'latest.yml'
);
form.append('yml', createReadStream(ymlPath));
console.log(` ${ymlPath}`);
if (platform === 'darwin') {
// MacOS
['x64', 'arm64'].forEach((arch) => {
form.append(
'app',
createReadStream(
join(
webpackPaths.buildPath,
`${productName}_Setup_${arch}_${version}.dmg`
)
)
);
form.append(
'app',
createReadStream(
join(
webpackPaths.buildPath,
`${productName}_Setup_${arch}_${version}.zip`
)
)
);
});
} else if (platform === 'win32') {
// Windows
const appPath = join(
webpackPaths.buildPath,
`${productName}_Setup_${archs}_${version}.exe`
);
form.append('app', createReadStream(appPath));
console.log(` ${appPath}`);
}
const uploadSize = await getFormLength(form);
const progressBar = new ProgressBar(
'uploading [:bar] :rate/bps :percent :etas :elapseds',
{
complete: '=',
incomplete: ' ',
width: 40,
total: uploadSize,
}
);
form.on('data', function (data) {
progressBar.tick(data.length, undefined);
});
const req = http.request({
host,
port,
method: 'POST',
path: '/api/v1/electron/full',
headers: form.getHeaders(),
});
form.pipe(req);
req.on('response', function (res) {
res.on('data', (chunk) => {
const { code, msg } = JSON.parse(`${chunk}`);
if (code === 200) {
console.log(msg);
} else {
console.log(`Publish failed: ${msg}`);
}
});
res.on('end', () => {
console.log('---------- Publish end ----------');
});
});
} else if (PUBLISH_TYPE === 'asar') {
// Incremental publish
const form = new FormData({ maxDataSize: 200 * 1024 * 1024 });
form.append('platform', isDarwin ? 'macos' : 'windows');
form.append('version', version);
// mac => x64 | mac-arm64 => arm64
if (platform === 'win32') {
const curArchs = archs === 'x64' ? 'x64' : 'x32';
const resourcesPath = join(
webpackPaths.buildPath,
'win-unpacked/resources'
);
const zip = new AdmZip();
// zip app.asar
const asar = join(resourcesPath, 'app.asar');
const update = join(webpackPaths.buildPath, `update.asar`);
copySync(asar, update);
zip.addLocalFile(update);
const updateZip = join(webpackPaths.buildPath, `${curArchs}.zip`);
zip.writeZip(updateZip);
removeSync(update);
form.append('app', createReadStream(updateZip));
console.log(` ${updateZip}`);
} else if (platform === 'darwin') {
console.log('files:');
['x64', 'arm64'].map((archs) => {
const zip = new AdmZip();
const resourcesPath = join(
webpackPaths.buildPath,
archs === 'x64' ? 'mac' : 'mac-arm64',
`${productName}.app/Contents/Resources`
);
// zip app.asar
const asar = join(resourcesPath, 'app.asar');
const update = join(webpackPaths.buildPath, `update.asar`);
copySync(asar, update);
zip.addLocalFile(update);
const updateZip = join(webpackPaths.buildPath, `${archs}.zip`);
zip.writeZip(updateZip);
removeSync(update);
form.append('app', createReadStream(updateZip));
console.log(` ${updateZip}`);
});
}
const uploadSize = await getFormLength(form);
const progressBar = new ProgressBar(
'uploading [:bar] :rate/bps :percent :etas :elapseds',
{
complete: '=',
incomplete: ' ',
width: 40,
total: uploadSize,
}
);
form.on('data', function (data) {
progressBar.tick(data.length, undefined);
});
const req = http.request({
host,
port,
method: 'POST',
path: '/api/v1/electron/asar',
headers: form.getHeaders(),
});
form.pipe(req);
req.on('response', function (res) {
res.on('data', (chunk) => {
const { code, msg } = JSON.parse(`${chunk}`);
if (code === 200) {
console.log(msg);
} else {
console.log(`Publish failed: ${msg}`);
}
});
res.on('end', () => {
console.log('---------- Publish end ----------');
});
});
}
})();