forked from Specy/genshin-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildApp.js
75 lines (70 loc) · 3.12 KB
/
buildApp.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
import fse from 'fs-extra'
import urlJoin from 'url-join'
import clc from "cli-color";
import { execSync } from 'child_process'
const skyPath = './src/appData/sky'
const genshinPath = './src/appData/genshin'
const publicPath = './public'
const chosenApp = process.argv[2]
const date = new Date()
const SW_VERSION = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}_${date.getHours()}-${date.getMinutes()}`
const PATH_NAMES = {
Sky: "skyMusic",
Genshin: "genshinMusic"
}
if (!['Genshin', 'Sky', "All"].includes(chosenApp)) {
console.error('Please specify an app name [Sky / Genshin / All]')
process.exit(1)
}
async function execute() {
const toBuild = chosenApp === "All" ? ['Sky', 'Genshin'] : [chosenApp]
try {
for (const app of toBuild) {
const basePath = Boolean(process.argv[3]) ? `/${PATH_NAMES[app]}` : ""
console.log(clc.bold.yellow(`Building ${app}...`))
await fse.copy(app === "Sky" ? skyPath : genshinPath, publicPath, { overwrite: true })
await updateManifest(basePath)
if (process.platform === 'win32') {
console.log(clc.italic("Building on windows"))
execSync(
`set NEXT_PUBLIC_APP_NAME=${app}&& set NEXT_PUBLIC_SW_VERSION=${SW_VERSION}&& set BUILD_PATH=./build/${PATH_NAMES[app]}&& set NEXT_PUBLIC_BASE_PATH=${basePath}&& npm run build`,
{ stdio: 'inherit' }
)
} else {
console.log(clc.italic("Building on Linux"))
execSync(
`NEXT_PUBLIC_APP_NAME=${app} BUILD_PATH=./build/${PATH_NAMES[app]} NEXT_PUBLIC_SW_VERSION=${SW_VERSION} NEXT_PUBLIC_BASE_PATH=${basePath} npm run build`,
{ stdio: 'inherit' }
)
}
console.log(clc.green(`${app} build complete \n`))
}
console.log(clc.bold.green("Build complete \n"))
process.exit(0)
} catch (e) {
console.log(clc.red("[Error]: There was an error building"))
console.error(e)
process.exit(1)
}
}
async function updateManifest(basePath) {
try {
const manifest = await fse.readJson('./public/manifest.json')
if (manifest.icons) manifest.icons = manifest.icons.map(icon => ({ ...icon, src: urlJoin(basePath, icon.src) }))
if (manifest.start_url) manifest.start_url = basePath || "."
if (manifest.screenshots) manifest.screenshots = manifest.screenshots.map(screenshot => ({ ...screenshot, src: urlJoin(basePath, screenshot.src) }))
if (manifest.file_handlers) {
manifest.file_handlers = manifest.file_handlers.map(handler => {
const icons = handler.icons.map(icon => ({ ...icon, src: urlJoin(basePath, icon.src) }))
const action = basePath || "."
return { ...handler, icons, action }
})
}
await fse.writeFile('./public/manifest.json', JSON.stringify(manifest, null, 2))
} catch (e) {
console.log(clc.red("[Error]: There was an error updating the manifest"))
console.error(e)
process.exit(1)
}
}
execute()