@codextheme/cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,4 @@
1
+ CodexTheme CLI
2
+ Copyright 2026 CodexTheme contributors
3
+
4
+ This product includes software developed by CodeDrobe contributors under the Apache License 2.0 through the @codextheme/runtime dependency. See the repository UPSTREAM.md and the runtime package NOTICE for provenance and modification details.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @codextheme/cli
2
+
3
+ Fixed-version, one-command curated themes for Codex Desktop on macOS.
4
+
5
+ ```sh
6
+ npx --yes @codextheme/cli@0.1.0 apply midnight-circuit
7
+ ```
8
+
9
+ The package bundles exactly three original themes and the exact runtime dependency required to apply them. It does not download themes, run install scripts, use `sudo`, or modify the Codex application bundle.
10
+
11
+ After restarting Codex, reapply the active theme with:
12
+
13
+ ```sh
14
+ npx --yes @codextheme/cli@0.1.0 reapply
15
+ ```
16
+
17
+ Restore the official appearance with:
18
+
19
+ ```sh
20
+ npx --yes @codextheme/cli@0.1.0 restore
21
+ ```
22
+
23
+ Requirements: macOS, Node.js 22.4+, and Codex Desktop. A running Codex process is never closed or reopened without an explicit `y` confirmation.
24
+
25
+ CodexTheme is an independent project and is not affiliated with or endorsed by OpenAI.
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@codextheme/cli",
3
+ "version": "0.1.0",
4
+ "description": "One-command curated themes for Codex Desktop on macOS.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "bin": {
8
+ "codextheme": "src/bin.mjs"
9
+ },
10
+ "files": [
11
+ "src",
12
+ "themes",
13
+ "README.md",
14
+ "LICENSE",
15
+ "NOTICE"
16
+ ],
17
+ "scripts": {
18
+ "test": "node --test"
19
+ },
20
+ "dependencies": {
21
+ "@codextheme/runtime": "0.1.0"
22
+ },
23
+ "engines": {
24
+ "node": ">=22.4"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/kRadius/codextheme.git",
29
+ "directory": "packages/cli"
30
+ },
31
+ "homepage": "https://codextheme.tech",
32
+ "bugs": {
33
+ "url": "https://github.com/kRadius/codextheme/issues"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "registry": "https://registry.npmjs.org"
38
+ }
39
+ }
package/src/bin.mjs ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+
3
+ const [major, minor] = process.versions.node.split(".").map(Number);
4
+ if (major < 22 || (major === 22 && minor < 4)) {
5
+ console.error("E_NODE_VERSION: CodexTheme 需要 Node.js 22.4 或更高版本。");
6
+ process.exitCode = 1;
7
+ } else {
8
+ const { run } = await import("./main.mjs");
9
+ process.exitCode = await run(process.argv.slice(2));
10
+ }
@@ -0,0 +1,17 @@
1
+ import { fileURLToPath } from "node:url";
2
+
3
+ export const CATALOG = Object.freeze({
4
+ "midnight-circuit": Object.freeze({ slug: "midnight-circuit", name: "Midnight Circuit" }),
5
+ "crimson-eclipse": Object.freeze({ slug: "crimson-eclipse", name: "Crimson Eclipse" }),
6
+ "aurora-glass": Object.freeze({ slug: "aurora-glass", name: "Aurora Glass" }),
7
+ });
8
+
9
+ export function getCatalogEntry(slug) {
10
+ return CATALOG[slug] ?? null;
11
+ }
12
+
13
+ export function themeFilename(slug) {
14
+ const entry = getCatalogEntry(slug);
15
+ if (!entry) return null;
16
+ return fileURLToPath(new URL(`../themes/${entry.slug}.codextheme-theme`, import.meta.url));
17
+ }
@@ -0,0 +1,107 @@
1
+ export class CliError extends Error {
2
+ constructor(code, message) {
3
+ super(message);
4
+ this.name = "CliError";
5
+ this.code = code;
6
+ }
7
+ }
8
+
9
+ function mappedError(error) {
10
+ if (error instanceof CliError || String(error?.code ?? "").startsWith("E_")) return error;
11
+ switch (error?.code) {
12
+ case "CODEDROBE_RESTART_REQUIRED":
13
+ return new CliError("E_RESTART_REQUIRED", "需要得到你的确认后才能重新打开 Codex。");
14
+ case "CODEDROBE_DOM_INCOMPATIBLE":
15
+ return new CliError("E_DOM_INCOMPATIBLE", "当前 Codex 界面结构与该主题暂不兼容。");
16
+ case "CODEDROBE_VERIFY_FAILED":
17
+ return new CliError("E_CORE_VERIFY", "主题应用后的完整性验证失败,已尝试回滚。");
18
+ default:
19
+ return new CliError("E_DOM_INCOMPATIBLE", "Codex 主题运行时未能完成本次操作。");
20
+ }
21
+ }
22
+
23
+ function isVerified(result) {
24
+ return Array.isArray(result?.targets)
25
+ && result.targets.length > 0
26
+ && result.targets.every((target) => target.result?.pass === true);
27
+ }
28
+
29
+ async function requireRestartConsent(promptRestart) {
30
+ if (!await promptRestart()) {
31
+ throw new CliError("E_RESTART_REQUIRED", "未重新打开 Codex,主题没有被应用。");
32
+ }
33
+ }
34
+
35
+ export async function applyTheme({ slug, runtime, stateStore, promptRestart, now = () => new Date() }) {
36
+ let theme;
37
+ let detection;
38
+ try {
39
+ theme = await runtime.loadTheme(slug);
40
+ detection = await runtime.detect();
41
+ } catch (error) {
42
+ throw mappedError(error);
43
+ }
44
+ if (!detection?.installed) {
45
+ throw new CliError("E_CODEX_NOT_FOUND", "没有找到已安装的 Codex Desktop。");
46
+ }
47
+
48
+ let restartExisting = false;
49
+ if (detection.running && !detection.ready) {
50
+ await requireRestartConsent(promptRestart);
51
+ restartExisting = true;
52
+ }
53
+
54
+ let result;
55
+ try {
56
+ result = await runtime.apply({ theme, restartExisting });
57
+ } catch (error) {
58
+ if (error?.code === "CODEDROBE_RESTART_REQUIRED" && !restartExisting) {
59
+ await requireRestartConsent(promptRestart);
60
+ restartExisting = true;
61
+ try {
62
+ result = await runtime.apply({ theme, restartExisting });
63
+ } catch (retryError) {
64
+ throw mappedError(retryError);
65
+ }
66
+ } else {
67
+ throw mappedError(error);
68
+ }
69
+ }
70
+
71
+ if (!isVerified(result)) {
72
+ throw new CliError("E_CORE_VERIFY", "主题应用后的完整性验证失败,未保存活动状态。");
73
+ }
74
+
75
+ const appliedAt = now().toISOString();
76
+ await stateStore.write({ schemaVersion: 1, themeSlug: slug, appliedAt });
77
+ return { theme, result, appliedAt };
78
+ }
79
+
80
+ export async function reapplyTheme(options) {
81
+ const state = await options.stateStore.read();
82
+ if (!state) throw new CliError("E_USAGE", "没有可重新应用的活动主题,请先从主题页复制 apply 命令。");
83
+ return applyTheme({ ...options, slug: state.themeSlug });
84
+ }
85
+
86
+ const RENDERER_ABSENT_CODES = new Set([
87
+ "CODEDROBE_TARGET_TIMEOUT",
88
+ "ECONNREFUSED",
89
+ ]);
90
+
91
+ export async function restoreTheme({ runtime, stateStore }) {
92
+ let result;
93
+ try {
94
+ result = await runtime.restore();
95
+ } catch {
96
+ throw new CliError("E_RESTORE_FAILED", "无法完整恢复 Codex 官方外观,活动状态已保留。");
97
+ }
98
+ const rendererComplete = result?.renderer?.restored === true
99
+ || RENDERER_ABSENT_CODES.has(result?.renderer?.code);
100
+ const hostComplete = result?.host?.restored === true
101
+ || result?.host?.reason === "missing-backup";
102
+ if (!rendererComplete || !hostComplete) {
103
+ throw new CliError("E_RESTORE_FAILED", "无法完整恢复 Codex 官方外观,活动状态已保留。");
104
+ }
105
+ await stateStore.remove();
106
+ return result;
107
+ }
package/src/main.mjs ADDED
@@ -0,0 +1,94 @@
1
+ import { CATALOG, getCatalogEntry } from "./catalog.mjs";
2
+ import { applyTheme, CliError, reapplyTheme, restoreTheme } from "./lifecycle.mjs";
3
+ import { confirmRestart } from "./prompt.mjs";
4
+ import { runtime as productionRuntime } from "./runtime.mjs";
5
+ import { createStateStore } from "./state.mjs";
6
+
7
+ export const VERSION = "0.1.0";
8
+ const REAPPLY = "npx --yes @codextheme/cli@0.1.0 reapply";
9
+ const RESTORE = "npx --yes @codextheme/cli@0.1.0 restore";
10
+
11
+ const HELP = `CodexTheme ${VERSION}
12
+
13
+ 用法:
14
+ codextheme apply <theme>
15
+ codextheme reapply
16
+ codextheme restore
17
+
18
+ 首发主题:${Object.keys(CATALOG).join(", ")}
19
+ `;
20
+
21
+ function usage(message) {
22
+ return new CliError("E_USAGE", message);
23
+ }
24
+
25
+ function safePublicError(error) {
26
+ if (error?.code === "E_USAGE") return { code: "E_USAGE", message: error.message, exitCode: 2 };
27
+ const allowed = new Set([
28
+ "E_NODE_VERSION",
29
+ "E_PLATFORM",
30
+ "E_CODEX_NOT_FOUND",
31
+ "E_RESTART_REQUIRED",
32
+ "E_DOM_INCOMPATIBLE",
33
+ "E_CORE_VERIFY",
34
+ "E_RESTORE_FAILED",
35
+ ]);
36
+ if (allowed.has(error?.code)) return { code: error.code, message: error.message, exitCode: 1 };
37
+ return { code: "E_DOM_INCOMPATIBLE", message: "CodexTheme 未能完成本次操作。", exitCode: 1 };
38
+ }
39
+
40
+ function validateCommand(argv) {
41
+ if (argv.length === 2 && argv[0] === "apply") {
42
+ const theme = getCatalogEntry(argv[1]);
43
+ if (!theme) throw usage("未知主题。请从 codextheme.tech 复制完整命令。");
44
+ return { command: "apply", slug: theme.slug };
45
+ }
46
+ if (argv.length === 1 && argv[0] === "reapply") return { command: "reapply" };
47
+ if (argv.length === 1 && argv[0] === "restore") return { command: "restore" };
48
+ throw usage("参数无效。运行 codextheme --help 查看用法。");
49
+ }
50
+
51
+ export async function run(argv, dependencies = {}) {
52
+ const stdout = dependencies.stdout ?? process.stdout;
53
+ const stderr = dependencies.stderr ?? process.stderr;
54
+
55
+ if (argv.length === 1 && ["--version", "-v"].includes(argv[0])) {
56
+ stdout.write(`${VERSION}\n`);
57
+ return 0;
58
+ }
59
+ if (!argv.length || (argv.length === 1 && ["--help", "-h"].includes(argv[0]))) {
60
+ stdout.write(HELP);
61
+ return 0;
62
+ }
63
+
64
+ try {
65
+ const parsed = validateCommand(argv);
66
+ if ((dependencies.platform ?? process.platform) !== "darwin") {
67
+ throw new CliError("E_PLATFORM", "0.1.0 首发版仅支持 macOS 上的 Codex Desktop。");
68
+ }
69
+ const services = {
70
+ runtime: dependencies.runtime ?? productionRuntime,
71
+ stateStore: dependencies.stateStore ?? createStateStore(),
72
+ promptRestart: dependencies.promptRestart ?? (() => confirmRestart()),
73
+ now: dependencies.now ?? (() => new Date()),
74
+ };
75
+
76
+ if (parsed.command === "apply") {
77
+ const result = await applyTheme({ ...services, slug: parsed.slug });
78
+ const name = result.theme.theme?.displayName?.split(" / ")[0] ?? getCatalogEntry(parsed.slug).name;
79
+ stdout.write(`✓ ${name} 主题已应用并通过验证\n重开 Codex 后运行:${REAPPLY}\n恢复官方外观:${RESTORE}\n`);
80
+ } else if (parsed.command === "reapply") {
81
+ const result = await reapplyTheme(services);
82
+ const name = result.theme.theme?.displayName?.split(" / ")[0] ?? "当前";
83
+ stdout.write(`✓ ${name} 主题已重新应用并通过验证\n恢复官方外观:${RESTORE}\n`);
84
+ } else {
85
+ await restoreTheme(services);
86
+ stdout.write("✓ 已恢复 Codex 官方外观\n");
87
+ }
88
+ return 0;
89
+ } catch (error) {
90
+ const safe = safePublicError(error);
91
+ stderr.write(`${safe.code}: ${safe.message}\n`);
92
+ return safe.exitCode;
93
+ }
94
+ }
package/src/prompt.mjs ADDED
@@ -0,0 +1,14 @@
1
+ import { createInterface } from "node:readline/promises";
2
+
3
+ const MESSAGE = "Codex 需要重新打开才能完整应用主题。未发送的输入可能丢失。\n现在重新打开 Codex?[y/N] ";
4
+
5
+ export async function confirmRestart({ input = process.stdin, output = process.stdout } = {}) {
6
+ if (!input?.isTTY || !output?.isTTY) return false;
7
+ const readline = createInterface({ input, output });
8
+ try {
9
+ const answer = await readline.question(MESSAGE);
10
+ return answer.trim().toLowerCase() === "y";
11
+ } finally {
12
+ readline.close();
13
+ }
14
+ }
@@ -0,0 +1,56 @@
1
+ import {
2
+ applySkin,
3
+ discoverApp,
4
+ findRunningPids,
5
+ findTargets,
6
+ getAdapter,
7
+ lintThemePackage,
8
+ readThemePackage,
9
+ resolveThemeTarget,
10
+ restoreSkin,
11
+ } from "@codextheme/runtime";
12
+ import { themeFilename } from "./catalog.mjs";
13
+
14
+ const adapter = getAdapter("codex");
15
+
16
+ export const runtime = {
17
+ async loadTheme(slug) {
18
+ const filename = themeFilename(slug);
19
+ if (!filename) throw Object.assign(new Error("Unknown theme."), { code: "E_USAGE" });
20
+ const bundle = await readThemePackage(filename);
21
+ const warnings = lintThemePackage(bundle);
22
+ if (warnings.length) {
23
+ throw Object.assign(new Error("Bundled theme failed safety lint."), { code: "E_DOM_INCOMPATIBLE" });
24
+ }
25
+ return resolveThemeTarget(bundle, adapter.id);
26
+ },
27
+
28
+ async detect() {
29
+ const discovered = await discoverApp(adapter);
30
+ if (!discovered) return { installed: false, running: false, ready: false };
31
+ let ready = false;
32
+ try {
33
+ ready = (await findTargets(adapter, adapter.defaultPort)).length > 0;
34
+ } catch { /* An absent loopback endpoint is an expected detection result. */ }
35
+ let running = ready;
36
+ if (!running) {
37
+ try {
38
+ running = (await findRunningPids(adapter, process.platform, discovered.executable)).length > 0;
39
+ } catch { /* applySkin still performs the authoritative launch check. */ }
40
+ }
41
+ return { installed: true, running, ready };
42
+ },
43
+
44
+ async apply({ theme, restartExisting }) {
45
+ return applySkin({
46
+ adapter,
47
+ targetTheme: theme,
48
+ launch: true,
49
+ restartExisting,
50
+ });
51
+ },
52
+
53
+ async restore() {
54
+ return restoreSkin({ adapter });
55
+ },
56
+ };
package/src/state.mjs ADDED
@@ -0,0 +1,53 @@
1
+ import fs from "node:fs/promises";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+
5
+ function cleanState(value) {
6
+ if (
7
+ value?.schemaVersion !== 1
8
+ || typeof value.themeSlug !== "string"
9
+ || !value.themeSlug
10
+ || typeof value.appliedAt !== "string"
11
+ || !value.appliedAt
12
+ ) {
13
+ throw Object.assign(new Error("Invalid CodexTheme state."), { code: "E_USAGE" });
14
+ }
15
+ return {
16
+ schemaVersion: 1,
17
+ themeSlug: value.themeSlug,
18
+ appliedAt: value.appliedAt,
19
+ };
20
+ }
21
+
22
+ export function createStateStore({ home = os.homedir(), fsApi = fs } = {}) {
23
+ const directory = path.join(home, "Library", "Application Support", "codextheme");
24
+ const filename = path.join(directory, "state.json");
25
+
26
+ return {
27
+ filename,
28
+ async read() {
29
+ try {
30
+ return cleanState(JSON.parse(await fsApi.readFile(filename, "utf8")));
31
+ } catch (error) {
32
+ if (error.code === "ENOENT") return null;
33
+ throw error;
34
+ }
35
+ },
36
+ async write(value) {
37
+ const state = cleanState(value);
38
+ await fsApi.mkdir(directory, { recursive: true, mode: 0o700 });
39
+ const temporary = path.join(directory, `.state.${process.pid}.${Date.now()}.tmp`);
40
+ try {
41
+ await fsApi.writeFile(temporary, `${JSON.stringify(state, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
42
+ await fsApi.rename(temporary, filename);
43
+ await fsApi.chmod(filename, 0o600);
44
+ } catch (error) {
45
+ await fsApi.rm(temporary, { force: true }).catch(() => {});
46
+ throw error;
47
+ }
48
+ },
49
+ async remove() {
50
+ await fsApi.rm(filename, { force: true });
51
+ },
52
+ };
53
+ }