/
Maker7z-sfx.ts
130 lines (111 loc) · 4.15 KB
/
Maker7z-sfx.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
import MakerBase, { MakerOptions } from '@electron-forge/maker-base';
import { ForgePlatform } from '@electron-forge/shared-types';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import { finished } from 'stream';
export interface MakerSfxConfig {
sfxFile?: string,
//the following map to https://github.com/chrislake/7zsfxmm/wiki/Parameters
title?: string,
extractDialogText?: string,
installPath?: string,
runProgram: string,
guiMode?: 1 | 2,
overwriteMode?: 0 | 1 | 2,
extractPathTitle?: string,
extractTitle?: string,
additionalConfig?: string[],
additionalPostConfig?: string[],
sevenZMethod: string[]
}
export default class Maker7zSfx extends MakerBase<MakerSfxConfig> {
name = 'maker-7z-sfx';
defaultPlatforms: ForgePlatform[] = ['win32'];
isSupportedOnCurrentPlatform() {
return true;
}
async make({
dir,
makeDir,
packageJSON,
targetArch,
targetPlatform,
}: MakerOptions) {
const exePath = path.resolve(makeDir, '7z-sfx', targetPlatform, targetArch, `${path.basename(dir)}-${packageJSON.version}.exe`);
await this.ensureFile(exePath);
const runProgram = this.config.runProgram || `${packageJSON.productName}.exe`;
if (!(await promisify(fs.exists)(path.join(dir, runProgram)))) {
throw new Error(`${path.join(dir, runProgram)} doesn't exist use runProgram option if your executable is not package productName.exe`);
}
await build(exePath, path.join(dir, "*"), this.config, targetArch, runProgram);
return [exePath];
}
}
export const build = async function (output: string, input: string, config: MakerSfxConfig, targetArch: string, runProgram: string) {
// eslint-disable-next-line global-require
const Seven = require('node-7z');
const sevenZFile = output.replace("exe", "7z");
console.log(`Compressing ${input} to ${sevenZFile}`);
await promisify(finished)(Seven.add(sevenZFile, input, {
method: config.sevenZMethod || [
"0=lzma2",
"x=9",
"d=1024m",
"s=on"
],
$bin: require.resolve("7zip-bin\\win\\x64\\7za.exe")
}));
console.log("Compressed");
console.log(`Building executable ${output}`);
const exeStream = fs.createWriteStream(output);
await pipeFileToStream(config.sfxFile || path.join(__dirname, "..", "7zsd_extra_162_3888", targetArch === "x64" ? "7zSD_All_x64.sfx" : "7zSD_All.sfx"), exeStream);
await writeConfigToStream(config, exeStream, runProgram);
await pipeFileToStream(sevenZFile, exeStream);
await promisify(fs.unlink)(sevenZFile);
exeStream.close();
}
async function pipeFileToStream(file: string, writeStream: fs.WriteStream) {
console.log(`Copying ${file}`);
const readStream = fs.createReadStream(file);
const p = new Promise((resolve: (value?: unknown) => void, reject: (reason?: any) => void) => {
readStream.on('end', () => {
resolve();
});
readStream.on('error', (err: any) => {
console.warn(`Rejecting error `, err);
reject(err);
});
});
readStream.pipe(writeStream, { end: false });
await p;
console.log(`Copied ${file}`);
}
async function writeConfigToStream(config: MakerSfxConfig, exeStream: fs.WriteStream, runProgram: string) {
exeStream.write(";!@Install@!UTF-8! \r\n");
if (config.title) {
exeStream.write(`Title="${config.title}" \r\n`);
}
exeStream.write(`InstallPath="${config.installPath || "extract"}" \r\n`);
exeStream.write(`GUIMode="${config.guiMode || 1}" \r\n`);
exeStream.write(`OverwriteMode="${config.overwriteMode || 2}" \r\n`);
if (config.extractPathTitle) {
exeStream.write(`ExtractPathTitle="${config.extractPathTitle}" \r\n`);
}
exeStream.write(`ExtractTitle="${config.extractTitle || `Launching ${runProgram}`}" \r\n`);
if (config.extractDialogText) {
exeStream.write(`ExtractDialogText="${config.extractDialogText}" \r\n`);
}
exeStream.write(`RunProgram="${runProgram}" \r\n`);
if (config.additionalConfig) {
config.additionalConfig.forEach(line => {
exeStream.write(`${line} \r\n`);
});
}
exeStream.write(";!@InstallEnd@! \r\n");
if (config.additionalPostConfig) {
config.additionalPostConfig.forEach(line => {
exeStream.write(`${line} \r\n`);
});
}
}