-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
112 lines (99 loc) · 3.57 KB
/
index.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
import fs from "fs-extra";
import path from "path";
import { spawn } from "child_process";
//@ts-ignore
import qode from "@nodegui/qode";
//@ts-ignore
import { qtHome } from "@nodegui/nodegui/config/qtConfig";
import { switchToGuiSubsystem } from "./patchQode";
const cwd = process.cwd();
const deployDirectory = path.resolve(cwd, "deploy");
const configFile = path.resolve(deployDirectory, "config.json");
const copyQode = async (dest: string) => {
const qodeBinaryFile = qode.qodePath;
await fs.chmod(qodeBinaryFile, "755");
await fs.copyFile(qodeBinaryFile, path.resolve(dest, "qode.exe"));
};
const copyAppDist = async (distPath: string, resourceDir: string) => {
await fs.copy(distPath, path.resolve(resourceDir, "dist"), {
recursive: true,
});
};
function getAllNodeAddons(dirPath: string) {
const addonExt = "node";
let dir = fs.readdirSync(dirPath);
return dir
.filter((elm) => elm.match(new RegExp(`.*\.(${addonExt}$)`, "ig")))
.map((eachElement) => path.resolve(dirPath, eachElement));
}
const runWinDeployQt = async (appName: string, buildDir: string) => {
const winDeployQtBin = path.resolve(qtHome, "bin", "windeployqt.exe");
// insert qtHome/bin into the PATH for windeployqt work correctly
process.env.PATH=`${path.resolve(qtHome, "bin")};${process.env.PATH}`;
const distPath = path.resolve(buildDir, "dist");
const allAddons = getAllNodeAddons(distPath);
const winDeployQt = spawn(
winDeployQtBin,
[
...allAddons,
"--verbose=2",
"--release",
"--no-translations",
"--compiler-runtime",
`--dir=${buildDir}`,
],
{
cwd: buildDir,
env: process.env,
}
);
return new Promise((resolve, reject) => {
winDeployQt.stdout.on("data", function (data) {
console.log("stdout: " + data.toString());
});
winDeployQt.stderr.on("data", function (data) {
console.log("stderr: " + data.toString());
});
winDeployQt.on("exit", function (code) {
if (!code) {
return resolve(true);
}
return reject("child process exited with code " + code);
});
});
};
export const init = async (appName: string) => {
const config = {
appName: null,
};
const templateDirectory = path.resolve(__dirname, "../../template/win32");
const userTemplate = path.resolve(deployDirectory, "win32");
const appDir = path.resolve(userTemplate, appName);
await fs.mkdirp(path.resolve(userTemplate, appDir));
await fs.copy(templateDirectory, appDir, { recursive: true });
Object.assign(config, { appName });
await fs.writeJSON(configFile, config);
};
export const pack = async (distPath: string) => {
const config = await fs.readJSON(
path.resolve(deployDirectory, "config.json")
);
const { appName } = config;
const usertemplate = path.resolve(deployDirectory, "win32");
const templateAppDir = path.resolve(usertemplate, appName);
const buildDir = path.resolve(usertemplate, "build");
const buildAppPackage = path.resolve(buildDir, appName);
console.log(`cleaning build directory at ${buildDir}`);
await fs.remove(buildDir);
console.log(`creating build directory at ${buildDir}`);
await fs.copy(templateAppDir, buildAppPackage, { recursive: true });
console.log(`copying qode`);
await copyQode(buildAppPackage);
console.log(`copying dist`);
await copyAppDist(distPath, buildAppPackage);
console.log(`running windeployqt`);
await runWinDeployQt(appName, buildAppPackage);
console.log(`Hiding Qode's console`);
await switchToGuiSubsystem(path.resolve(buildAppPackage, "qode.exe"));
console.log(`Build successful. Find the app at ${buildDir}`);
};