/
LibUiFramework.ts
127 lines (108 loc) · 4.58 KB
/
LibUiFramework.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
import { emptyDir } from "fs-extra"
import { mkdir, chmod, rename, writeFile } from "fs/promises"
import * as path from "path"
import { executeAppBuilder } from "builder-util"
import { PackContext } from "../configuration"
import { Platform } from "../core"
import { Framework, PrepareApplicationStageDirectoryOptions } from "../Framework"
import { LinuxPackager } from "../linuxPackager"
import MacPackager from "../macPackager"
import { executeAppBuilderAndWriteJson } from "../util/appBuilder"
export class LibUiFramework implements Framework {
readonly name: string = "libui"
// noinspection JSUnusedGlobalSymbols
readonly macOsDefaultTargets = ["dmg"]
readonly defaultAppIdPrefix: string = "com.libui."
// noinspection JSUnusedGlobalSymbols
readonly isCopyElevateHelper = false
// noinspection JSUnusedGlobalSymbols
readonly isNpmRebuildRequired = false
constructor(readonly version: string, readonly distMacOsAppName: string, protected readonly isUseLaunchUi: boolean) {}
async prepareApplicationStageDirectory(options: PrepareApplicationStageDirectoryOptions) {
await emptyDir(options.appOutDir)
const packager = options.packager
const platform = packager.platform
if (this.isUseLaunchUiForPlatform(platform)) {
const appOutDir = options.appOutDir
await executeAppBuilder([
"proton-native",
"--node-version",
this.version,
"--use-launch-ui",
"--platform",
platform.nodeName,
"--arch",
options.arch,
"--stage",
appOutDir,
"--executable",
`${packager.appInfo.productFilename}${platform === Platform.WINDOWS ? ".exe" : ""}`,
])
return
}
if (platform === Platform.MAC) {
await this.prepareMacosApplicationStageDirectory(packager as MacPackager, options)
} else if (platform === Platform.LINUX) {
await this.prepareLinuxApplicationStageDirectory(options)
}
}
private async prepareMacosApplicationStageDirectory(packager: MacPackager, options: PrepareApplicationStageDirectoryOptions) {
const appContentsDir = path.join(options.appOutDir, this.distMacOsAppName, "Contents")
await mkdir(path.join(appContentsDir, "Resources"), { recursive: true })
await mkdir(path.join(appContentsDir, "MacOS"), { recursive: true })
await executeAppBuilder(["proton-native", "--node-version", this.version, "--platform", "darwin", "--stage", path.join(appContentsDir, "MacOS")])
const appPlist: any = {
// https://github.com/albe-rosado/create-proton-app/issues/13
NSHighResolutionCapable: true,
}
await packager.applyCommonInfo(appPlist, appContentsDir)
await Promise.all([
executeAppBuilderAndWriteJson(["encode-plist"], { [path.join(appContentsDir, "Info.plist")]: appPlist }),
writeExecutableMain(
path.join(appContentsDir, "MacOS", appPlist.CFBundleExecutable),
`#!/bin/sh
DIR=$(dirname "$0")
"$DIR/node" "$DIR/../Resources/app/${options.packager.info.metadata.main || "index.js"}"
`
),
])
}
private async prepareLinuxApplicationStageDirectory(options: PrepareApplicationStageDirectoryOptions) {
const appOutDir = options.appOutDir
await executeAppBuilder(["proton-native", "--node-version", this.version, "--platform", "linux", "--arch", options.arch, "--stage", appOutDir])
const mainPath = path.join(appOutDir, (options.packager as LinuxPackager).executableName)
await writeExecutableMain(
mainPath,
`#!/bin/sh
DIR=$(dirname "$0")
"$DIR/node" "$DIR/app/${options.packager.info.metadata.main || "index.js"}"
`
)
}
async afterPack(context: PackContext) {
const packager = context.packager
if (!this.isUseLaunchUiForPlatform(packager.platform)) {
return
}
// LaunchUI requires main.js, rename if need
const userMain = packager.info.metadata.main || "index.js"
if (userMain === "main.js") {
return
}
await rename(path.join(context.appOutDir, "app", userMain), path.join(context.appOutDir, "app", "main.js"))
}
getMainFile(platform: Platform): string | null {
return this.isUseLaunchUiForPlatform(platform) ? "main.js" : null
}
private isUseLaunchUiForPlatform(platform: Platform) {
return platform === Platform.WINDOWS || (this.isUseLaunchUi && platform === Platform.LINUX)
}
getExcludedDependencies(platform: Platform): Array<string> | null {
// part of launchui
return this.isUseLaunchUiForPlatform(platform) ? ["libui-node"] : null
}
}
async function writeExecutableMain(file: string, content: string) {
await writeFile(file, content, { mode: 0o755 })
await chmod(file, 0o755)
}