scribase 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.
Files changed (162) hide show
  1. package/README.md +220 -0
  2. package/dist/cli/credentials.d.ts +35 -0
  3. package/dist/cli/credentials.d.ts.map +1 -0
  4. package/dist/cli/credentials.js +125 -0
  5. package/dist/cli/credentials.js.map +1 -0
  6. package/dist/cli/envfile.d.ts +20 -0
  7. package/dist/cli/envfile.d.ts.map +1 -0
  8. package/dist/cli/envfile.js +86 -0
  9. package/dist/cli/envfile.js.map +1 -0
  10. package/dist/cli/framework.d.ts +45 -0
  11. package/dist/cli/framework.d.ts.map +1 -0
  12. package/dist/cli/framework.js +107 -0
  13. package/dist/cli/framework.js.map +1 -0
  14. package/dist/cli/index.d.ts +8 -0
  15. package/dist/cli/index.d.ts.map +1 -0
  16. package/dist/cli/index.js +239 -0
  17. package/dist/cli/index.js.map +1 -0
  18. package/dist/cli/init.d.ts +118 -0
  19. package/dist/cli/init.d.ts.map +1 -0
  20. package/dist/cli/init.js +455 -0
  21. package/dist/cli/init.js.map +1 -0
  22. package/dist/cli/login.d.ts +104 -0
  23. package/dist/cli/login.d.ts.map +1 -0
  24. package/dist/cli/login.js +306 -0
  25. package/dist/cli/login.js.map +1 -0
  26. package/dist/cli/pow.d.ts +9 -0
  27. package/dist/cli/pow.d.ts.map +1 -0
  28. package/dist/cli/pow.js +31 -0
  29. package/dist/cli/pow.js.map +1 -0
  30. package/dist/cli/vercel.d.ts +92 -0
  31. package/dist/cli/vercel.d.ts.map +1 -0
  32. package/dist/cli/vercel.js +230 -0
  33. package/dist/cli/vercel.js.map +1 -0
  34. package/dist/client.d.ts +137 -0
  35. package/dist/client.d.ts.map +1 -0
  36. package/dist/client.js +311 -0
  37. package/dist/client.js.map +1 -0
  38. package/dist/errors.d.ts +79 -0
  39. package/dist/errors.d.ts.map +1 -0
  40. package/dist/errors.js +139 -0
  41. package/dist/errors.js.map +1 -0
  42. package/dist/evidence.d.ts +78 -0
  43. package/dist/evidence.d.ts.map +1 -0
  44. package/dist/evidence.js +36 -0
  45. package/dist/evidence.js.map +1 -0
  46. package/dist/idempotency.d.ts +24 -0
  47. package/dist/idempotency.d.ts.map +1 -0
  48. package/dist/idempotency.js +59 -0
  49. package/dist/idempotency.js.map +1 -0
  50. package/dist/importers/firebase.d.ts +101 -0
  51. package/dist/importers/firebase.d.ts.map +1 -0
  52. package/dist/importers/firebase.js +251 -0
  53. package/dist/importers/firebase.js.map +1 -0
  54. package/dist/importers/index.d.ts +11 -0
  55. package/dist/importers/index.d.ts.map +1 -0
  56. package/dist/importers/index.js +9 -0
  57. package/dist/importers/index.js.map +1 -0
  58. package/dist/importers/instantdb.d.ts +76 -0
  59. package/dist/importers/instantdb.d.ts.map +1 -0
  60. package/dist/importers/instantdb.js +152 -0
  61. package/dist/importers/instantdb.js.map +1 -0
  62. package/dist/index.d.ts +78 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +53 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/pagination.d.ts +33 -0
  67. package/dist/pagination.d.ts.map +1 -0
  68. package/dist/pagination.js +68 -0
  69. package/dist/pagination.js.map +1 -0
  70. package/dist/resources/account.d.ts +156 -0
  71. package/dist/resources/account.d.ts.map +1 -0
  72. package/dist/resources/account.js +114 -0
  73. package/dist/resources/account.js.map +1 -0
  74. package/dist/resources/audit.d.ts +26 -0
  75. package/dist/resources/audit.d.ts.map +1 -0
  76. package/dist/resources/audit.js +43 -0
  77. package/dist/resources/audit.js.map +1 -0
  78. package/dist/resources/auth.d.ts +149 -0
  79. package/dist/resources/auth.d.ts.map +1 -0
  80. package/dist/resources/auth.js +70 -0
  81. package/dist/resources/auth.js.map +1 -0
  82. package/dist/resources/backups.d.ts +117 -0
  83. package/dist/resources/backups.d.ts.map +1 -0
  84. package/dist/resources/backups.js +92 -0
  85. package/dist/resources/backups.js.map +1 -0
  86. package/dist/resources/branches.d.ts +151 -0
  87. package/dist/resources/branches.d.ts.map +1 -0
  88. package/dist/resources/branches.js +62 -0
  89. package/dist/resources/branches.js.map +1 -0
  90. package/dist/resources/database.d.ts +107 -0
  91. package/dist/resources/database.d.ts.map +1 -0
  92. package/dist/resources/database.js +54 -0
  93. package/dist/resources/database.js.map +1 -0
  94. package/dist/resources/environments.d.ts +106 -0
  95. package/dist/resources/environments.d.ts.map +1 -0
  96. package/dist/resources/environments.js +131 -0
  97. package/dist/resources/environments.js.map +1 -0
  98. package/dist/resources/export.d.ts +101 -0
  99. package/dist/resources/export.d.ts.map +1 -0
  100. package/dist/resources/export.js +89 -0
  101. package/dist/resources/export.js.map +1 -0
  102. package/dist/resources/functions.d.ts +75 -0
  103. package/dist/resources/functions.d.ts.map +1 -0
  104. package/dist/resources/functions.js +58 -0
  105. package/dist/resources/functions.js.map +1 -0
  106. package/dist/resources/imports.d.ts +183 -0
  107. package/dist/resources/imports.d.ts.map +1 -0
  108. package/dist/resources/imports.js +168 -0
  109. package/dist/resources/imports.js.map +1 -0
  110. package/dist/resources/insights.d.ts +50 -0
  111. package/dist/resources/insights.d.ts.map +1 -0
  112. package/dist/resources/insights.js +21 -0
  113. package/dist/resources/insights.js.map +1 -0
  114. package/dist/resources/metrics.d.ts +61 -0
  115. package/dist/resources/metrics.d.ts.map +1 -0
  116. package/dist/resources/metrics.js +23 -0
  117. package/dist/resources/metrics.js.map +1 -0
  118. package/dist/resources/migrations.d.ts +24 -0
  119. package/dist/resources/migrations.d.ts.map +1 -0
  120. package/dist/resources/migrations.js +29 -0
  121. package/dist/resources/migrations.js.map +1 -0
  122. package/dist/resources/operations.d.ts +37 -0
  123. package/dist/resources/operations.d.ts.map +1 -0
  124. package/dist/resources/operations.js +81 -0
  125. package/dist/resources/operations.js.map +1 -0
  126. package/dist/resources/organizations.d.ts +171 -0
  127. package/dist/resources/organizations.d.ts.map +1 -0
  128. package/dist/resources/organizations.js +180 -0
  129. package/dist/resources/organizations.js.map +1 -0
  130. package/dist/resources/previews.d.ts +49 -0
  131. package/dist/resources/previews.d.ts.map +1 -0
  132. package/dist/resources/previews.js +62 -0
  133. package/dist/resources/previews.js.map +1 -0
  134. package/dist/resources/projects.d.ts +73 -0
  135. package/dist/resources/projects.d.ts.map +1 -0
  136. package/dist/resources/projects.js +84 -0
  137. package/dist/resources/projects.js.map +1 -0
  138. package/dist/resources/queues.d.ts +100 -0
  139. package/dist/resources/queues.d.ts.map +1 -0
  140. package/dist/resources/queues.js +77 -0
  141. package/dist/resources/queues.js.map +1 -0
  142. package/dist/resources/realtime.d.ts +37 -0
  143. package/dist/resources/realtime.d.ts.map +1 -0
  144. package/dist/resources/realtime.js +23 -0
  145. package/dist/resources/realtime.js.map +1 -0
  146. package/dist/resources/restores.d.ts +67 -0
  147. package/dist/resources/restores.d.ts.map +1 -0
  148. package/dist/resources/restores.js +90 -0
  149. package/dist/resources/restores.js.map +1 -0
  150. package/dist/resources/storage.d.ts +50 -0
  151. package/dist/resources/storage.d.ts.map +1 -0
  152. package/dist/resources/storage.js +38 -0
  153. package/dist/resources/storage.js.map +1 -0
  154. package/dist/resources/usage.d.ts +85 -0
  155. package/dist/resources/usage.d.ts.map +1 -0
  156. package/dist/resources/usage.js +37 -0
  157. package/dist/resources/usage.js.map +1 -0
  158. package/dist/types.gen.d.ts +473 -0
  159. package/dist/types.gen.d.ts.map +1 -0
  160. package/dist/types.gen.js +8 -0
  161. package/dist/types.gen.js.map +1 -0
  162. package/package.json +53 -0
