@hisaabo/cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/.turbo/turbo-build.log +13 -0
  2. package/.turbo/turbo-typecheck.log +4 -0
  3. package/CLI-UX-ARCHITECTURE.md +1269 -0
  4. package/dist/bin/hisaabo.js +3516 -0
  5. package/package.json +34 -0
  6. package/src/auth.ts +212 -0
  7. package/src/bin/hisaabo.ts +950 -0
  8. package/src/client.ts +1229 -0
  9. package/src/commands/bank/index.ts +244 -0
  10. package/src/commands/dashboard.ts +105 -0
  11. package/src/commands/expense/create.ts +51 -0
  12. package/src/commands/expense/delete.ts +45 -0
  13. package/src/commands/expense/list.ts +65 -0
  14. package/src/commands/gst/index.ts +116 -0
  15. package/src/commands/import/index.ts +100 -0
  16. package/src/commands/invoice/create.ts +262 -0
  17. package/src/commands/invoice/delete.ts +44 -0
  18. package/src/commands/invoice/get.ts +105 -0
  19. package/src/commands/invoice/list.ts +126 -0
  20. package/src/commands/invoice/pdf.ts +75 -0
  21. package/src/commands/invoice/status.ts +41 -0
  22. package/src/commands/item/create.ts +98 -0
  23. package/src/commands/item/delete.ts +44 -0
  24. package/src/commands/item/list.ts +81 -0
  25. package/src/commands/item/stock.ts +46 -0
  26. package/src/commands/party/create.ts +88 -0
  27. package/src/commands/party/delete.ts +44 -0
  28. package/src/commands/party/get.ts +42 -0
  29. package/src/commands/party/ledger.ts +63 -0
  30. package/src/commands/party/list.ts +69 -0
  31. package/src/commands/payment/create.ts +133 -0
  32. package/src/commands/payment/delete.ts +46 -0
  33. package/src/commands/payment/list.ts +66 -0
  34. package/src/commands/report/index.ts +218 -0
  35. package/src/commands/shipment/index.ts +170 -0
  36. package/src/commands/store/index.ts +77 -0
  37. package/src/commands/target/index.ts +118 -0
  38. package/src/components/ErrorDisplay.tsx +16 -0
  39. package/src/components/Header.tsx +26 -0
  40. package/src/components/MoneyCell.tsx +16 -0
  41. package/src/components/Spinner.tsx +12 -0
  42. package/src/components/StatusBadge.tsx +39 -0
  43. package/src/components/Table.tsx +63 -0
  44. package/src/config.ts +47 -0
  45. package/src/format.ts +180 -0
  46. package/src/output.ts +227 -0
  47. package/tsconfig.json +22 -0
  48. package/tsup.config.ts +13 -0
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@hisaabo/cli",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "bin": {
7
+ "hisaabo": "./dist/bin/hisaabo.js"
8
+ },
9
+ "scripts": {
10
+ "build": "tsup",
11
+ "dev": "tsx src/bin/hisaabo.ts",
12
+ "typecheck": "tsc --noEmit"
13
+ },
14
+ "dependencies": {
15
+ "@hisaabo/shared": "workspace:*",
16
+ "@inkjs/ui": "^2.0.0",
17
+ "chalk": "^5.4.0",
18
+ "commander": "^13.0.0",
19
+ "conf": "^13.0.0",
20
+ "ink": "^5.2.0",
21
+ "open": "^10.1.0",
22
+ "react": "^18.3.1",
23
+ "superjson": "^2.2.0",
24
+ "zod": "^3.24.0"
25
+ },
26
+ "devDependencies": {
27
+ "@hisaabo/api": "workspace:*",
28
+ "@types/node": "^22.0.0",
29
+ "@types/react": "^18.3.0",
30
+ "tsup": "^8.3.0",
31
+ "tsx": "^4.19.0",
32
+ "typescript": "^5.7.0"
33
+ }
34
+ }
package/src/auth.ts ADDED
@@ -0,0 +1,212 @@
1
+ import { HisaaboClient, HisaaboApiError } from "./client.js";
2
+ import { getConfig, setConfig, clearConfig, requireAuth, getConfigPath } from "./config.js";
3
+ import { fatalError, EXIT, outputJSON, success } from "./output.js";
4
+
5
+ /**
6
+ * Authenticate using a long-lived API key (hisaabo_key_...).
7
+ * Validates the token by calling auth.me, then stores it in config.
8
+ */
9
+ export async function loginWithToken(apiUrl: string, token: string): Promise<void> {
10
+ const base = apiUrl.replace(/\/$/, "");
11
+
12
+ // Use a temporary client with the token but no business/tenant yet
13
+ const client = new HisaaboClient({
14
+ apiUrl: base,
15
+ token,
16
+ tenantId: "",
17
+ businessId: "",
18
+ });
19
+
20
+ let user: AuthUser;
21
+ try {
22
+ user = await client.auth.me();
23
+ } catch (e) {
24
+ if (e instanceof HisaaboApiError) {
25
+ const err = e.hisaaboError;
26
+ if (err.code === "unauthorized") fatalError("Invalid or expired API key.", EXIT.AUTH);
27
+ if (err.code === "network_error") fatalError("Cannot reach server: " + err.message, EXIT.NETWORK);
28
+ }
29
+ fatalError(String(e instanceof Error ? e.message : e), EXIT.GENERAL);
30
+ return; // unreachable — fatalError throws, but satisfies TS control flow
31
+ }
32
+
33
+ // Temporarily store the token so business.list() can authenticate
34
+ setConfig({ apiUrl: base, token });
35
+
36
+ const authedClient = new HisaaboClient({
37
+ apiUrl: base,
38
+ token,
39
+ tenantId: "",
40
+ businessId: "",
41
+ });
42
+
43
+ let businesses: BusinessSummary[];
44
+ try {
45
+ businesses = await authedClient.business.list();
46
+ } catch {
47
+ businesses = [];
48
+ }
49
+
50
+ if (businesses.length === 0) {
51
+ fatalError("No businesses found for this account.", EXIT.GENERAL);
52
+ }
53
+
54
+ console.log("\n You have access to " + businesses.length + " business" + (businesses.length > 1 ? "es" : "") + ":\n");
55
+ console.log(" # Business" + " ".repeat(22) + "GSTIN" + " ".repeat(15) + "Role");
56
+ console.log(" " + "─".repeat(58));
57
+ businesses.forEach((b, i) => {
58
+ const name = b.name.padEnd(26);
59
+ const gstin = (b.gstin ?? "-").padEnd(19);
60
+ console.log(` ${i + 1} ${name} ${gstin} ${b.gstRegistrationType ?? "member"}`);
61
+ });
62
+ console.log();
63
+
64
+ let selected = businesses[0];
65
+ if (businesses.length > 1) {
66
+ const readline = await import("readline");
67
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
68
+ const answer = await new Promise<string>((resolve) => {
69
+ rl.question(" Select business [1]: ", resolve);
70
+ });
71
+ rl.close();
72
+ const idx = parseInt(answer.trim() || "1", 10) - 1;
73
+ selected = businesses[Math.max(0, Math.min(idx, businesses.length - 1))];
74
+ }
75
+
76
+ if (!selected) {
77
+ fatalError("No business selected.", EXIT.GENERAL);
78
+ }
79
+
80
+ setConfig({
81
+ businessId: selected.id,
82
+ businessName: selected.name,
83
+ tenantId: selected.id,
84
+ });
85
+
86
+ success(`Authenticated as ${user.name ?? user.email} (${user.email}) via API key`);
87
+ console.log(" Active business: " + selected.name);
88
+ console.log(" Config saved to " + getConfigPath() + "\n");
89
+ }
90
+
91
+ // Type aliases used locally (mirrors what the client returns)
92
+ type AuthUser = { id: string; email: string; name: string | null; role: string };
93
+ type BusinessSummary = { id: string; name: string; gstin?: string | null; gstRegistrationType?: string | null };
94
+
95
+ export async function login(apiUrl: string, email: string, password: string): Promise<void> {
96
+ // Normalize URL
97
+ const base = apiUrl.replace(/\/$/, "");
98
+
99
+ // Use a temporary client without auth for login
100
+ const client = new HisaaboClient({
101
+ apiUrl: base,
102
+ token: "",
103
+ tenantId: "",
104
+ businessId: "",
105
+ });
106
+
107
+ try {
108
+ const result = await client.auth.login({ email, password });
109
+ // After login, fetch businesses
110
+ const authedClient = new HisaaboClient({
111
+ apiUrl: base,
112
+ token: result.sessionId,
113
+ tenantId: "",
114
+ businessId: "",
115
+ });
116
+ const businesses = await authedClient.business.list();
117
+
118
+ if (businesses.length === 0) {
119
+ fatalError("No businesses found for this account.", EXIT.GENERAL);
120
+ }
121
+
122
+ setConfig({
123
+ apiUrl: base,
124
+ token: result.sessionId,
125
+ });
126
+
127
+ // Return businesses for caller to handle selection
128
+ console.log("\n You have access to " + businesses.length + " business" + (businesses.length > 1 ? "es" : "") + ":\n");
129
+ console.log(" # Business" + " ".repeat(22) + "GSTIN" + " ".repeat(15) + "Role");
130
+ console.log(" " + "─".repeat(58));
131
+ businesses.forEach((b, i) => {
132
+ const name = b.name.padEnd(26);
133
+ const gstin = (b.gstin ?? "-").padEnd(19);
134
+ console.log(` ${i + 1} ${name} ${gstin} ${b.gstRegistrationType ?? "member"}`);
135
+ });
136
+ console.log();
137
+
138
+ // Default to first if only one
139
+ let selected = businesses[0];
140
+ if (businesses.length > 1) {
141
+ const readline = await import("readline");
142
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
143
+ const answer = await new Promise<string>((resolve) => {
144
+ rl.question(" Select business [1]: ", resolve);
145
+ });
146
+ rl.close();
147
+ const idx = parseInt(answer.trim() || "1", 10) - 1;
148
+ selected = businesses[Math.max(0, Math.min(idx, businesses.length - 1))];
149
+ }
150
+
151
+ if (!selected) {
152
+ fatalError("No business selected.", EXIT.GENERAL);
153
+ }
154
+
155
+ setConfig({
156
+ businessId: selected.id,
157
+ businessName: selected.name,
158
+ tenantId: selected.id, // fallback; real tenantId may differ
159
+ });
160
+
161
+ success(`Active business: ${selected.name}`);
162
+ console.log(" Config saved to " + getConfigPath());
163
+ console.log("\n You can switch businesses anytime with:");
164
+ console.log(" hisaabo business switch\n");
165
+ } catch (e) {
166
+ if (e instanceof HisaaboApiError) {
167
+ const err = e.hisaaboError;
168
+ if (err.code === "unauthorized") fatalError("Invalid email or password.", EXIT.AUTH);
169
+ if (err.code === "network_error") fatalError("Cannot reach server: " + err.message, EXIT.NETWORK);
170
+ }
171
+ fatalError(String(e instanceof Error ? e.message : e), EXIT.GENERAL);
172
+ }
173
+ }
174
+
175
+ export async function logout(): Promise<void> {
176
+ if (!getConfig().token) {
177
+ console.log("Not logged in.");
178
+ return;
179
+ }
180
+ try {
181
+ const cfg = requireAuth();
182
+ const client = new HisaaboClient(cfg);
183
+ await client.auth.logout();
184
+ } catch {
185
+ // ignore errors on logout
186
+ }
187
+ clearConfig();
188
+ success("Logged out.");
189
+ }
190
+
191
+ export async function whoami(jsonMode: boolean): Promise<void> {
192
+ const cfg = requireAuth();
193
+ const client = new HisaaboClient(cfg);
194
+ try {
195
+ const user = await client.auth.me();
196
+ if (jsonMode) {
197
+ outputJSON({ user, business: { id: cfg.businessId, name: cfg.businessName }, apiUrl: cfg.apiUrl });
198
+ return;
199
+ }
200
+ console.log(` User: ${user.name} <${user.email}>`);
201
+ console.log(` Role: ${user.role}`);
202
+ console.log(` Business: ${cfg.businessName} (${cfg.businessId})`);
203
+ console.log(` API: ${cfg.apiUrl}`);
204
+ } catch (e) {
205
+ if (e instanceof HisaaboApiError) {
206
+ const err = e.hisaaboError;
207
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
208
+ fatalError(e.message, EXIT.GENERAL);
209
+ }
210
+ fatalError(String(e instanceof Error ? e.message : e), EXIT.GENERAL);
211
+ }
212
+ }