@nestica/cli 0.3.0 → 0.5.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 (3) hide show
  1. package/README.md +29 -91
  2. package/dist/cli.js +176 -85
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,54 +1,44 @@
1
- # nestica-cli
1
+ # nestica
2
2
 
3
- Command-line client for the Nestica REST API (`nestica-api`): full CRUD
4
- over `profiles`, `accounts`, `contacts`, `transactions` and `yields`,
5
- plus session management (`login`, `logout`, `whoami`, `set-profile`).
3
+ Command-line client for the Nestica REST API: full CRUD over `profiles`,
4
+ `accounts`, `contacts`, `transactions` and `yields`, plus session
5
+ management (`login`, `logout`, `whoami`, `set-profile`).
6
6
 
7
7
  ## Install
8
8
 
9
- Published package: `npm install -g @nestica/cli` (zero runtime
10
- dependencies — everything is bundled into the `nestica` binary).
11
-
12
- From this repository:
13
-
14
9
  ```sh
15
- pnpm install
16
- pnpm --filter @nestica/cli build
17
- pnpm exec nestica --help
10
+ npm install -g @nestica/cli
18
11
  ```
19
12
 
20
- The CLI talks to the deployed API by default
21
- (`https://api-tjnuhxmzkq-uc.a.run.app/api/v1.0`); point it elsewhere
22
- with `--base-url` or `NESTICA_BASE_URL`.
13
+ Zero runtime dependencies everything is bundled into the `nestica`
14
+ binary. Requires Node.js >= 22.12. The CLI talks to the deployed API
15
+ by default.
23
16
 
24
17
  ## Sessions
25
18
 
26
- State lives in one JSON file with owner-only permissions:
27
- `${XDG_CONFIG_HOME:-~/.config}/nestica/config.json` (override the whole
28
- path with `NESTICA_CONFIG`). It stores the connection settings, the
29
- Firebase tokens and the default profile. Precedence for every setting:
30
- **flag > environment > session file > default**.
19
+ ```sh
20
+ nestica login --email dev@example.com # prompts for the password
21
+ nestica login --google --google-client-id <id> # in the browser
31
22
 
32
- | Setting | Flag | Environment | Default |
33
- | --- | --- | --- | --- |
34
- | API base URL | `-H, --base-url` | `NESTICA_BASE_URL` | deployed API |
35
- | Firebase web API key | `--api-key` | `FIREBASE_API_KEY` | the Nestica project's web key |
36
- | Google OAuth client id | `--google-client-id` | `GOOGLE_CLIENT_ID` | — |
37
- | Google OAuth client secret | `--google-client-secret` | `GOOGLE_CLIENT_SECRET` | — |
38
- | Auth emulator | — | `FIREBASE_AUTH_EMULATOR_HOST` | inferred from a localhost base URL |
23
+ nestica whoami
24
+ nestica set-profile
25
+ nestica logout
26
+ ```
39
27
 
40
- Expired ID tokens refresh automatically before requests; when the
41
- refresh token itself is rejected the CLI says so and asks for a new
42
- `login`.
28
+ The session (connection settings, Firebase tokens, default profile)
29
+ lives in one JSON file with owner-only permissions:
30
+ `${XDG_CONFIG_HOME:-~/.config}/nestica/config.json`.
31
+
32
+ `set-profile` lists the profiles and asks which one to make the
33
+ default; `nestica set-profile <uuid>` stores a specific one and
34
+ `--clear` removes it. The API would otherwise default to the first
35
+ profile created. `logout` deletes the session file; running `login`
36
+ while already signed in just reports the current session — `logout`
37
+ first to switch accounts.
43
38
 
44
39
  ## Commands
45
40
 
