phibac 0.0.1 → 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 (52) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +47 -5
  3. package/dist/api.d.ts +96 -0
  4. package/dist/api.js +174 -0
  5. package/dist/api.js.map +1 -0
  6. package/dist/brand.d.ts +19 -0
  7. package/dist/brand.js +20 -0
  8. package/dist/brand.js.map +1 -0
  9. package/dist/cli.d.ts +4 -0
  10. package/dist/cli.js +66 -0
  11. package/dist/cli.js.map +1 -0
  12. package/dist/commands/deploy.d.ts +3 -0
  13. package/dist/commands/deploy.js +83 -0
  14. package/dist/commands/deploy.js.map +1 -0
  15. package/dist/commands/dev.d.ts +3 -0
  16. package/dist/commands/dev.js +132 -0
  17. package/dist/commands/dev.js.map +1 -0
  18. package/dist/commands/generate.d.ts +4 -0
  19. package/dist/commands/generate.js +33 -0
  20. package/dist/commands/generate.js.map +1 -0
  21. package/dist/commands/init.d.ts +3 -0
  22. package/dist/commands/init.js +62 -0
  23. package/dist/commands/init.js.map +1 -0
  24. package/dist/commands/mcp.d.ts +3 -0
  25. package/dist/commands/mcp.js +151 -0
  26. package/dist/commands/mcp.js.map +1 -0
  27. package/dist/commands/native.d.ts +3 -0
  28. package/dist/commands/native.js +114 -0
  29. package/dist/commands/native.js.map +1 -0
  30. package/dist/compile.d.ts +37 -0
  31. package/dist/compile.js +187 -0
  32. package/dist/compile.js.map +1 -0
  33. package/dist/config.d.ts +58 -0
  34. package/dist/config.js +24 -0
  35. package/dist/config.js.map +1 -0
  36. package/dist/index.d.ts +2 -0
  37. package/dist/index.js +24 -0
  38. package/dist/index.js.map +1 -0
  39. package/package.json +39 -7
  40. package/src/api.ts +216 -0
  41. package/src/brand.ts +23 -0
  42. package/src/cli.ts +66 -0
  43. package/src/commands/deploy.ts +88 -0
  44. package/src/commands/dev.ts +127 -0
  45. package/src/commands/generate.ts +35 -0
  46. package/src/commands/init.ts +67 -0
  47. package/src/commands/mcp.ts +145 -0
  48. package/src/commands/native.ts +125 -0
  49. package/src/compile.ts +217 -0
  50. package/src/config.ts +73 -0
  51. package/src/index.ts +26 -0
  52. package/bin/phibac.js +0 -8
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/README.md CHANGED
@@ -1,10 +1,52 @@
1
1
  # phibac
2
2
 
3
3
  The command-line tool for **phibac**, a personal backend: one private
4
- database per user, shared by every app that user approves.
4
+ database per user, shared by the apps that user approves. Use it to create
5
+ your app, describe and publish its data, and generate TypeScript types. Your
6
+ app's code then uses the SDK, [`@phibac/sdk`](https://www.npmjs.com/package/@phibac/sdk).
5
7
 
6
- **Not released yet.** This version is a placeholder. Running `npx phibac`
7
- only prints a message and exits.
8
+ ## Install
8
9
 
