@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 159 lines 4.97 KB
Raw
1 import * as androidpublisher from "@distilled.cloud/gcp/androidpublisher_v3";
2 import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import { getEdit, ignoreMissing } from "./internal.ts";
11
12 export type EditProps = {
13 /**
14 * Play package name of the app (for example `com.example.app`).
15 * Immutable — changing it replaces the edit.
16 */
17 packageName: string;
18 /**
19 * Server-assigned edit id. Immutable — changing it replaces the edit.
20 * An expired edit is recreated on the next reconcile.
21 */
22 editId?: string;
23 };
24
25 export type Edit = Resource<
26 "GCP.AndroidPublisher.Edit",
27 EditProps,
28 {
29 /** Server-assigned edit id. */
30 editId: string;
31 /** Play package name. */
32 packageName: string;
33 /** Project id used when the edit was reconciled. */
34 project: string;
35 /** Unix epoch seconds when the edit expires. */
36 expiryTimeSeconds: string | undefined;
37 },
38 never,
39 Providers
40 >;
41
42 /**
43 * A Google Play app edit (`edits`).
44 *
45 * An edit is a short-lived staging session for APK, listing, and track
46 * changes. The API has no list method and no labels field, so `list`
47 * returns an empty set — nuke cannot discover orphaned edits, and they
48 * expire on their own. `packageName` and `editId` are identity. There is
49 * nothing mutable to sync: reconcile observes the edit and inserts one
50 * when it is missing or expired.
51 *
52 * ### Creating an Edit
53 * **Example:** Open an edit
54 * ```typescript
55 * const edit = yield* GCP.AndroidPublisher.Edit("Release", {
56 * packageName: "com.example.app",
57 * });
58 * ```
59 *
60 * ### Reusing an Edit
61 * **Example:** Continue an existing edit
62 * ```typescript
63 * const edit = yield* GCP.AndroidPublisher.Edit("Release", {
64 * packageName: "com.example.app",
65 * editId: "03487915625743125468", // an edit opened elsewhere
66 * });
67 * ```
68 *
69 * @resource
70 * @category AndroidPublisher
71 */
72 export const Edit = Resource<Edit>("GCP.AndroidPublisher.Edit");
73
74 export class EditNotResolved extends Data.TaggedError("GCP.AndroidPublisher.EditNotResolved")<{
75 packageName: string;
76 editId: string;
77 }> {}
78
79 const toAttrs = (edit: androidpublisher.AppEdit, packageName: string, project: string) => ({
80 editId: edit.id ?? "",
81 packageName,
82 project,
83 expiryTimeSeconds: edit.expiryTimeSeconds,
84 });
85
86 export const EditProvider = () =>
87 Provider.succeed(Edit, {
88 stables: ["editId", "packageName", "project"],
89
90 diff: Effect.fn(function* ({ news, olds, output }) {
91 if (!isResolved(news)) return undefined;
92 const previousPackage = olds?.packageName ?? output?.packageName;
93 if (previousPackage !== undefined && news.packageName !== previousPackage) {
94 return { action: "replace" as const, deleteFirst: false };
95 }
96 const previousId = olds?.editId ?? output?.editId;
97 if (previousId !== undefined && news.editId !== undefined && news.editId !== previousId) {
98 return { action: "replace" as const, deleteFirst: true };
99 }
100 return undefined;
101 }),
102
103 read: Effect.fn(function* ({ olds, output, previous }) {
104 const env = yield* GcpEnvironment.current;
105 const packageName = olds?.packageName ?? output?.packageName ?? "";
106 const editId = olds?.editId ?? output?.editId ?? "";
107 const existing = yield* getEdit(packageName, editId);
108 if (existing === undefined) return undefined;
109 const attrs = toAttrs(existing, packageName, env.project);
110 return output !== undefined ? attrs : unownedUnlessPrevious(attrs, previous, "editId");
111 }),
112
113 list: () =>
114 Effect.succeed(
115 [] as Array<{
116 editId: string;
117 packageName: string;
118 project: string;
119 expiryTimeSeconds: string | undefined;
120 }>,
121 ),
122
123 reconcile: Effect.fn(function* ({ news, output }) {
124 const env = yield* GcpEnvironment.current;
125 const packageName = news.packageName;
126 const editId = news.editId ?? output?.editId ?? "";
127
128 let current = yield* getEdit(packageName, editId);
129
130 if (current === undefined) {
131 const created = yield* androidpublisher
132 .insertEdits({
133 packageName,
134 body: {},
135 })
136 .pipe(Effect.catchTag("Conflict", () => getEdit(packageName, editId)));
137 current = created ?? undefined;
138 }
139
140 if (current === undefined) {
141 return yield* new EditNotResolved({
142 packageName,
143 editId,
144 });
145 }
146
147 return toAttrs(current, packageName, env.project);
148 }),
149
150 delete: Effect.fn(function* ({ output }) {
151 if (!output.packageName || !output.editId) return;
152 yield* ignoreMissing(
153 androidpublisher.deleteEdits({
154 packageName: output.packageName,
155 editId: output.editId,
156 }),
157 );
158 }),
159 });