forked from CyyberFroggy/porn-vault-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
198 lines (163 loc) · 5.65 KB
/
gulpfile.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
import { exec, ExecOptions } from "child_process";
import { mkdirSync } from "fs";
import { dest, src } from "gulp";
import zip from "gulp-zip";
import yargs from "yargs";
import { version as packageVersion } from "./package.json";
import { version as assetsVersion } from "./assets/version.json";
const gulpArgs = yargs.string("build-version").argv;
/**
* Usage with npm: npm run build:linux -- --build-version <version>
* Usage with gulp: gulp buildWin --build-version <version>
*/
const RELEASE_DIR = "./releases";
const getOutDir = (pkgTarget: string) => {
const main = `${RELEASE_DIR}/${pkgTarget}`;
if (gulpArgs["build-version"]) {
return `${main}_${gulpArgs["build-version"]}`;
}
return main;
};
enum BuildTargets {
GENERIC = "node14",
WINDOWS = "node14-win-x64",
LINUX = "node14-linux-x64",
MAC = "node14-macos-x64",
ARMV7 = "node14-linux-armv7",
}
const BuildTargetNames = {
[BuildTargets.GENERIC]: "generic",
[BuildTargets.WINDOWS]: "windows",
[BuildTargets.LINUX]: "linux",
[BuildTargets.MAC]: "macos",
[BuildTargets.ARMV7]: "armv7",
};
const MAIN_TARGETS: BuildTargets[] = [BuildTargets.WINDOWS, BuildTargets.LINUX, BuildTargets.MAC];
function checkVersion() {
const buildVersion = gulpArgs["build-version"];
if (!buildVersion) {
console.log("WARN: did not receive version");
} else if (buildVersion !== packageVersion) {
throw new Error(`argument build version "${buildVersion}" is not the same as the version in package.json: "${packageVersion}"`);
} else if (packageVersion !== assetsVersion) {
throw new Error(`package.json version "${packageVersion}" is not the same as the version in assets/version.json: "${assetsVersion}"`);
}
}
async function copy(source: string, target: string) {
return new Promise((resolve, reject) => {
src(source).pipe(dest(target)).on("end", resolve).on("error", reject);
});
}
async function execAsync(cmd: string, opts: ExecOptions = {}) {
return new Promise((resolve, reject) => {
exec(cmd, opts, (err, stdout, stderr) => {
console.log(stdout);
console.error(stderr);
if (err) {
reject(err);
} else {
resolve(stdout || stderr);
}
});
});
}
export async function installApp() {
return execAsync("npm ci", { cwd: "./app" });
}
export async function buildApp() {
return execAsync("npm run build", { cwd: "./app" });
}
async function runVersionScript() {
return execAsync("node version");
}
async function transpileProd() {
return execAsync("npm run transpile:prod");
}
async function packageServer(target: BuildTargets, outPath: string) {
return execAsync(
`npx pkg . --targets ${target} --options max_old_space_size=8192 --out-path ${outPath}`
);
}
async function buildServer(pkgTarget: BuildTargets, outDir: string) {
await runVersionScript();
await transpileProd();
await packageServer(pkgTarget, outDir);
}
async function buildPlatform(pkgTarget: BuildTargets) {
checkVersion();
const outDir = getOutDir(pkgTarget);
mkdirSync(`${outDir}/app/dist`, { recursive: true });
await Promise.all([
copy("./views/**/*", `${outDir}/views`),
copy("./assets/**/*", `${outDir}/assets`),
(async () => {
await buildApp();
await copy("./app/dist/**/*", `${outDir}/app/dist`);
})(),
buildServer(pkgTarget, outDir),
]);
}
async function zipRelease(buildTarget: BuildTargets) {
checkVersion();
const friendlyTargetName = BuildTargetNames[buildTarget];
const finalOutZip = gulpArgs["build-version"]
? `porn-vault_${gulpArgs["build-version"]}_${friendlyTargetName}.zip`
: `porn-vault_${friendlyTargetName}.zip`;
return new Promise((resolve, reject) => {
src(`${getOutDir(buildTarget)}/**/*`)
.pipe(zip(finalOutZip))
.pipe(dest(RELEASE_DIR))
.on("end", resolve)
.on("error", reject);
});
}
const PlatformsFunctions = Object.values(BuildTargets).reduce((platforms, target) => {
platforms[target] = {
build: () => buildPlatform(target),
zip: () => zipRelease(target),
};
return platforms;
}, {}) as {
[key in BuildTargets]: {
build: () => Promise<void>;
zip: () => Promise<void>;
};
};
export const buildGeneric = PlatformsFunctions[BuildTargets.GENERIC].build;
export const zipGeneric = PlatformsFunctions[BuildTargets.GENERIC].zip;
export const buildWindows = PlatformsFunctions[BuildTargets.WINDOWS].build;
export const zipWindows = PlatformsFunctions[BuildTargets.WINDOWS].zip;
export const buildLinux = PlatformsFunctions[BuildTargets.LINUX].build;
export const zipLinux = PlatformsFunctions[BuildTargets.LINUX].zip;
export const buildMac = PlatformsFunctions[BuildTargets.MAC].build;
export const zipMac = PlatformsFunctions[BuildTargets.MAC].zip;
export const buildArmv7 = PlatformsFunctions[BuildTargets.ARMV7].build;
export const zipArmv7 = PlatformsFunctions[BuildTargets.ARMV7].zip;
export async function buildAll() {
checkVersion();
MAIN_TARGETS.map((pkgTarget) =>
mkdirSync(`${getOutDir(pkgTarget)}/app/dist`, { recursive: true })
);
await Promise.all([
...MAIN_TARGETS.flatMap((pkgTarget: string) => [
copy("./views/**/*", `${getOutDir(pkgTarget)}/views`),
copy("./assets/**/*", `${getOutDir(pkgTarget)}/assets`),
]),
(async () => {
await buildApp();
await Promise.all(
MAIN_TARGETS.map((pkgTarget) => copy("./app/dist/**/*", `${getOutDir(pkgTarget)}/app/dist`))
);
})(),
(async () => {
await runVersionScript();
await transpileProd();
})(),
]);
await Promise.all(
MAIN_TARGETS.map((pkgTarget) => packageServer(pkgTarget, getOutDir(pkgTarget)))
);
}
export async function zipAll() {
await Promise.all(MAIN_TARGETS.map((pkgTarget) => zipRelease(pkgTarget)));
}