forked from yaagl/yet-another-anime-game-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-app.js
296 lines (285 loc) · 8.21 KB
/
build-app.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const execa = require("execa");
const fs = require("fs-extra");
const path = require("path");
const { rimraf } = require("rimraf");
const { IconIcns } = require("@shockpkg/icon-encoder");
(async () => {
const icns = new IconIcns();
const raw = true;
await execa("cp", ["neutralino.config.json", "neutralino.config.json.bak"]);
// build done read neutralino.config.js file
const config = await fs.readJSON(
path.resolve(process.cwd(), "neutralino.config.json")
);
let bundleId;
let appDistributionName;
switch (process.env["YAAGL_CHANNEL_CLIENT"]) {
case "hk4ecn":
bundleId = config.applicationId;
appDistributionName = config.cli.binaryName;
break;
case "hk4eos":
bundleId = config.applicationId + ".os";
appDistributionName = config.cli.binaryName + " OS";
break;
case "hk4euniversal":
bundleId = config.applicationId + ".uni";
appDistributionName = config.cli.binaryName + " Uni";
break;
case "hkrpgcn":
bundleId = config.applicationId + ".hkrpg.cn";
appDistributionName = config.cli.binaryName + " HSR";
config.modes.window.icon = "/src/icons/March7th.cr.png";
break;
case "hkrpgos":
bundleId = config.applicationId + ".hkrpg.os";
appDistributionName = config.cli.binaryName + " HSR OS";
config.modes.window.icon = "/src/icons/March7th.cr.png";
break;
case "bh3glb":
bundleId = config.applicationId + ".bh3.glb";
appDistributionName = config.cli.binaryName + " Honkai Global";
config.modes.window.icon = "/src/icons/Elysia.cr.png";
break;
default:
throw new Error("YAAGL_CHANNEL_CLIENT env required");
}
await fs.writeJSON(
path.resolve(process.cwd(), "neutralino.config.json"),
config
);
try {
await execa("pnpm", ["exec", "tsc"]); // do typecheck first
await execa("rm", ["-rf", "./.tmp"]);
await execa("pnpm", ["exec", "vite", "build"]);
await execa("cp", ["./neutralino.js", "./dist/neutralino.js"]);
// run neu build command
await execa("pnpm", ["exec", "neu", "build"]);
} finally {
await execa("mv", [
"-f",
"neutralino.config.json.bak",
"neutralino.config.json",
]);
}
const appname = config.cli.binaryName;
const binaryName = `${config.cli.binaryName}-mac_x64`;
// read package.json
const pkg = await fs.readJSON(path.resolve(process.cwd(), "package.json"));
// remove old app folder
await rimraf(path.resolve(process.cwd(), `${appDistributionName}.app`));
// create app folder
await fs.mkdir(path.resolve(process.cwd(), `${appDistributionName}.app`));
await fs.mkdir(
path.resolve(process.cwd(), `${appDistributionName}.app`, "Contents")
);
await fs.mkdir(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS"
)
);
await fs.mkdir(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"Resources"
)
);
await fs.mkdir(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"Resources",
".storage"
)
);
// move binary to app folder
await fs.move(
path.resolve(process.cwd(), "dist", appname, binaryName),
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS",
binaryName
)
);
await fs.rename(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS",
binaryName
),
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS",
appname
)
);
// move res.neu or resources.neu to app folder
const resources = fs.readdirSync(
path.resolve(process.cwd(), "dist", appname)
);
const resourcesFile = resources.find(file => /res(ources)?/.test(file));
await fs.copy(
path.resolve(process.cwd(), "dist", appname, resourcesFile),
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"Resources",
resourcesFile
)
);
// check if file exists
if (fs.existsSync(path.join(process.cwd(), config.modes.window.icon))) {
const iconFile = await fs.readFile(
path.join(process.cwd(), config.modes.window.icon)
);
icns.addFromPng(iconFile, ["ic09"], raw);
// icns.addFromPng(iconFile, ['ic07'], raw);
// icns.addFromPng(iconFile, ['ic08'], raw);
// icns.addFromPng(iconFile, ['ic04'], raw);
// icns.addFromPng(iconFile, ['ic09'], raw);
// icns.addFromPng(iconFile, ['ic05'], raw);
// icns.addFromPng(iconFile, ['ic12'], raw);
// icns.addFromPng(iconFile, ['ic13'], raw);
// icns.addFromPng(iconFile, ['ic14'], raw);
// icns.addFromPng(iconFile, ['ic10'], raw);
// icns.addFromPng(iconFile, ['ic11'], raw);
}
// save icns file
await fs.writeFile(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"Resources",
"icon.icns"
),
icns.encode()
);
// create an empty icon file in the app folder
await fs.ensureFile(
path.resolve(process.cwd(), `${appDistributionName}.app`, "Icon")
);
//
await fs.writeFile(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS",
"parameterized"
),
`#!/usr/bin/env bash
SCRIPT_DIR="$( cd -- "$( dirname -- "\${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
APST_DIR="$HOME/Library/Application Support/${appDistributionName}"
echo $APST_DIR
mkdir -p "$APST_DIR"
CONTENTS_DIR="$(dirname "$SCRIPT_DIR")"
rsync -rlptu "$CONTENTS_DIR/Resources/." "$APST_DIR"
cd "$APST_DIR"
PATH_LAUNCH="$(dirname "$CONTENTS_DIR")" exec "$SCRIPT_DIR/${appname}" --path="$APST_DIR"`
);
await fs.chmod(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS",
"parameterized"
),
0o755
);
await fs.chmod(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"MacOS",
appname
),
0o755
);
// copy sidecar
const sidecarDst = path.resolve(
process.cwd(),
`${appDistributionName}.app`,
`Contents`,
`Resources`,
`sidecar`
);
await fs.copy(path.resolve(process.cwd(), `sidecar`), sidecarDst, {
preserveTimestamps: true,
});
(async function getFiles(dir) {
const dirents = await fs.readdir(dir, { withFileTypes: true });
await Promise.all(
dirents.map(dirent => {
const res = path.resolve(dir, dirent.name);
return dirent.isDirectory()
? getFiles(res)
: dirent.isFile()
? dirent.name.split(".").length == 1
? fs.chmod(res, 0o755).then(() => {
console.log("chmod +x " + res);
})
: Promise.resolve()
: Promise.resolve();
})
);
})(sidecarDst);
// chmod executable
// create info.plist file
await fs.writeFile(
path.resolve(
process.cwd(),
`${appDistributionName}.app`,
"Contents",
"info.plist"
),
`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSHighResolutionCapable</key>
<true/>
<key>CFBundleExecutable</key>
<string>parameterized</string>
<key>CFBundleIconFile</key>
<string>icon.icns</string>
<key>CFBundleIdentifier</key>
<string>${bundleId}</string>
<key>CFBundleName</key>
<string>${config.modes.window.title}</string>
<key>CFBundleDisplayName</key>
<string>${config.modes.window.title}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleVersion</key>
<string>${config.version}</string>
<key>CFBundleShortVersionString</key>
<string>${config.version}</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2023 3Shain.</string>
<key>LSMinimumSystemVersion</key>
<string>10.13.0</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>`
);
})();