46
41
  ```
47
- nestica login [--email <email>] [--password <pw>] [--google] ...
48
- nestica logout
49
- nestica whoami
50
- nestica set-profile [<id>] [--clear]
51
-
52
42
  nestica <resource> list [--page <n>] [--page-size <n>] [--profile <uuid>]
53
43
  nestica <resource> get <id>
54
44
  nestica <resource> create [field flags] [--data <json|->]
@@ -61,32 +51,14 @@ yields`. Scoped resources send the stored default profile (or an
61
51
  explicit `--profile`) as the `profileId` query parameter; `transactions
62
52
  list` also takes `--type expense|income`.
63
53
 
64
- Global flags: `-H/--base-url`, `--api-key`, `--profile`,
65
- `-F/--format pretty|json|table` (default `pretty`), `--no-color`
66
- (or `NO_COLOR`; color is also off when stdout is not a TTY).
67
-
68
- ### login
69
-
70
- - Email/password (prompted when omitted; the password is not echoed):
71
- `nestica login --email dev@example.com` (the Firebase web API key
72
- defaults to the Nestica project's own key)
73
- - Google, in the browser (OAuth + PKCE against a loopback redirect;
74
- needs the Firebase project's web OAuth client id):
75
- `nestica login --google --google-client-id <id> --api-key <key>`
76
-
77
- `logout` deletes the session file.
78
-
79
- ### set-profile
80
-
81
- `nestica set-profile` lists the profiles and asks which one to make the
82
- default; `nestica set-profile <uuid>` stores a specific one and
83
- `--clear` removes it. The API would otherwise default to the first
84
- profile created.
54
+ Global flags: `--profile`, `-F/--format pretty|json|table` (default
55
+ `pretty`), `--no-color` (or `NO_COLOR`; color is also off when stdout
56
+ is not a TTY).
85
57
 
86
58
  ### Field flags
87
59
 
88
60
  Create/update bodies come from typed flags — identical rules to the
89
- API, validated locally with `@nestica/models` — plus `--data` (or
61
+ API, validated locally before the request is sent — plus `--data` (or
90
62
  `--json`) to merge a raw JSON body over them (`-` reads stdin):
91
63
 
92
64
  ```sh
@@ -121,37 +93,3 @@ print nothing at all.
121
93
  ### Exit codes
122
94
 
123
95
  `0` success · `1` API/runtime errors · `2` usage errors.
124
-
125
- ## Smoke test against the emulators
126
-
127
- ```sh
128
- pnpm --filter nestica-api serve # functions :5001, firestore :8080, auth :9099
129
- # create a user once in the Auth UI at http://localhost:4000
130
-
131
- export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099
132
- pnpm exec nestica login \
133
- --base-url http://localhost:5001/nestica-finances/us-central1/api/api/v1.0 \
134
- --api-key demo --email dev@example.com
135
-
136
- pnpm exec nestica profiles create --name Gary --country CO \
137
- --timezone America/Bogota --currency COP --default-currency COP
138
- pnpm exec nestica set-profile
139
- pnpm exec nestica accounts create --name Bancolombia --label bank
140
- pnpm exec nestica transactions create --type expense --amount 42.5 \
141
- --currency COP --date 2026-08-22T12:00:00Z --status settled --label groceries
142
- pnpm exec nestica transactions list --type expense --format table
143
- pnpm exec nestica whoami
144
- pnpm exec nestica logout
145
- ```
146
-
147
- Note: the Google flow always talks to the real Google endpoints — use
148
- email/password with the emulators.
149
-
150
- ## Tests
151
-
152
- ```sh
153
- pnpm --filter @nestica/cli test
154
- ```
155
-
156
- Unit tests only (mocked `fetch`, temporary session files); the
157
- repository-wide `pnpm test` runs them through Nx.
package/dist/cli.js CHANGED
@@ -3380,7 +3380,7 @@ var program = new Command();
3380
3380
  // package.json
3381
3381
  var package_default = {
3382
3382
  name: "@nestica/cli",
3383
- version: "0.3.0",
3383
+ version: "0.5.0",
3384
3384
  type: "module",
3385
3385
  description: "Command-line client for the Nestica REST API",
3386
3386
  license: "MIT",
@@ -3788,40 +3788,148 @@ ${url2}`
3788
3788
  }
3789
3789
  }
3790
3790
 
3791
- // src/commands/prompt.ts
3792
- import * as readline from "node:readline/promises";
3793
- async function prompt(question) {
3794
- const rl = readline.createInterface({
3795
- input: process.stdin,
3796
- output: process.stdout
3797
- });
3798
- try {
3799
- return await rl.question(question);
3800
- } finally {
3801
- rl.close();
3791
+ // src/output/colors.ts
3792
+ var IDENTITY_COLORS = {
3793
+ dim: (text) => text,
3794
+ bold: (text) => text,
3795
+ cyan: (text) => text,
3796
+ green: (text) => text,
3797
+ yellow: (text) => text,
3798
+ red: (text) => text,
3799
+ magenta: (text) => text
3800
+ };
3801
+ function wrap(code) {
3802
+ return (text) => `\x1B[${code}m${text}\x1B[0m`;
3803
+ }
3804
+ var ANSI_COLORS = {
3805
+ dim: wrap("2"),
3806
+ bold: wrap("1"),
3807
+ cyan: wrap("36"),
3808
+ green: wrap("32"),
3809
+ yellow: wrap("33"),
3810
+ red: wrap("31"),
3811
+ magenta: wrap("35")
3812
+ };
3813
+ function pickColors(options) {
3814
+ const stream = options.stream ?? process.stdout;
3815
+ if (options.noColor || process.env["NO_COLOR"] !== void 0 || !stream.isTTY) {
3816
+ return IDENTITY_COLORS;
3802
3817
  }
3818
+ return ANSI_COLORS;
3803
3819
  }
3804
- async function promptHidden(question) {
3805
- const rl = readline.createInterface({
3806
- input: process.stdin,
3807
- output: process.stdout
3820
+
3821
+ // src/commands/prompt.ts
3822
+ import { createInterface, emitKeypressEvents } from "node:readline";
3823
+ var lines = [];
3824
+ var reader;
3825
+ var closed = false;
3826
+ var resolveLine;
3827
+ function ensureReader() {
3828
+ if (reader || closed) {
3829
+ return;
3830
+ }
3831
+ reader = createInterface({ input: process.stdin, output: process.stdout });
3832
+ reader.on("line", (line) => {
3833
+ lines.push(line);
3834
+ resolveLine?.();
3835
+ resolveLine = void 0;
3808
3836
  });
3809
- const mutable = rl;
3810
- mutable._writeToOutput = () => {
3811
- };
3812
- process.stdout.write(question);
3813
- try {
3814
- return await rl.question("");
3815
- } finally {
3816
- process.stdout.write("\n");
3817
- rl.close();
3837
+ reader.on("close", () => {
3838
+ closed = true;
3839
+ resolveLine?.();
3840
+ resolveLine = void 0;
3841
+ });
3842
+ }
3843
+ async function nextLine() {
3844
+ ensureReader();
3845
+ if (lines.length > 0) {
3846
+ return lines.shift() ?? "";
3847
+ }
3848
+ if (closed) {
3849
+ return "";
3818
3850
  }
3851
+ await new Promise((resolve) => {
3852
+ resolveLine = resolve;
3853
+ });
3854
+ return lines.shift() ?? "";
3855
+ }
3856
+ function closePrompts() {
3857
+ reader?.close();
3858
+ reader = void 0;
3859
+ closed = false;
3860
+ }
3861
+ async function prompt(label2) {
3862
+ process.stdout.write(`${label2} `);
3863
+ return (await nextLine()).trim();
3864
+ }
3865
+ async function promptHidden(label2) {
3866
+ process.stdout.write(`${label2} `);
3867
+ if (!process.stdin.isTTY) {
3868
+ const answer = await nextLine();
3869
+ return answer.replace(/\r$/, "");
3870
+ }
3871
+ if (lines.length > 0) {
3872
+ return lines.shift()?.replace(/\r$/, "") ?? "";
3873
+ }
3874
+ closePrompts();
3875
+ const input = process.stdin;
3876
+ return new Promise((resolve) => {
3877
+ let value = "";
3878
+ const onKey = (ch, key) => {
3879
+ if (key.ctrl && (key.name === "c" || key.name === "d")) {
3880
+ restore();
3881
+ process.stdout.write("\n");
3882
+ process.kill(process.pid, "SIGINT");
3883
+ return;
3884
+ }
3885
+ if (key.name === "return" || key.name === "enter") {
3886
+ restore();
3887
+ process.stdout.write("\n");
3888
+ resolve(value);
3889
+ return;
3890
+ }
3891
+ if (key.name === "backspace") {
3892
+ value = value.slice(0, -1);
3893
+ process.stdout.write("\b \b");
3894
+ return;
3895
+ }
3896
+ if (typeof ch === "string" && ch.length === 1 && !key.ctrl && !key.meta) {
3897
+ value += ch;
3898
+ process.stdout.write("*");
3899
+ }
3900
+ };
3901
+ const restore = () => {
3902
+ input.removeListener("keypress", onKey);
3903
+ input.removeListener("end", onEnd);
3904
+ input.pause();
3905
+ if (input.isRaw) {
3906
+ input.setRawMode(false);
3907
+ }
3908
+ };
3909
+ const onEnd = () => {
3910
+ restore();
3911
+ process.stdout.write("\n");
3912
+ resolve(value);
3913
+ };
3914
+ emitKeypressEvents(input);
3915
+ input.on("keypress", onKey);
3916
+ input.once("end", onEnd);
3917
+ input.setRawMode(true);
3918
+ input.resume();
3919
+ });
3819
3920
  }
3820
3921
 
3821
3922
  // src/commands/login.ts
3822
3923
  var EXPIRY_MARGIN_MS = 6e4;
3823
3924
  async function loginAction(options) {
3824
3925
  const session = loadSession();
3926
+ if (session.idToken) {
3927
+ const identity = session.email ?? session.uid;
3928
+ console.log(`Session: ${sessionPath()}`);
3929
+ console.log("Run `nestica logout` first to change account.");
3930
+ console.log(pickColors({}).green(`Already logged in as ${identity}.`));
3931
+ return;
3932
+ }
3825
3933
  const connection = resolveConnection(
3826
3934
  { baseUrl: options.baseUrl, apiKey: options.apiKey },
3827
3935
  session
@@ -3832,21 +3940,27 @@ async function loginAction(options) {
3832
3940
  );
3833
3941
  }
3834
3942
  const result = options.google ? await googleLogin(connection.apiKey, connection.baseUrl, options, session) : await passwordLogin(connection.baseUrl, connection.apiKey, options);
3835
- saveSession({
3836
- ...session,
3837
- baseUrl: connection.baseUrl,
3838
- apiKey: connection.apiKey,
3839
- email: result.email ?? session.email,
3840
- uid: result.uid,
3841
- idToken: result.idToken,
3842
- refreshToken: result.refreshToken,
3843
- expiresAt: Date.now() + result.expiresIn * 1e3 - EXPIRY_MARGIN_MS
3844
- });
3845
- console.log(`Logged in as ${result.email ?? result.uid}.`);
3943
+ const path2 = sessionPath();
3944
+ saveSession(
3945
+ {
3946
+ ...session,
3947
+ baseUrl: connection.baseUrl,
3948
+ apiKey: connection.apiKey,
3949
+ email: result.email ?? session.email,
3950
+ uid: result.uid,
3951
+ idToken: result.idToken,
3952
+ refreshToken: result.refreshToken,
3953
+ expiresAt: Date.now() + result.expiresIn * 1e3 - EXPIRY_MARGIN_MS
3954
+ },
3955
+ path2
3956
+ );
3957
+ console.log(`Session saved to ${path2}`);
3958
+ console.log(pickColors({}).green(`Logged in as ${result.email ?? result.uid}.`));
3846
3959
  }
3847
3960
  async function passwordLogin(baseUrl, apiKey, options) {
3848
- const email3 = options.email ?? await prompt("Email: ");
3849
- const password = options.password ?? await promptHidden("Password: ");
3961
+ const email3 = options.email ?? await prompt("Email:");
3962
+ const password = options.password ?? await promptHidden("Password:");
3963
+ closePrompts();
3850
3964
  if (!email3 || !password) {
3851
3965
  throw new Error("Email and password are required.");
3852
3966
  }
@@ -3865,10 +3979,16 @@ async function googleLogin(apiKey, baseUrl, options, session) {
3865
3979
 
3866
3980
  // src/commands/logout.ts
3867
3981
  function logoutAction() {
3982
+ const session = loadSession();
3868
3983
  if (removeSession()) {
3869
- console.log("Logged out.");
3984
+ const identity = session.email ?? session.uid;
3985
+ console.log(`Session removed: ${sessionPath()}`);
3986
+ console.log(
3987
+ pickColors({}).green(identity ? `Logged out as ${identity}.` : "Logged out.")
3988
+ );
3870
3989
  } else {
3871
- console.log("Not logged in.");
3990
+ console.log(`(no session file at ${sessionPath()})`);
3991
+ console.log(pickColors({}).green("Not logged in."));
3872
3992
  }
3873
3993
  }
3874
3994
 
@@ -3886,7 +4006,7 @@ function withComma(block) {
3886
4006
  const last = block.pop() ?? "";
3887
4007
  return [...block, `${last},`];
3888
4008
  }
3889
- function lines(value, colors, depth) {
4009
+ function lines2(value, colors, depth) {
3890
4010
  const pad = " ".repeat(depth);
3891
4011
  const inner = " ".repeat(depth + 1);
3892
4012
  if (value === null) {
@@ -3906,7 +4026,7 @@ function lines(value, colors, depth) {
3906
4026
  }
3907
4027
  const out2 = [pad + colors.dim("[")];
3908
4028
  value.forEach((item, index) => {
3909
- const block = lines(item, colors, depth + 1);
4029
+ const block = lines2(item, colors, depth + 1);
3910
4030
  out2.push(...index < value.length - 1 ? withComma(block) : block);
3911
4031
  });
3912
4032
  out2.push(pad + colors.dim("]"));
@@ -3918,7 +4038,7 @@ function lines(value, colors, depth) {
3918
4038
  }
3919
4039
  const out = [pad + colors.dim("{")];
3920
4040
  entries.forEach(([key, val], index) => {
3921
- const block = lines(val, colors, depth + 1);
4041
+ const block = lines2(val, colors, depth + 1);
3922
4042
  const first = block[0] ?? "";
3923
4043
  block[0] = `${inner}${colors.cyan(JSON.stringify(key))}: ${first.slice(inner.length)}`;
3924
4044
  out.push(...index < entries.length - 1 ? withComma(block) : block);
@@ -3927,7 +4047,7 @@ function lines(value, colors, depth) {
3927
4047
  return out;
3928
4048
  }
3929
4049
  function prettyJson(value, colors) {
3930
- return lines(value, colors, 0).join("\n");
4050
+ return lines2(value, colors, 0).join("\n");
3931
4051
  }
3932
4052
  function compactJson(value) {
3933
4053
  return JSON.stringify(value);
@@ -4122,8 +4242,9 @@ async function pickProfile(ctx) {
4122
4242
  items.forEach((profile, index2) => {
4123
4243
  console.log(`${index2 + 1}. ${profile.name ?? profile.id}`);
4124
4244
  });
4125
- const answer = await prompt("Select a profile [1]: ");
4126
- const index = answer.trim() === "" ? 0 : Number(answer) - 1;
4245
+ const answer = await prompt("Select a profile [1]:");
4246
+ closePrompts();
4247
+ const index = answer === "" ? 0 : Number(answer) - 1;
4127
4248
  const chosen = items[index];
4128
4249
  if (!Number.isInteger(index) || index < 0 || chosen === void 0) {
4129
4250
  throw new Error(`Pick a number between 1 and ${items.length}.`);
@@ -4257,36 +4378,6 @@ function makeClient(deps) {
4257
4378
  };
4258
4379
  }
4259
4380
 
4260
- // src/output/colors.ts
4261
- var IDENTITY_COLORS = {
4262
- dim: (text) => text,
4263
- bold: (text) => text,
4264
- cyan: (text) => text,
4265
- green: (text) => text,
4266
- yellow: (text) => text,
4267
- red: (text) => text,
4268
- magenta: (text) => text
4269
- };
4270
- function wrap(code) {
4271
- return (text) => `\x1B[${code}m${text}\x1B[0m`;
4272
- }
4273
- var ANSI_COLORS = {
4274
- dim: wrap("2"),
4275
- bold: wrap("1"),
4276
- cyan: wrap("36"),
4277
- green: wrap("32"),
4278
- yellow: wrap("33"),
4279
- red: wrap("31"),
4280
- magenta: wrap("35")
4281
- };
4282
- function pickColors(options) {
4283
- const stream = options.stream ?? process.stdout;
4284
- if (options.noColor || process.env["NO_COLOR"] !== void 0 || !stream.isTTY) {
4285
- return IDENTITY_COLORS;
4286
- }
4287
- return ANSI_COLORS;
4288
- }
4289
-
4290
4381
  // ../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/classic/external.js
4291
4382
  var external_exports = {};
4292
4383
  __export(external_exports, {
@@ -5711,14 +5802,14 @@ function toDotPath(_path) {
5711
5802
  return segs.join("");
5712
5803
  }
5713
5804
  function prettifyError(error51) {
5714
- const lines2 = [];
5805
+ const lines3 = [];
5715
5806
  const issues = [...error51.issues].sort((a, b) => (a.path ?? []).length - (b.path ?? []).length);
5716
5807
  for (const issue2 of issues) {
5717
- lines2.push(`\u2716 ${issue2.message}`);
5808
+ lines3.push(`\u2716 ${issue2.message}`);
5718
5809
  if (issue2.path?.length)
5719
- lines2.push(` \u2192 at ${toDotPath(issue2.path)}`);
5810
+ lines3.push(` \u2192 at ${toDotPath(issue2.path)}`);
5720
5811
  }
5721
- return lines2.join("\n");
5812
+ return lines3.join("\n");
5722
5813
  }
5723
5814
 
5724
5815
  // ../node_modules/.pnpm/zod@4.4.3/node_modules/zod/v4/core/parse.js
@@ -6536,9 +6627,9 @@ var Doc = class {
6536
6627
  return;
6537
6628
  }
6538
6629
  const content = arg;
6539
- const lines2 = content.split("\n").filter((x) => x);
6540
- const minIndent = Math.min(...lines2.map((x) => x.length - x.trimStart().length));
6541
- const dedented = lines2.map((x) => x.slice(minIndent)).map((x) => " ".repeat(this.indent * 2) + x);
6630
+ const lines3 = content.split("\n").filter((x) => x);
6631
+ const minIndent = Math.min(...lines3.map((x) => x.length - x.trimStart().length));
6632
+ const dedented = lines3.map((x) => x.slice(minIndent)).map((x) => " ".repeat(this.indent * 2) + x);
6542
6633
  for (const line of dedented) {
6543
6634
  this.content.push(line);
6544
6635
  }
@@ -6547,8 +6638,8 @@ var Doc = class {
6547
6638
  const F = Function;
6548
6639
  const args = this?.args;
6549
6640
  const content = this?.content ?? [``];
6550
- const lines2 = [...content.map((x) => ` ${x}`)];
6551
- return new F(...args, lines2.join("\n"));
6641
+ const lines3 = [...content.map((x) => ` ${x}`)];
6642
+ return new F(...args, lines3.join("\n"));
6552
6643
  }
6553
6644
  };
6554
6645
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestica/cli",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "Command-line client for the Nestica REST API",
6
6
  "license": "MIT",