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
| import fs from 'node:fs/promises';
import {createWriteStream, createReadStream} from 'node:fs';
import {Transform} from 'node:stream';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {formatRFC3339} from 'date-fns';
import {simpleGit} from 'simple-git';
import process from 'node:process';
import zlib from 'node:zlib';
import {createHash} from 'node:crypto';
const download_url_base = '/updater';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packageFile = path.join(__dirname, '..', 'package.json');
const git = simpleGit(path.join(__dirname, '..'), {binary: 'git'});
const asarPaths = [
path.join(__dirname, '../dist/win-unpacked/resources/app.asar'),
path.join(__dirname, '../dist/mac/slots-config-gui.app/Contents/Resources/app.asar'),
];
const updaterDir = path.join(__dirname, '../dist/updater');
const versionFile = path.join(updaterDir, 'version.json');
class HashTransform extends Transform {
#hash;
#result;
constructor(algorithm, options) {
super();
this.#hash = createHash(algorithm, options);
}
_transform(chunk, _, next) {
this.#hash.update(chunk);
next(null, chunk);
}
_flush(done) {
this.#result = this.#hash.digest('hex').toLowerCase();
done();
}
get hash() {
return this.#result;
}
}
/**
*
* @param {fs.Stream} stream
* @param {...fs.Writable} streams
* @returns {Promise<void>}
*/
function pipe(stream, ...streams) {
return new Promise(function (resolve, reject) {
stream.on('error', reject);
for (const nextStream of streams) {
stream = stream.pipe(nextStream);
stream.on('error', reject);
}
stream.on('finish', resolve);
});
}
async function getCommit() {
const local = await git.log({
maxCount: 1,
});
return [local.latest.message, local.latest.hash];
}
async function exists(path, mode) {
try {
await fs.access(path, mode);
return true;
} catch (err) {
return false;
}
}
async function main() {
let asarPath = null;
for (const p of asarPaths) {
if (await exists(p)) {
asarPath = p;
break;
}
}
if (!asarPath) {
throw new Error('Could not find app.asar');
}
const p = JSON.parse(await fs.readFile(packageFile, {encoding: 'utf-8'}));
const version = p.version;
await fs.rm(updaterDir, {recursive: true, force: true}).catch(Promise.resolve);
await fs.mkdir(updaterDir, {recursive: true});
const asarName = `app-${version}.asar.gz`;
const intput = createReadStream(asarPath);
const outputPath = path.join(updaterDir, asarName);
const output = createWriteStream(outputPath);
const hashTransform = new HashTransform('md5');
await pipe(intput, zlib.createGzip(), hashTransform, output);
const checksum = hashTransform.hash;
const now = new Date();
const [description, commit_hash] = await getCommit();
await fs.writeFile(
versionFile,
JSON.stringify(
{
version: version,
datetime: formatRFC3339(now),
description: process.argv.length >= 3 ? process.argv[2] : description,
download_url: `${download_url_base}/${asarName}`,
commit_hash,
checksum,
},
null,
2,
),
);
console.log(`${version} pack done`);
}
await main();
|