9
- The SDK will be published as `@phibac/sdk`. Until both are released, see
10
- https://phibac.com/docs for how to build and install them from source.
10
+ In your app's project:
11
+
12
+ ```sh
13
+ npm install --save-dev phibac
14
+ ```
15
+
16
+ Then run it with `npx phibac <command>`.
17
+
18
+ ## Set up
19
+
20
+ ```sh
21
+ export PB_BASE_URL=https://phibac.com # the server (default http://localhost:8787)
22
+ export PB_TOKEN=pbd_... # from https://phibac.com/account/developer
23
+ ```
24
+
25
+ A developer token manages your apps and schemas only. It can never read,
26
+ write or export any user's data. Keep it secret: don't commit it or paste
27
+ it into a chat.
28
+
29
+ ## Commands
30
+
31
+ | Command | What it does |
32
+ |---|---|
33
+ | `phibac init --name <name> [--origin <url>]` | Create an app and write `pb.config.ts` |
34
+ | `phibac deploy` | Preview, then publish the collections in `pb.config.ts` (nothing is published if a change is breaking) |
35
+ | `phibac generate [--out <file>]` | Write TypeScript types for those collections |
36
+ | `phibac dev` | Run a local server (needs `PB_SERVER_BIN`, the path of a `pb-server` program) |
37
+ | `phibac mcp` | Connect an AI agent's MCP client to the server's tools |
38
+ | `phibac native set\|show` | Register the iOS and Android apps that are this app |
39
+
40
+ Run `phibac <command> --help` for a command's options.
41
+
42
+ ## Docs
43
+
44
+ - Quick start: https://phibac.com/docs/quickstart
45
+ - CLI reference: https://phibac.com/docs/reference/cli
46
+ - `pb.config.ts` reference: https://phibac.com/docs/reference/config
47
+
48
+ Needs Node.js 22 or newer.
49
+
50
+ ## License
51
+
52
+ Apache-2.0
package/dist/api.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ import type { spawn } from "node:child_process";
2
+ export type Env = Record<string, string | undefined>;
3
+ /**
4
+ * Context is everything a command needs from its surroundings, so tests can
5
+ * call a command directly: its arguments (after the command name), the
6
+ * environment, the working directory, output (one line at a time), and the
7
+ * outside world (fetch, spawn, stdin, platform).
8
+ */
9
+ export interface Context {
10
+ args: string[];
11
+ env: Env;
12
+ cwd: string;
13
+ /** Writes one line to stdout. */
14
+ out: (line: string) => void;
15
+ /** Writes one line to stderr. */
16
+ err: (line: string) => void;
17
+ fetch: typeof fetch;
18
+ stdin: AsyncIterable<string | Uint8Array>;
19
+ spawn: typeof spawn;
20
+ /** The CLI's version. */
21
+ version: string;
22
+ platform: NodeJS.Platform;
23
+ arch: string;
24
+ homedir: string;
25
+ }
26
+ /** CliError is a failure to report to the user (exit code 1). */
27
+ export declare class CliError extends Error {
28
+ name: string;
29
+ }
30
+ /** UsageError is a mistake in how the command was invoked (exit code 2). */
31
+ export declare class UsageError extends Error {
32
+ name: string;
33
+ }
34
+ /** NetworkError is a failure to reach the server at all. */
35
+ export declare class NetworkError extends CliError {
36
+ name: string;
37
+ }
38
+ /** ApiError is an error body the server returned. */
39
+ export declare class ApiError extends CliError {
40
+ readonly status: number;
41
+ readonly code: string;
42
+ readonly hint: string;
43
+ readonly reasons: string[];
44
+ name: string;
45
+ constructor(status: number, code: string, message: string, hint: string, reasons: string[]);
46
+ }
47
+ /** The base URL of the server, without a trailing slash. */
48
+ export declare function baseUrl(env: Env): string;
49
+ /** The developer token, or an error saying where to create one. */
50
+ export declare function requireToken(env: Env): string;
51
+ /** Replaces every occurrence of secret in text. */
52
+ export declare function redact(text: string, secret: string | undefined): string;
53
+ export interface RequestOptions {
54
+ body?: unknown;
55
+ query?: Record<string, string>;
56
+ }
57
+ /**
58
+ * Client talks to the server's HTTP API. With a token it sends it as a bearer
59
+ * token; the token is never put in a URL and is removed from any text the
60
+ * client produces.
61
+ */
62
+ export declare class Client {
63
+ readonly baseUrl: string;
64
+ private readonly token;
65
+ private readonly doFetch;
66
+ /** auth: true needs PB_TOKEN; false sends no credentials. */
67
+ constructor(ctx: Pick<Context, "env" | "fetch">, auth: boolean);
68
+ private url;
69
+ /** send performs the request and returns the response if it is 2xx. */
70
+ send(method: string, path: string, opts?: RequestOptions & {
71
+ headers?: Record<string, string>;
72
+ }): Promise<Response>;
73
+ /** request performs the request and returns the parsed JSON body. */
74
+ request<T>(method: string, path: string, opts?: RequestOptions): Promise<T>;
75
+ networkError(e: unknown): NetworkError;
76
+ /** responseError turns a non-2xx response into an ApiError, or a CliError if the body is not an error body. */
77
+ responseError(res: Response, url: string): Promise<CliError>;
78
+ /** errorFromText is responseError for a body that has already been read. */
79
+ errorFromText(status: number, rawText: string, url: string): CliError;
80
+ }
81
+ /** formatApiError renders an error as its code, message, hint and reasons. */
82
+ export declare function formatApiError(e: ApiError): string[];
83
+ /** parseFlags parses a command's flags; any problem is a UsageError. */
84
+ export declare function parseFlags<O extends Record<string, {
85
+ type: "string" | "boolean";
86
+ short?: string;
87
+ }>>(args: string[], options: O): {
88
+ values: {
89
+ [K in keyof O]?: O[K]["type"] extends "string" ? string : boolean;
90
+ };
91
+ };
92
+ /**
93
+ * guard runs a command body and turns the errors commands throw into
94
+ * messages on stderr and an exit code: 2 for a usage error, 1 for a failure.
95
+ */
96
+ export declare function guard(ctx: Pick<Context, "err">, usage: string, body: () => Promise<number>): Promise<number>;
package/dist/api.js ADDED
@@ -0,0 +1,174 @@
1
+ import { parseArgs } from "node:util";
2
+ import { BRAND, CLI_NAME, DEFAULT_BASE_URL } from "./brand.js";
3
+ /** CliError is a failure to report to the user (exit code 1). */
4
+ export class CliError extends Error {
5
+ name = "CliError";
6
+ }
7
+ /** UsageError is a mistake in how the command was invoked (exit code 2). */
8
+ export class UsageError extends Error {
9
+ name = "UsageError";
10
+ }
11
+ /** NetworkError is a failure to reach the server at all. */
12
+ export class NetworkError extends CliError {
13
+ name = "NetworkError";
14
+ }
15
+ /** ApiError is an error body the server returned. */
16
+ export class ApiError extends CliError {
17
+ status;
18
+ code;
19
+ hint;
20
+ reasons;
21
+ name = "ApiError";
22
+ constructor(status, code, message, hint, reasons) {
23
+ super(message);
24
+ this.status = status;
25
+ this.code = code;
26
+ this.hint = hint;
27
+ this.reasons = reasons;
28
+ }
29
+ }
30
+ /** The base URL of the server, without a trailing slash. */
31
+ export function baseUrl(env) {
32
+ const v = env.PB_BASE_URL?.trim();
33
+ return (v || DEFAULT_BASE_URL).replace(/\/+$/, "");
34
+ }
35
+ /** The developer token, or an error saying where to create one. */
36
+ export function requireToken(env) {
37
+ const t = env.PB_TOKEN?.trim();
38
+ if (t)
39
+ return t;
40
+ throw new CliError(`PB_TOKEN is not set. Create a developer token in the developer section of your account page at ${baseUrl(env)}/account/developer, then run: export PB_TOKEN=pbd_...`);
41
+ }
42
+ /** Replaces every occurrence of secret in text. */
43
+ export function redact(text, secret) {
44
+ return secret ? text.split(secret).join("[redacted]") : text;
45
+ }
46
+ /**
47
+ * Client talks to the server's HTTP API. With a token it sends it as a bearer
48
+ * token; the token is never put in a URL and is removed from any text the
49
+ * client produces.
50
+ */
51
+ export class Client {
52
+ baseUrl;
53
+ token;
54
+ doFetch;
55
+ /** auth: true needs PB_TOKEN; false sends no credentials. */
56
+ constructor(ctx, auth) {
57
+ this.baseUrl = baseUrl(ctx.env);
58
+ this.token = auth ? requireToken(ctx.env) : undefined;
59
+ this.doFetch = ctx.fetch;
60
+ }
61
+ url(path, query) {
62
+ const q = query ? "?" + new URLSearchParams(query).toString() : "";
63
+ return this.baseUrl + path + q;
64
+ }
65
+ /** send performs the request and returns the response if it is 2xx. */
66
+ async send(method, path, opts = {}) {
67
+ const url = this.url(path, opts.query);
68
+ const headers = { ...opts.headers };
69
+ if (this.token)
70
+ headers.Authorization = `Bearer ${this.token}`;
71
+ const init = { method, headers };
72
+ if (opts.body !== undefined) {
73
+ headers["Content-Type"] = "application/json";
74
+ init.body = typeof opts.body === "string" ? opts.body : JSON.stringify(opts.body);
75
+ }
76
+ let res;
77
+ try {
78
+ res = await this.doFetch(url, init);
79
+ }
80
+ catch (e) {
81
+ throw this.networkError(e);
82
+ }
83
+ if (!res.ok)
84
+ throw await this.responseError(res, url);
85
+ return res;
86
+ }
87
+ /** request performs the request and returns the parsed JSON body. */
88
+ async request(method, path, opts = {}) {
89
+ const res = await this.send(method, path, { ...opts, headers: { Accept: "application/json" } });
90
+ try {
91
+ return (await res.json());
92
+ }
93
+ catch {
94
+ throw new CliError(`the server at ${this.baseUrl} sent a response that is not JSON (${method} ${path}); check that PB_BASE_URL points at a ${BRAND} server`);
95
+ }
96
+ }
97
+ networkError(e) {
98
+ const cause = e instanceof Error ? e.cause : undefined;
99
+ const code = typeof cause?.code === "string" ? ` (${cause.code})` : "";
100
+ return new NetworkError(`could not reach the server at ${this.baseUrl}${code}. Check that PB_BASE_URL is right and the server is running (${CLI_NAME} dev starts a local one).`);
101
+ }
102
+ /** responseError turns a non-2xx response into an ApiError, or a CliError if the body is not an error body. */
103
+ async responseError(res, url) {
104
+ return this.errorFromText(res.status, await res.text().catch(() => ""), url);
105
+ }
106
+ /** errorFromText is responseError for a body that has already been read. */
107
+ errorFromText(status, rawText, url) {
108
+ const text = redact(rawText, this.token);
109
+ let body;
110
+ try {
111
+ body = JSON.parse(text);
112
+ }
113
+ catch {
114
+ body = undefined;
115
+ }
116
+ const e = body?.error;
117
+ if (e && typeof e.code === "string") {
118
+ const reasons = Array.isArray(e.reasons) ? e.reasons.filter((r) => typeof r === "string") : [];
119
+ return new ApiError(status, e.code, String(e.message ?? ""), String(e.hint ?? ""), reasons);
120
+ }
121
+ const snippet = text.trim().slice(0, 200);
122
+ return new CliError(`HTTP ${status} from ${redact(url, this.token)}, which is not a ${BRAND} error response${snippet ? `: ${snippet}` : ""}`);
123
+ }
124
+ }
125
+ /** formatApiError renders an error as its code, message, hint and reasons. */
126
+ export function formatApiError(e) {
127
+ const lines = [`error: ${e.code}: ${e.message}`];
128
+ if (e.hint)
129
+ lines.push(`hint: ${e.hint}`);
130
+ if (e.reasons.length > 0) {
131
+ lines.push("reasons:");
132
+ for (const r of e.reasons)
133
+ lines.push(` - ${r}`);
134
+ }
135
+ return lines;
136
+ }
137
+ /** parseFlags parses a command's flags; any problem is a UsageError. */
138
+ export function parseFlags(args, options) {
139
+ try {
140
+ const { values } = parseArgs({ args, options, strict: true, allowPositionals: false });
141
+ return { values: values };
142
+ }
143
+ catch (e) {
144
+ throw new UsageError(e instanceof Error ? e.message : String(e));
145
+ }
146
+ }
147
+ /**
148
+ * guard runs a command body and turns the errors commands throw into
149
+ * messages on stderr and an exit code: 2 for a usage error, 1 for a failure.
150
+ */
151
+ export async function guard(ctx, usage, body) {
152
+ try {
153
+ return await body();
154
+ }
155
+ catch (e) {
156
+ if (e instanceof UsageError) {
157
+ ctx.err(`error: ${e.message}`);
158
+ for (const l of usage.split("\n"))
159
+ ctx.err(l);
160
+ return 2;
161
+ }
162
+ if (e instanceof ApiError) {
163
+ for (const l of formatApiError(e))
164
+ ctx.err(l);
165
+ return 1;
166
+ }
167
+ if (e instanceof CliError) {
168
+ ctx.err(`error: ${e.message}`);
169
+ return 1;
170
+ }
171
+ throw e;
172
+ }
173
+ }
174
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,YAAY,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C,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,GAAyB,EAAE,KAAa,EAAE,IAA2B;IAC/F,IAAI,CAAC;QACH,OAAO,MAAM,IAAI,EAAE,CAAC;IACtB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,YAAY,UAAU,EAAE,CAAC;YAC5B,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;YAC/B,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC;gBAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC9C,OAAO,CAAC,CAAC;QACX,CAAC;QACD,IAAI,CAAC,YAAY,QAAQ,EAAE,CAAC;YAC1B,KAAK,MAAM,CAAC,IAAI,cAAc,CAAC,CAAC,CAAC;gBAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC9C,OAAO,CAAC,CAAC;QACX,CAAC;QACD,IAAI,CAAC,YAAY,QAAQ,EAAE,CAAC;YAC1B,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;YAC/B,OAAO,CAAC,CAAC;QACX,CAAC;QACD,MAAM,CAAC,CAAC;IACV,CAAC;AACH,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * BRAND is the product name shown in user-facing CLI text (messages and the
3
+ * generated pb.config.ts). It is also the internal code name behind the
4
+ * config file, the server binary and the cache folder. Change it here; never
5
+ * write the name anywhere else.
6
+ */
7
+ export declare const BRAND = "pb";
8
+ /**
9
+ * CLI_NAME is the CLI's npm package name, its command name (package.json
10
+ * "bin") and the module pb.config.ts imports the helpers from. A test checks
11
+ * it against package.json.
12
+ */
13
+ export declare const CLI_NAME = "phibac";
14
+ /** DEFAULT_BASE_URL is the local development server. */
15
+ export declare const DEFAULT_BASE_URL = "http://localhost:8787";
16
+ /** SERVER_BIN is the base name of the server binary release assets. */
17
+ export declare const SERVER_BIN = "pb-server";
18
+ /** CONFIG_FILE is the name of the app config file in a project. */
19
+ export declare const CONFIG_FILE = "pb.config.ts";
package/dist/brand.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * BRAND is the product name shown in user-facing CLI text (messages and the
3
+ * generated pb.config.ts). It is also the internal code name behind the
4
+ * config file, the server binary and the cache folder. Change it here; never
5
+ * write the name anywhere else.
6
+ */
7
+ export const BRAND = "pb";
8
+ /**
9
+ * CLI_NAME is the CLI's npm package name, its command name (package.json
10
+ * "bin") and the module pb.config.ts imports the helpers from. A test checks
11
+ * it against package.json.
12
+ */
13
+ export const CLI_NAME = "phibac";
14
+ /** DEFAULT_BASE_URL is the local development server. */
15
+ export const DEFAULT_BASE_URL = "http://localhost:8787";
16
+ /** SERVER_BIN is the base name of the server binary release assets. */
17
+ export const SERVER_BIN = `${BRAND}-server`;
18
+ /** CONFIG_FILE is the name of the app config file in a project. */
19
+ export const CONFIG_FILE = `${BRAND}.config.ts`;
20
+ //# sourceMappingURL=brand.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"brand.js","sourceRoot":"","sources":["../src/brand.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,IAAI,CAAC;AAE1B;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,QAAQ,CAAC;AAEjC,wDAAwD;AACxD,MAAM,CAAC,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AAExD,uEAAuE;AACvE,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,KAAK,SAAS,CAAC;AAE5C,mEAAmE;AACnE,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,KAAK,YAAY,CAAC"}
package/dist/cli.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { type Context } from "./api.js";
2
+ export declare const USAGE = "Usage: phibac <command> [options]\n\nCommands:\n phibac init --name <name> [--origin <url>] create an app and write pb.config.ts\n phibac deploy preview, then publish the collections in pb.config.ts\n phibac generate [--out <file>] write TypeScript types for those collections\n phibac dev run a local server (billing off, data in ./.pb)\n phibac mcp bridge an agent's stdio MCP to the server's /mcp endpoint\n phibac native set|show register the iOS and Android apps that are this app\n\nRun \"phibac <command> --help\" for a command's options. phibac --version prints the version.\n\nEnvironment:\n PB_BASE_URL the server (default http://localhost:8787)\n PB_TOKEN developer token (pbd_...), from the developer section of your account page;\n needed by init, deploy, mcp and native\n PB_SERVER_BIN phibac dev: path of the server binary to run\n PB_RELEASES_URL phibac dev: https address to download the server binary from, if PB_SERVER_BIN is not set\n\nExit codes: 0 success, 1 failure, 2 usage error.";
3
+ /** run dispatches argv (the arguments after the program name) to a command. */
4
+ export declare function run(argv: string[], ctx: Context): Promise<number>;
package/dist/cli.js ADDED
@@ -0,0 +1,66 @@
1
+ import { guard } from "./api.js";
2
+ import { CLI_NAME, CONFIG_FILE } from "./brand.js";
3
+ import * as deploy from "./commands/deploy.js";
4
+ import * as dev from "./commands/dev.js";
5
+ import * as generate from "./commands/generate.js";
6
+ import * as init from "./commands/init.js";
7
+ import * as mcp from "./commands/mcp.js";
8
+ import * as native from "./commands/native.js";
9
+ const COMMANDS = {
10
+ init: { run: init.init, USAGE: init.USAGE },
11
+ deploy: { run: deploy.deploy, USAGE: deploy.USAGE },
12
+ generate: { run: generate.generate, USAGE: generate.USAGE },
13
+ dev: { run: dev.dev, USAGE: dev.USAGE },
14
+ mcp: { run: mcp.mcp, USAGE: mcp.USAGE },
15
+ native: { run: native.native, USAGE: native.USAGE },
16
+ };
17
+ export const USAGE = `Usage: ${CLI_NAME} <command> [options]
18
+
19
+ Commands:
20
+ ${CLI_NAME} init --name <name> [--origin <url>] create an app and write ${CONFIG_FILE}
21
+ ${CLI_NAME} deploy preview, then publish the collections in ${CONFIG_FILE}
22
+ ${CLI_NAME} generate [--out <file>] write TypeScript types for those collections
23
+ ${CLI_NAME} dev run a local server (billing off, data in ./.pb)
24
+ ${CLI_NAME} mcp bridge an agent's stdio MCP to the server's /mcp endpoint
25
+ ${CLI_NAME} native set|show register the iOS and Android apps that are this app
26
+
27
+ Run "${CLI_NAME} <command> --help" for a command's options. ${CLI_NAME} --version prints the version.
28
+
29
+ Environment:
30
+ PB_BASE_URL the server (default http://localhost:8787)
31
+ PB_TOKEN developer token (pbd_...), from the developer section of your account page;
32
+ needed by init, deploy, mcp and native
33
+ PB_SERVER_BIN ${CLI_NAME} dev: path of the server binary to run
34
+ PB_RELEASES_URL ${CLI_NAME} dev: https address to download the server binary from, if PB_SERVER_BIN is not set
35
+
36
+ Exit codes: 0 success, 1 failure, 2 usage error.`;
37
+ /** run dispatches argv (the arguments after the program name) to a command. */
38
+ export async function run(argv, ctx) {
39
+ const [name, ...rest] = argv;
40
+ if (name === undefined) {
41
+ for (const l of USAGE.split("\n"))
42
+ ctx.err(l);
43
+ return 2;
44
+ }
45
+ if (name === "--help" || name === "-h" || name === "help") {
46
+ ctx.out(USAGE);
47
+ return 0;
48
+ }
49
+ if (name === "--version" || name === "-v" || name === "-V") {
50
+ ctx.out(ctx.version);
51
+ return 0;
52
+ }
53
+ const command = Object.hasOwn(COMMANDS, name) ? COMMANDS[name] : undefined;
54
+ if (!command) {
55
+ ctx.err(`error: unknown command "${name}"`);
56
+ for (const l of USAGE.split("\n"))
57
+ ctx.err(l);
58
+ return 2;
59
+ }
60
+ if (rest.includes("--help") || rest.includes("-h")) {
61
+ ctx.out(command.USAGE);
62
+ return 0;
63
+ }
64
+ return guard(ctx, command.USAGE, () => command.run({ ...ctx, args: rest }));
65
+ }
66
+ //# sourceMappingURL=cli.js.map