package/README.md ADDED
@@ -0,0 +1,220 @@
1
+ # scribase
2
+
3
+ The typed TypeScript client for the [Scribase](https://scribase.com) control-plane
4
+ `/v1` API — the agent-native, flat-priced, Supabase-compatible application cloud.
5
+
6
+ - **Zero runtime dependencies.** A thin, typed wrapper over `fetch`.
7
+ - **Contract-first core.** `src/types.gen.ts` is emitted from
8
+ `api/openapi.yaml` (orgs, memberships, projects, environments, operations,
9
+ audit, imports, migrations, exports). Newer surfaces (backups, restores,
10
+ usage, database, queues, functions, account, …) are typed by hand in
11
+ `src/resources/*.ts` against the exact bodies `crates/control-api` renders;
12
+ each type's doc comment names the server function it mirrors.
13
+ - **Async-operation aware.** Every mutation returns an `Operation` to poll, and
14
+ the client gives you `wait(...)` helpers so "accepted" becomes "done".
15
+ - **Idempotency + retries built in.** Mutating calls attach an
16
+ `Idempotency-Key` automatically; 429/503/network failures back off and retry.
17
+
18
+ Requires Node >= 22.13 (built-in `fetch`, `crypto.randomUUID`) or any runtime
19
+ with the Fetch and Web Crypto globals.
20
+
21
+ ## `npx scribase init`
22
+
23
+ The package also ships a `scribase` binary with one command, made for coding
24
+ agents (Claude Code, Codex, Cursor, Windsurf) and people alike:
25
+
26
+ ```bash
27
+ npx scribase init --yes --json # with your account (SCRIBASE_ACCESS_TOKEN or `scribase login`)
28
+ npx scribase init --temp --yes --json # no account: a 72-hour project with a claim URL
29
+ ```
30
+
31
+ It detects Next.js, Expo, Vite or plain Node, creates (or reuses) a project,
32
+ writes the env file with that framework's variable names
33
+ (`NEXT_PUBLIC_SUPABASE_URL`, `EXPO_PUBLIC_SUPABASE_URL`, `VITE_SUPABASE_URL`,
34
+ `SUPABASE_URL`, and the anon key; the service-role key only where it cannot
35
+ reach a client bundle), installs `@supabase/supabase-js`, writes a small
36
+ client module and prints one JSON object (`ok`, `api_url`, `env_vars`,
37
+ `client_file`, `claim_url`, `next_steps`, or `ok: false` with an error `code`
38
+ and a `next` step). Run it again and it reuses the same project.
39
+
40
+ When the folder is linked to Vercel (`.vercel/project.json` or
41
+ `VERCEL_PROJECT_ID`), init also sets the URL and keys on that Vercel project for
42
+ development, preview and production, using `VERCEL_TOKEN` or the signed-in
43
+ `vercel` CLI, and reports `vercel: { status, targets }` (`updated`,
44
+ `not_linked`, `no_credentials`, `failed`). It never fails init; `--no-vercel`
45
+ turns it off.
46
+
47
+ ## Install
48
+
49
+ The package is not on the npm registry yet. Depend on it from this repository:
50
+
51
+ ```bash
52
+ # build once (the dist/ folder is not committed)
53
+ npm --prefix packages/scribase-js install
54
+ npm --prefix packages/scribase-js run build
55
+
56
+ # then, in your app
57
+ npm install file:../path/to/scribase/packages/scribase-js
58
+ ```
59
+
60
+
61
+
62
+ ## Quick start
63
+
64
+ ```ts
65
+ import { ScribaseClient } from 'scribase';
66
+
67
+ const scribase = new ScribaseClient({
68
+ baseUrl: 'https://api.scribase.com', // your installation's management endpoint
69
+ token: process.env.SCRIBASE_ACCESS_TOKEN ?? '',
70
+ });
71
+
72
+ // Bootstrap an org (idempotent; caller becomes owner), then a project.
73
+ await scribase.organizations.put('acme', { display_name: 'Acme, Inc.' });
74
+ await scribase.projects.put('acme', 'store', { region: 'us-east', postgres_major: 17 });
75
+
76
+ // Provision production. Mutations are asynchronous.
77
+ const { operation } = await scribase.environments.create({
78
+ organization_id: 'acme',
79
+ project_id: 'store',
80
+ environment_id: 'production',
81
+ kind: 'production',
82
+ data_mode: 'snapshot',
83
+ region: 'us-east',
84
+ postgres_major: 17,
85
+ });
86
+ await scribase.operations.wait(operation.id, 'acme', {
87
+ onUpdate: (op) => console.info(`provision: ${op.state}`),
88
+ });
89
+ ```
90
+
91
+ ## Resources
92
+
93
+ Every method maps to one real route in `crates/control-api/src/http.rs`.
94
+
95
+ | Accessor | Methods → endpoints |
96
+ |---|---|
97
+ | `client.organizations` | `put`/`get` `/v1/organizations/{id}`, `update` (`PATCH`, rename), `list` `/v1/organizations`, `putMembership`/`listMemberships` `…/memberships`, `scheduleDeletion` (`DELETE`), `get`/`putNotificationSettings`, `listApiKeys`/`createApiKey`/`revokeApiKey` `…/api-keys`, `listPolicies`, `listMcpTools` |
98
+ | `client.projects` | `put`/`get` `…/projects/{id}`, `list` `…/projects`, `listDomains` `…/domains` |
99
+ | `client.environments` | `list`/`create` `/v1/environments`, `get`/`delete` `…/{env}`, `suspend`/`resume`/`renew`, `connection` (server-side only), `logs` |
100
+ | `client.previews` | built on environments: `create`/`list`/`get`/`delete`/`renew` — TTL-swept, `sanitized` by default |
101
+ | `client.backups` | `list`/`create` `…/environments/{env}/backups`, `get` `…/projects/{p}/backups/{id}?environment=`, `verify` `…/environments/{env}/backups/{id}/verify`, `restore` `…/backups/{id}/restore` |
102
+ | `client.restores` | `start` — restore-shaped form of `backups.restore`; `list`/`get` `…/environments/{env}/restores[/{id}]`, `waitForCompletion` |
103
+ | `client.export` | `create` `…/environments/{env}/exports`, `createForProject` `…/projects/{p}/export`, `list` `…/projects/{p}/exports`, `getManifest` `…/exports/{id}`, `downloadUrl`/`download` (signed `…/exports/{id}/download`) |
104
+ | `client.migrations` | `run` `…/migrations` — plan → lint → apply |
105
+ | `client.imports` | `create`/`dryRun` `/v1/imports`, `history`/`report` `…/imports` |
106
+ | `client.usage` | `get` `…/environments/{env}/usage`, `capsNearingLimit`, `billing` `…/organizations/{org}/billing` |
107
+ | `client.database` | `tables` `…/database/tables`, `query` `…/database/query` (read-only) |
108
+ | `client.queues` | `list`, `peek`, `create`/`send`/`read`/`archive`/`delete`/`purge` |
109
+ | `client.functions` | `list`, `deploy`, `invoke` |
110
+ | `client.auth` | `listUsers`, `listProviders`, `listPolicies` |
111
+ | `client.storage` / `client.realtime` / `client.metrics` / `client.insights` | read surfaces for the console |
112
+ | `client.branches` | `list`/`register`/`diff`/`merge`/`linkPullRequest` |
113
+ | `client.account` | `get`/`update` (`PATCH`) `/v1/account`, `security`, `revokeSession`/`removeFactor`, `enrollFactor`/`verifyFactor` (TOTP), `challenge` (MFA step-up), `listTokens`/`createToken`/`revokeToken` |
114
+ | `client.operations` | `get` `/v1/operations/{id}`, `wait` |
115
+ | `client.audit` | `list`, `paginate` over `…/audit-events` |
116
+
117
+ ## The two money surfaces
118
+
119
+ **Verified restore.** Backups become restorable only once the backup worker has
120
+ verified them (`status: 'available'`); the control plane refuses anything else.
121
+ A restore always lands in a *different* environment, and the finished record
122
+ carries its own evidence: relation count, RLS relation count, and the SHA-256
123
+ of the evidence document.
124
+
125
+ ```ts
126
+ import { isRestorable, restoreEvidence } from 'scribase';
127
+
128
+ const production = { organizationId: 'acme', projectId: 'store', environmentId: 'production' };
129
+ const [latest] = (await scribase.backups.list(production)).filter(isRestorable);
130
+ if (latest) {
131
+ // Restore into a scratch preview, never over production, to prove the backup.
132
+ const { restore } = await scribase.backups.restore(production, latest.backup_id, 'restore-check');
133
+ console.info(restore.id, restore.status); // 'pending' until the restore worker finishes
134
+ const proof = restoreEvidence(restore); // non-null once status === 'restored'
135
+ }
136
+ ```
137
+
138
+ **Runnable export.** No lock-in: assemble a complete, runnable stack with a
139
+ signed manifest. The call is synchronous and returns the manifest directly.
140
+
141
+ ```ts
142
+ const ref = { organizationId: 'acme', projectId: 'store', environmentId: 'production' };
143
+ const { manifest, bundleRef } = await scribase.export.create(ref, { format: 'compose' });
144
+ // `manifest.artifacts[]` carries per-file sha256; `manifest.signature` signs the
145
+ // manifest; `bundleRef` is where the control plane wrote the bundle.
146
+ ```
147
+
148
+ ## Migrate an existing project
149
+
150
+ The dry-run is the sales tool: it prints exactly what will move and what won't,
151
+ before anything is written. Acceptance is binding — existing users log in
152
+ afterward *without resetting passwords*.
153
+
154
+ ```ts
155
+ const source = {
156
+ source: 'supabase' as const,
157
+ sourceProjectRef: 'abcdefgh',
158
+ sourceConnectionString: process.env.SOURCE_DB_URL ?? '',
159
+ sourceServiceKey: process.env.SOURCE_SERVICE_KEY ?? '', // sent once, never logged
160
+ destination: { organizationId: 'acme', projectId: 'store', environmentId: 'production' },
161
+ };
162
+ const plan = await scribase.imports.dryRun(source); // nothing is written
163
+ const report = await scribase.imports.run(source); // starts it in the background, follows progress, returns the verified report
164
+ // or: const job = await scribase.imports.createFrom(source); then scribase.imports.progress(job.import_id)
165
+ ```
166
+
167
+ ## Apply migrations safely
168
+
169
+ `plan → lint → apply`. A blocking lint finding (`missing_rls`, `public_grant`,
170
+ `permissive_policy`, `no_with_check`, `security_definer`) refuses the migration
171
+ before a single statement runs.
172
+
173
+ ```ts
174
+ const ref = { organizationId: 'acme', projectId: 'store', environmentId: 'production' };
175
+ const result = await scribase.migrations.run(ref, {
176
+ migrations: [{ version: '20260921120000', name: 'add_notes', up: 'create table ...' }],
177
+ apply: true,
178
+ });
179
+ // result.plan, result.lintFindings, result.applied
180
+ ```
181
+
182
+ ## Pagination
183
+
184
+ ```ts
185
+ for await (const event of scribase.audit.paginate('acme', { limit: 200 })) {
186
+ console.info(event.sequence, event.action);
187
+ }
188
+ ```
189
+
190
+ ## Errors
191
+
192
+ Every failure is a typed subclass of `ScribaseError`. API errors carry the
193
+ parsed RFC 9457 `problem+json` body:
194
+
195
+ ```ts
196
+ import { ConflictError, QuotaExceededError } from 'scribase';
197
+
198
+ try {
199
+ await scribase.environments.create(spec);
200
+ } catch (error) {
201
+ if (error instanceof QuotaExceededError) {
202
+ // buy a capacity pack; retry after error.retryAfterSeconds
203
+ } else if (error instanceof ConflictError) {
204
+ console.error(error.problem.code, error.requestId);
205
+ }
206
+ }
207
+ ```
208
+
209
+ ## Regenerating types
210
+
211
+ `src/types.gen.ts` is generated; never edit it by hand. After the contract
212
+ changes:
213
+
214
+ ```bash
215
+ pnpm generate # tsx scripts/generate.ts → reads ../../api/openapi.yaml
216
+ ```
217
+
218
+ ## License
219
+
220
+ Apache-2.0.
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Stored CLI credentials, shared with the Rust `scribase` CLI:
3
+ * `$XDG_CONFIG_HOME/scribase/credentials` (default `~/.config/scribase`),
4
+ * mode 0600, holding `api_url` and either `access_token` or
5
+ * `token_store=keychain`, in which case the token lives in the OS credential
6
+ * store (macOS keychain via `security`, Linux Secret Service via
7
+ * `secret-tool`) under service `scribase`, account = the API URL. The token
8
+ * is handed to those tools on standard input, never as an argument.
9
+ * `SCRIBASE_NO_KEYCHAIN=1` forces the file.
10
+ */
11
+ export interface StoredCredentials {
12
+ apiUrl?: string;
13
+ token?: string;
14
+ }
15
+ export declare function configDir(env: NodeJS.ProcessEnv): string;
16
+ export declare function credentialsPath(env: NodeJS.ProcessEnv): string;
17
+ /** Parses the credentials file (`key=value` lines). */
18
+ export declare function parseCredentialsFile(text: string): {
19
+ apiUrl?: string;
20
+ token?: string;
21
+ tokenStore?: string;
22
+ };
23
+ /** The `security -i` command that saves a token; the token rides on stdin. */
24
+ export declare function macStoreCommand(account: string, token: string): string;
25
+ /** Reads the stored API URL and token (from the keychain when recorded there). */
26
+ export declare function readCredentials(env: NodeJS.ProcessEnv): StoredCredentials;
27
+ /** Stores a token for `apiUrl`; returns where it went (for display). Never prints it. */
28
+ export declare function storeCredentials(env: NodeJS.ProcessEnv, apiUrl: string, token: string): string;
29
+ /** The pending device login, kept between `--no-wait` runs (0600). */
30
+ export declare function pendingStore(env: NodeJS.ProcessEnv): {
31
+ read: () => string | null;
32
+ write: (text: string) => void;
33
+ clear: () => void;
34
+ };
35
+ //# sourceMappingURL=credentials.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../../src/cli/credentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AASH,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,MAAM,CAExD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,MAAM,CAE9D;AAED,uDAAuD;AACvD,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,CAa3G;AAkBD,8EAA8E;AAC9E,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtE;AAgCD,kFAAkF;AAClF,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG,iBAAiB,CAazE;AAED,yFAAyF;AACzF,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAS9F;AAED,sEAAsE;AACtE,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,IAAI,CAAA;CAAE,CAOpI"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Stored CLI credentials, shared with the Rust `scribase` CLI:
3
+ * `$XDG_CONFIG_HOME/scribase/credentials` (default `~/.config/scribase`),
4
+ * mode 0600, holding `api_url` and either `access_token` or
5
+ * `token_store=keychain`, in which case the token lives in the OS credential
6
+ * store (macOS keychain via `security`, Linux Secret Service via
7
+ * `secret-tool`) under service `scribase`, account = the API URL. The token
8
+ * is handed to those tools on standard input, never as an argument.
9
+ * `SCRIBASE_NO_KEYCHAIN=1` forces the file.
10
+ */
11
+ import { spawnSync } from 'node:child_process';
12
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync, chmodSync } from 'node:fs';
13
+ import { homedir } from 'node:os';
14
+ import { dirname, join } from 'node:path';
15
+ const SERVICE = 'scribase';
16
+ export function configDir(env) {
17
+ return join(env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'scribase');
18
+ }
19
+ export function credentialsPath(env) {
20
+ return join(configDir(env), 'credentials');
21
+ }
22
+ /** Parses the credentials file (`key=value` lines). */
23
+ export function parseCredentialsFile(text) {
24
+ const out = {};
25
+ for (const line of text.split('\n')) {
26
+ const at = line.indexOf('=');
27
+ if (at < 0)
28
+ continue;
29
+ const key = line.slice(0, at).trim();
30
+ const value = line.slice(at + 1).trim();
31
+ if (!value)
32
+ continue;
33
+ if (key === 'api_url')
34
+ out.apiUrl = value;
35
+ if (key === 'access_token')
36
+ out.token = value;
37
+ if (key === 'token_store')
38
+ out.tokenStore = value;
39
+ }
40
+ return out;
41
+ }
42
+ function keychain(env) {
43
+ if (env.SCRIBASE_NO_KEYCHAIN && env.SCRIBASE_NO_KEYCHAIN !== '0')
44
+ return null;
45
+ const store = process.platform === 'darwin' ? 'mac' : process.platform === 'linux' ? 'secret-service' : null;
46
+ if (!store)
47
+ return null;
48
+ const probe = spawnSync(store === 'mac' ? 'security' : 'secret-tool', ['--help'], { stdio: 'ignore' });
49
+ return probe.error ? null : store;
50
+ }
51
+ function safeAccount(account) {
52
+ return account.length > 0 && account.length <= 512 && !/["\\\u0000-\u001f]/.test(account);
53
+ }
54
+ const TOKEN_SHAPE = /^[A-Za-z0-9._~+/=-]{1,16384}$/;
55
+ /** The `security -i` command that saves a token; the token rides on stdin. */
56
+ export function macStoreCommand(account, token) {
57
+ return `add-generic-password -U -s ${SERVICE} -l "Scribase CLI" -a "${account}" -w "${token}"\n`;
58
+ }
59
+ function keychainLoad(store, account) {
60
+ if (!safeAccount(account))
61
+ return undefined;
62
+ const result = store === 'mac'
63
+ ? spawnSync('security', ['find-generic-password', '-s', SERVICE, '-a', account, '-w'], { encoding: 'utf8' })
64
+ : spawnSync('secret-tool', ['lookup', 'service', SERVICE, 'account', account], { encoding: 'utf8' });
65
+ const token = (result.stdout ?? '').trim();
66
+ return result.status === 0 && TOKEN_SHAPE.test(token) ? token : undefined;
67
+ }
68
+ function keychainSave(store, account, token) {
69
+ if (!safeAccount(account) || !TOKEN_SHAPE.test(token))
70
+ return false;
71
+ const result = store === 'mac'
72
+ ? spawnSync('security', ['-i'], { input: macStoreCommand(account, token), stdio: ['pipe', 'ignore', 'ignore'] })
73
+ : spawnSync('secret-tool', ['store', '--label=Scribase CLI', 'service', SERVICE, 'account', account], {
74
+ input: token,
75
+ stdio: ['pipe', 'ignore', 'ignore'],
76
+ });
77
+ // `security -i` exits 0 even when its command fails: read it back.
78
+ return result.status === 0 && keychainLoad(store, account) === token;
79
+ }
80
+ function writePrivate(path, contents) {
81
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
82
+ writeFileSync(path, contents, { mode: 0o600 });
83
+ // Tighten a file that existed with a wider mode.
84
+ chmodSync(path, 0o600);
85
+ }
86
+ /** Reads the stored API URL and token (from the keychain when recorded there). */
87
+ export function readCredentials(env) {
88
+ const path = credentialsPath(env);
89
+ if (!existsSync(path))
90
+ return {};
91
+ const parsed = parseCredentialsFile(readFileSync(path, 'utf8'));
92
+ const out = {};
93
+ if (parsed.apiUrl)
94
+ out.apiUrl = parsed.apiUrl;
95
+ if (parsed.token)
96
+ out.token = parsed.token;
97
+ if (!out.token && parsed.tokenStore === 'keychain' && parsed.apiUrl) {
98
+ const store = keychain(env);
99
+ const token = store ? keychainLoad(store, parsed.apiUrl) : undefined;
100
+ if (token)
101
+ out.token = token;
102
+ }
103
+ return out;
104
+ }
105
+ /** Stores a token for `apiUrl`; returns where it went (for display). Never prints it. */
106
+ export function storeCredentials(env, apiUrl, token) {
107
+ const path = credentialsPath(env);
108
+ const store = keychain(env);
109
+ if (store && keychainSave(store, apiUrl, token)) {
110
+ writePrivate(path, `api_url=${apiUrl}\ntoken_store=keychain\n`);
111
+ return store === 'mac' ? 'the macOS keychain' : 'the Secret Service keyring';
112
+ }
113
+ writePrivate(path, `api_url=${apiUrl}\naccess_token=${token}\n`);
114
+ return path;
115
+ }
116
+ /** The pending device login, kept between `--no-wait` runs (0600). */
117
+ export function pendingStore(env) {
118
+ const path = join(configDir(env), 'pending-login');
119
+ return {
120
+ read: () => (existsSync(path) ? readFileSync(path, 'utf8') : null),
121
+ write: (text) => writePrivate(path, text),
122
+ clear: () => rmSync(path, { force: true }),
123
+ };
124
+ }
125
+ //# sourceMappingURL=credentials.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../src/cli/credentials.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,SAAS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,KAAK,GAAG,KAAK,CAAC;IAC/B,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,gBAAgB,CAAC,GAAsB,EAAE,MAAc,EAAE,KAAa;IACpF,MAAM,IAAI,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;IAClC,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC5B,IAAI,KAAK,IAAI,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC;QAChD,YAAY,CAAC,IAAI,EAAE,WAAW,MAAM,0BAA0B,CAAC,CAAC;QAChE,OAAO,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,4BAA4B,CAAC;IAC/E,CAAC;IACD,YAAY,CAAC,IAAI,EAAE,WAAW,MAAM,kBAAkB,KAAK,IAAI,CAAC,CAAC;IACjE,OAAO,IAAI,CAAC;AACd,CAAC;AAED,sEAAsE;AACtE,MAAM,UAAU,YAAY,CAAC,GAAsB;IACjD,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,eAAe,CAAC,CAAC;IACnD,OAAO;QACL,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAClE,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC;QACzC,KAAK,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;KAC3C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Minimal `.env` editing: set keys without disturbing anything else in the
3
+ * file (comments, order, other variables), and make sure the file is ignored
4
+ * by git. Pure string functions; the caller does the I/O.
5
+ */
6
+ /**
7
+ * Returns `existing` with every key in `values` set: replaced in place when
8
+ * present, appended (under a short header) when not.
9
+ */
10
+ export declare function mergeEnv(existing: string, values: Record<string, string>): string;
11
+ /** Reads one key from env file contents. */
12
+ export declare function readEnvValue(contents: string, key: string): string | undefined;
13
+ /** `.gitignore` contents that ignore `envFile`, or null when already ignored. */
14
+ export declare function ensureIgnored(gitignore: string, envFile: string): string | null;
15
+ /**
16
+ * `.gitignore` contents that also ignore the directory `dir` (e.g. `.scribase`,
17
+ * which holds a temporary project's claim link), or null when already ignored.
18
+ */
19
+ export declare function ensureDirIgnored(gitignore: string, dir: string): string | null;
20
+ //# sourceMappingURL=envfile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envfile.d.ts","sourceRoot":"","sources":["../../src/cli/envfile.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAwBjF;AAED,4CAA4C;AAC5C,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAgB9E;AAED,iFAAiF;AACjF,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAY/E;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAS9E"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Minimal `.env` editing: set keys without disturbing anything else in the
3
+ * file (comments, order, other variables), and make sure the file is ignored
4
+ * by git. Pure string functions; the caller does the I/O.
5
+ */
6
+ const KEY = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/;
7
+ function quoteValue(value) {
8
+ return /^[A-Za-z0-9_./:@+-]*$/.test(value) ? value : JSON.stringify(value);
9
+ }
10
+ /**
11
+ * Returns `existing` with every key in `values` set: replaced in place when
12
+ * present, appended (under a short header) when not.
13
+ */
14
+ export function mergeEnv(existing, values) {
15
+ const remaining = new Map(Object.entries(values));
16
+ const lines = existing.length > 0 ? existing.replace(/\r\n/g, '\n').split('\n') : [];
17
+ const updated = lines.map((line) => {
18
+ const match = KEY.exec(line);
19
+ const key = match?.[1];
20
+ if (key !== undefined && remaining.has(key)) {
21
+ const value = remaining.get(key) ?? '';
22
+ remaining.delete(key);
23
+ return `${key}=${quoteValue(value)}`;
24
+ }
25
+ return line;
26
+ });
27
+ while (updated.length > 0 && updated[updated.length - 1] === '') {
28
+ updated.pop();
29
+ }
30
+ if (remaining.size > 0) {
31
+ if (updated.length > 0)
32
+ updated.push('');
33
+ updated.push('# Scribase (written by `npx scribase init`)');
34
+ for (const [key, value] of remaining) {
35
+ updated.push(`${key}=${quoteValue(value)}`);
36
+ }
37
+ }
38
+ return `${updated.join('\n')}\n`;
39
+ }
40
+ /** Reads one key from env file contents. */
41
+ export function readEnvValue(contents, key) {
42
+ for (const line of contents.split(/\r?\n/)) {
43
+ const match = KEY.exec(line);
44
+ if (match?.[1] === key) {
45
+ const raw = line.slice(line.indexOf('=') + 1).trim();
46
+ if (raw.startsWith('"')) {
47
+ try {
48
+ return JSON.parse(raw);
49
+ }
50
+ catch {
51
+ return raw.slice(1, -1);
52
+ }
53
+ }
54
+ return raw;
55
+ }
56
+ }
57
+ return undefined;
58
+ }
59
+ /** `.gitignore` contents that ignore `envFile`, or null when already ignored. */
60
+ export function ensureIgnored(gitignore, envFile) {
61
+ const patterns = gitignore.split(/\r?\n/).map((line) => line.trim());
62
+ const covered = patterns.some((pattern) => pattern === envFile ||
63
+ pattern === `/${envFile}` ||
64
+ pattern === '.env*' ||
65
+ (envFile.endsWith('.local') && (pattern === '*.local' || pattern === '.env*.local')));
66
+ if (covered)
67
+ return null;
68
+ const base = gitignore.length === 0 || gitignore.endsWith('\n') ? gitignore : `${gitignore}\n`;
69
+ return `${base}${envFile}\n`;
70
+ }
71
+ /**
72
+ * `.gitignore` contents that also ignore the directory `dir` (e.g. `.scribase`,
73
+ * which holds a temporary project's claim link), or null when already ignored.
74
+ */
75
+ export function ensureDirIgnored(gitignore, dir) {
76
+ const name = dir.replace(/^\/+|\/+$/g, '');
77
+ const covered = gitignore
78
+ .split(/\r?\n/)
79
+ .map((line) => line.trim())
80
+ .some((pattern) => [name, `${name}/`, `/${name}`, `/${name}/`, `${name}/*`, `/${name}/*`].includes(pattern));
81
+ if (covered)
82
+ return null;
83
+ const base = gitignore.length === 0 || gitignore.endsWith('\n') ? gitignore : `${gitignore}\n`;
84
+ return `${base}${name}/\n`;
85
+ }
86
+ //# sourceMappingURL=envfile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envfile.js","sourceRoot":"","sources":["../../src/cli/envfile.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,GAAG,GAAG,gDAAgD,CAAC;AAE7D,SAAS,UAAU,CAAC,KAAa;IAC/B,OAAO,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,QAAgB,EAAE,MAA8B;IACvE,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACrF,MAAM,OAAO,GAAG,KAAK,CAAC,G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AI,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,IAAI,CAAC;IAC/F,OAAO,GAAG,IAAI,GAAG,OAAO,IAAI,CAAC;AAC/B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAAiB,EAAE,GAAW;IAC7D,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;IAC3C,MAAM,OAAO,GAAG,SAAS;SACtB,KAAK,CAAC,OAAO,CAAC;SACd,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;SAC1B,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,GAAG,IAAI,GAAG,EAAE,IAAI,IAAI,EAAE,EAAE,IAAI,IAAI,GAAG,EAAE,GAAG,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/G,IAAI,OAAO;QAAE,OAAO,IAAI,CAAC;IACzB,MAAM,IAAI,GAAG,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,IAAI,CAAC;IAC/F,OAAO,GAAG,IAAI,GAAG,IAAI,KAAK,CAAC;AAC7B,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Framework detection and the per-framework conventions `scribase init`
3
+ * follows: which env file to write, which variable names the framework exposes
4
+ * to client code, and where the client module goes. Pure: callers pass the
5
+ * facts about the directory in.
6
+ */
7
+ export type Framework = 'next' | 'expo' | 'vite' | 'node';
8
+ export interface ProjectFacts {
9
+ /** Parsed package.json, or null when there is none. */
10
+ packageJson: {
11
+ dependencies?: Record<string, string>;
12
+ devDependencies?: Record<string, string>;
13
+ } | null;
14
+ /** Whether a path relative to the project root exists. */
15
+ exists: (relativePath: string) => boolean;
16
+ }
17
+ export interface FrameworkConventions {
18
+ framework: Framework;
19
+ /** The env file the framework loads in development. */
20
+ envFile: string;
21
+ /** Variable holding the project URL. */
22
+ urlVar: string;
23
+ /** Variable holding the public (anon) key. */
24
+ anonKeyVar: string;
25
+ /**
26
+ * Variable for the service-role key, or null where any env value can end up
27
+ * in a client bundle by mistake (Expo, Vite) — those projects never get it.
28
+ */
29
+ serviceRoleVar: string | null;
30
+ /** Where the client module is written, relative to the root. */
31
+ clientFile: string;
32
+ /** How client code reads a variable (`process.env.X` or `import.meta.env.X`). */
33
+ readEnv: (name: string) => string;
34
+ }
35
+ /** Detects the framework from package.json dependencies and config files. */
36
+ export declare function detectFramework(facts: ProjectFacts): Framework;
37
+ /** The conventions for a framework in a given directory layout. */
38
+ export declare function conventionsFor(framework: Framework, facts: ProjectFacts): FrameworkConventions;
39
+ /** The client module `scribase init` writes (Scribase speaks the supabase-js API). */
40
+ export declare function clientSource(conventions: FrameworkConventions): string;
41
+ /** The package manager a project uses, from its lockfile. */
42
+ export declare function packageManager(exists: (relativePath: string) => boolean): 'pnpm' | 'yarn' | 'bun' | 'npm';
43
+ /** The install command for supabase-js with a package manager. */
44
+ export declare function installCommand(manager: 'pnpm' | 'yarn' | 'bun' | 'npm'): [string, string[]];
45
+ //# sourceMappingURL=framework.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"framework.d.ts","sourceRoot":"","sources":["../../src/cli/framework.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAE1D,MAAM,WAAW,YAAY;IAC3B,uDAAuD;IACvD,WAAW,EAAE;QAAE,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAAC,eAAe,CAAC,EAAE,MAAM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