-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.mjs
More file actions
117 lines (108 loc) · 3.77 KB
/
Copy pathinstall.mjs
File metadata and controls
117 lines (108 loc) · 3.77 KB
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
import { execFile as execFileCb } from "node:child_process";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { fileURLToPath } from "node:url";
const execFile = promisify(execFileCb);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(__dirname, "app", "CodexUsageHalo.swift");
const menuIconSourcePath = path.join(__dirname, "assets", "menu-icon", "status-icon.png");
const appPath = path.join(os.homedir(), "Applications", "Codex Pet Meter.app");
const contentsPath = path.join(appPath, "Contents");
const macosPath = path.join(contentsPath, "MacOS");
const resourcesPath = path.join(contentsPath, "Resources");
const menuIconResourcePath = path.join(resourcesPath, "status-icon.png");
const executablePath = path.join(macosPath, "CodexPetMeter");
const plistPath = path.join(contentsPath, "Info.plist");
const appVersion = "0.3.0";
function plist() {
return `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>CodexPetMeter</string>
<key>CFBundleIdentifier</key>
<string>com.rainsday.codex-pet-meter</string>
<key>CFBundleName</key>
<string>Codex Pet Meter</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>${appVersion}</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>13.0</string>
<key>LSUIElement</key>
<true/>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
`;
}
async function run(command, args, options = {}) {
try {
return await execFile(command, args, { maxBuffer: 1024 * 1024 * 10, ...options });
} catch (error) {
if (options.ignoreFailure) return error;
throw error;
}
}
async function install() {
await stop();
await fs.mkdir(macosPath, { recursive: true });
await fs.mkdir(resourcesPath, { recursive: true });
await fs.writeFile(plistPath, plist(), "utf8");
await fs.rm(path.join(resourcesPath, "WarningHaloShards"), { recursive: true, force: true });
await fs.rm(path.join(resourcesPath, "BreathingAura"), { recursive: true, force: true });
await fs.copyFile(menuIconSourcePath, menuIconResourcePath);
await run("/usr/bin/swiftc", [
sourcePath,
"-O",
"-framework",
"Cocoa",
"-o",
executablePath,
]);
await fs.chmod(executablePath, 0o755);
await run("/usr/bin/codesign", ["--force", "--sign", "-", appPath], { ignoreFailure: true });
await start();
console.log(`Installed ${appPath}`);
}
async function start() {
await stop();
await run("/usr/bin/open", ["-n", appPath]);
console.log(`Started ${appPath}`);
}
async function status() {
const result = await run("/usr/bin/pgrep", ["-fl", "CodexPetMeter"], { ignoreFailure: true });
const stdout = "stdout" in result ? result.stdout.trim() : "";
console.log(stdout || "Codex Pet Meter is not running.");
}
async function stop() {
await run("/usr/bin/pkill", ["-f", "CodexPetMeter"], { ignoreFailure: true });
}
async function uninstall() {
await stop();
await fs.rm(appPath, { recursive: true, force: true });
console.log(`Removed ${appPath}`);
}
const command = process.argv[2] || "install";
if (command === "install") await install();
else if (command === "start") await start();
else if (command === "status") await status();
else if (command === "uninstall") await uninstall();
else {
console.error("Usage: node install.mjs install|start|status|uninstall");
process.exitCode = 1;
}