@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/src/format.ts ADDED
@@ -0,0 +1,180 @@
1
+ import chalk from "chalk";
2
+ import { hasColor } from "./output.js";
3
+
4
+ // ── INR formatting ─────────────────────────────────────────────────────────
5
+
6
+ /**
7
+ * Format a decimal string as Indian-locale currency with ₹ symbol.
8
+ * Uses en-IN grouping (lakh/crore system).
9
+ */
10
+ export function formatINR(amount: string | number): string {
11
+ const num = typeof amount === "string" ? parseFloat(amount) : amount;
12
+ if (isNaN(num)) return "0.00";
13
+ const abs = Math.abs(num);
14
+ const formatted = new Intl.NumberFormat("en-IN", {
15
+ minimumFractionDigits: 2,
16
+ maximumFractionDigits: 2,
17
+ }).format(abs);
18
+ return (num < 0 ? "-" : "") + "₹" + formatted;
19
+ }
20
+
21
+ /**
22
+ * Format for table cells — no ₹ symbol (column header has it).
23
+ */
24
+ export function formatAmount(amount: string | number): string {
25
+ const num = typeof amount === "string" ? parseFloat(amount) : amount;
26
+ if (isNaN(num)) return "0.00";
27
+ const abs = Math.abs(num);
28
+ const formatted = new Intl.NumberFormat("en-IN", {
29
+ minimumFractionDigits: 2,
30
+ maximumFractionDigits: 2,
31
+ }).format(abs);
32
+ return (num < 0 ? "-" : "") + formatted;
33
+ }
34
+
35
+ // ── Date formatting ────────────────────────────────────────────────────────
36
+
37
+ const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
38
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
39
+
40
+ /**
41
+ * Format ISO date string as "dd MMM yyyy" (en-IN style).
42
+ */
43
+ export function formatDate(date: string | null | undefined): string {
44
+ if (!date) return "-";
45
+ try {
46
+ const d = new Date(date);
47
+ if (isNaN(d.getTime())) return date;
48
+ const day = String(d.getDate()).padStart(2, "0");
49
+ const month = MONTHS[d.getMonth()];
50
+ const year = d.getFullYear();
51
+ return `${day} ${month} ${year}`;
52
+ } catch {
53
+ return date;
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Relative date — "Today", "Yesterday", "2d ago", etc.
59
+ */
60
+ export function formatRelativeDate(date: string | null | undefined): string {
61
+ if (!date) return "-";
62
+ try {
63
+ const d = new Date(date);
64
+ const now = new Date();
65
+ const diff = Math.floor((now.getTime() - d.getTime()) / (1000 * 60 * 60 * 24));
66
+ if (diff === 0) return "Today";
67
+ if (diff === 1) return "Yesterday";
68
+ if (diff < 30) return `${diff}d ago`;
69
+ if (diff < 365) return `${Math.floor(diff / 30)}mo ago`;
70
+ return `${Math.floor(diff / 365)}y ago`;
71
+ } catch {
72
+ return date ?? "-";
73
+ }
74
+ }
75
+
76
+ // ── Status badges ──────────────────────────────────────────────────────────
77
+
78
+ const STATUS_CONFIG: Record<string, { label: string; color: (s: string) => string }> = {
79
+ paid: { label: "PAID", color: (s) => chalk.green(s) },
80
+ sent: { label: "SENT", color: (s) => chalk.blue(s) },
81
+ draft: { label: "DRAFT", color: (s) => chalk.dim(s) },
82
+ partial: { label: "PARTIAL", color: (s) => chalk.yellow(s) },
83
+ overdue: { label: "OVERDUE", color: (s) => chalk.red(s) },
84
+ cancelled: { label: "CANCEL", color: (s) => chalk.dim(chalk.strikethrough(s)) },
85
+ unfulfilled: { label: "UNFUL", color: (s) => chalk.blue(s) },
86
+ pending: { label: "PEND", color: (s) => chalk.yellow(s) },
87
+ confirmed: { label: "CONF", color: (s) => chalk.blue(s) },
88
+ delivered: { label: "DELIV", color: (s) => chalk.green(s) },
89
+ shipped: { label: "SHIPPED", color: (s) => chalk.blue(s) },
90
+ in_transit: { label: "TRANSIT", color: (s) => chalk.cyan(s) },
91
+ returned: { label: "RETURN", color: (s) => chalk.red(s) },
92
+ preparing: { label: "PREP", color: (s) => chalk.yellow(s) },
93
+ ready: { label: "READY", color: (s) => chalk.cyan(s) },
94
+ };
95
+
96
+ export function formatStatus(status: string): string {
97
+ const cfg = STATUS_CONFIG[status.toLowerCase()];
98
+ const label = cfg ? `[${cfg.label}]` : `[${status.toUpperCase()}]`;
99
+ if (!hasColor() || !cfg) return label;
100
+ return cfg.color(label);
101
+ }
102
+
103
+ // ── Financial year ─────────────────────────────────────────────────────────
104
+
105
+ /**
106
+ * Current FY string, e.g. "2025-26". Assumes FY starts April 1.
107
+ */
108
+ export function currentFY(): string {
109
+ const now = new Date();
110
+ const year = now.getFullYear();
111
+ const month = now.getMonth() + 1; // 1-indexed
112
+ if (month >= 4) {
113
+ return `${year}-${String(year + 1).slice(2)}`;
114
+ }
115
+ return `${year - 1}-${String(year).slice(2)}`;
116
+ }
117
+
118
+ /**
119
+ * FY start date as ISO string (April 1 of current FY).
120
+ */
121
+ export function fyStart(): string {
122
+ const now = new Date();
123
+ const year = now.getMonth() + 1 >= 4 ? now.getFullYear() : now.getFullYear() - 1;
124
+ return `${year}-04-01`;
125
+ }
126
+
127
+ /**
128
+ * Today as ISO date string.
129
+ */
130
+ export function todayISO(): string {
131
+ return new Date().toISOString().split("T")[0] ?? "";
132
+ }
133
+
134
+ /**
135
+ * First day of current month.
136
+ */
137
+ export function monthStart(): string {
138
+ const now = new Date();
139
+ return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-01`;
140
+ }
141
+
142
+ /**
143
+ * Last day of current month.
144
+ */
145
+ export function monthEnd(): string {
146
+ const now = new Date();
147
+ const last = new Date(now.getFullYear(), now.getMonth() + 1, 0);
148
+ return `${last.getFullYear()}-${String(last.getMonth() + 1).padStart(2, "0")}-${String(last.getDate()).padStart(2, "0")}`;
149
+ }
150
+
151
+ /**
152
+ * Quarter start/end for a quarter string like "Q1", "Q2", "Q3", "Q4".
153
+ * Q1 = Apr-Jun, Q2 = Jul-Sep, Q3 = Oct-Dec, Q4 = Jan-Mar
154
+ */
155
+ export function quarterRange(q: string): { from: string; to: string; month: number; year: number } {
156
+ const now = new Date();
157
+ const fyYear = now.getMonth() + 1 >= 4 ? now.getFullYear() : now.getFullYear() - 1;
158
+
159
+ const ranges: Record<string, { from: string; to: string; month: number; year: number }> = {
160
+ Q1: { from: `${fyYear}-04-01`, to: `${fyYear}-06-30`, month: 4, year: fyYear },
161
+ Q2: { from: `${fyYear}-07-01`, to: `${fyYear}-09-30`, month: 7, year: fyYear },
162
+ Q3: { from: `${fyYear}-10-01`, to: `${fyYear}-12-31`, month: 10, year: fyYear },
163
+ Q4: { from: `${fyYear + 1}-01-01`, to: `${fyYear + 1}-03-31`, month: 1, year: fyYear + 1 },
164
+ };
165
+
166
+ const upper = q.toUpperCase();
167
+ return ranges[upper] ?? ranges["Q1"]!;
168
+ }
169
+
170
+ export function deliveryMethodLabel(method: string): string {
171
+ const map: Record<string, string> = {
172
+ self_pickup: "Self Pickup",
173
+ hand_delivery: "Hand Delivery",
174
+ courier: "Courier",
175
+ bus: "Bus",
176
+ transport: "Transport",
177
+ post: "Post",
178
+ };
179
+ return map[method] ?? method;
180
+ }
package/src/output.ts ADDED
@@ -0,0 +1,227 @@
1
+ import chalk from "chalk";
2
+
3
+ // ── Environment detection ──────────────────────────────────────────────────
4
+
5
+ export function isInteractive(): boolean {
6
+ return process.stdout.isTTY === true;
7
+ }
8
+
9
+ export function hasColor(): boolean {
10
+ if (process.env["NO_COLOR"] !== undefined) return false;
11
+ if (process.env["FORCE_COLOR"] === "0") return false;
12
+ return process.stdout.hasColors?.() ?? isInteractive();
13
+ }
14
+
15
+ // ── Strip ANSI ─────────────────────────────────────────────────────────────
16
+
17
+ export function stripAnsi(str: string): string {
18
+ // eslint-disable-next-line no-control-regex
19
+ return str.replace(/\x1b\[[0-9;]*m/g, "");
20
+ }
21
+
22
+ // ── Terminal width tiers ──────────────────────────────────────────────────
23
+
24
+ export type WidthTier = "narrow" | "standard" | "wide";
25
+
26
+ export function getWidthTier(): WidthTier {
27
+ const w = process.stdout.columns ?? 80;
28
+ if (w < 80) return "narrow";
29
+ if (w <= 120) return "standard";
30
+ return "wide";
31
+ }
32
+
33
+ export function termWidth(): number {
34
+ return process.stdout.columns ?? 80;
35
+ }
36
+
37
+ // ── Exit codes ────────────────────────────────────────────────────────────
38
+
39
+ export const EXIT = {
40
+ SUCCESS: 0,
41
+ GENERAL: 1,
42
+ USAGE: 2,
43
+ AUTH: 3,
44
+ FORBIDDEN: 4,
45
+ NOT_FOUND: 5,
46
+ VALIDATION: 6,
47
+ NETWORK: 7,
48
+ CONFLICT: 8,
49
+ } as const;
50
+
51
+ // ── Output helpers ────────────────────────────────────────────────────────
52
+
53
+ export function fatalError(message: string, code = 1): never {
54
+ if (hasColor()) {
55
+ process.stderr.write(chalk.red("Error: ") + message + "\n");
56
+ } else {
57
+ process.stderr.write("Error: " + message + "\n");
58
+ }
59
+ process.exit(code);
60
+ }
61
+
62
+ export function success(message: string): void {
63
+ if (hasColor()) {
64
+ process.stdout.write(chalk.green("✓ ") + message + "\n");
65
+ } else {
66
+ process.stdout.write("OK: " + message + "\n");
67
+ }
68
+ }
69
+
70
+ export function warn(message: string): void {
71
+ if (hasColor()) {
72
+ process.stderr.write(chalk.yellow("Warning: ") + message + "\n");
73
+ } else {
74
+ process.stderr.write("Warning: " + message + "\n");
75
+ }
76
+ }
77
+
78
+ // ── JSON output ───────────────────────────────────────────────────────────
79
+
80
+ export function outputJSON(data: unknown): void {
81
+ process.stdout.write(JSON.stringify(data, null, 2) + "\n");
82
+ }
83
+
84
+ // ── Table output ──────────────────────────────────────────────────────────
85
+
86
+ export interface ColumnDef<T extends object> {
87
+ key: keyof T | string;
88
+ header: string;
89
+ width?: number;
90
+ align?: "left" | "right";
91
+ format?: (value: unknown, row: T) => string;
92
+ }
93
+
94
+ function getNestedValue(obj: object, key: string): unknown {
95
+ const parts = key.split(".");
96
+ let cur: unknown = obj;
97
+ for (const part of parts) {
98
+ if (cur == null || typeof cur !== "object") return undefined;
99
+ cur = (cur as Record<string, unknown>)[part];
100
+ }
101
+ return cur;
102
+ }
103
+
104
+ function padEnd(str: string, len: number): string {
105
+ const visual = stripAnsi(str).length;
106
+ const pad = Math.max(0, len - visual);
107
+ return str + " ".repeat(pad);
108
+ }
109
+
110
+ function padStart(str: string, len: number): string {
111
+ const visual = stripAnsi(str).length;
112
+ const pad = Math.max(0, len - visual);
113
+ return " ".repeat(pad) + str;
114
+ }
115
+
116
+ export function outputTable<T extends object>(rows: T[], columns: ColumnDef<T>[]): void {
117
+ if (rows.length === 0) {
118
+ process.stdout.write(" (no results)\n");
119
+ return;
120
+ }
121
+
122
+ // Compute column widths
123
+ const widths = columns.map((col) => {
124
+ let max = stripAnsi(col.header).length;
125
+ for (const row of rows) {
126
+ const val = col.format
127
+ ? col.format(getNestedValue(row, col.key as string), row)
128
+ : String(getNestedValue(row, col.key as string) ?? "");
129
+ const vis = stripAnsi(val).length;
130
+ if (vis > max) max = vis;
131
+ }
132
+ if (col.width) max = Math.min(max, col.width);
133
+ return max;
134
+ });
135
+
136
+ const sep = "─".repeat(widths.reduce((s, w) => s + w + 2, 1));
137
+
138
+ // Header
139
+ let header = " ";
140
+ for (let i = 0; i < columns.length; i++) {
141
+ const col = columns[i];
142
+ const h = col.align === "right"
143
+ ? padStart(col.header, widths[i])
144
+ : padEnd(col.header, widths[i]);
145
+ header += (hasColor() ? chalk.dim(h) : h) + " ";
146
+ }
147
+ process.stdout.write(" " + (hasColor() ? chalk.dim(sep) : sep) + "\n");
148
+ process.stdout.write(header.trimEnd() + "\n");
149
+ process.stdout.write(" " + (hasColor() ? chalk.dim(sep) : sep) + "\n");
150
+
151
+ // Rows
152
+ for (const row of rows) {
153
+ let line = " ";
154
+ for (let i = 0; i < columns.length; i++) {
155
+ const col = columns[i];
156
+ let val = col.format
157
+ ? col.format(getNestedValue(row, col.key as string), row)
158
+ : String(getNestedValue(row, col.key as string) ?? "");
159
+ // Truncate if needed
160
+ const vis = stripAnsi(val).length;
161
+ if (col.width && vis > col.width) {
162
+ val = val.slice(0, col.width - 1) + "…";
163
+ }
164
+ const cell = col.align === "right"
165
+ ? padStart(val, widths[i])
166
+ : padEnd(val, widths[i]);
167
+ line += cell + " ";
168
+ }
169
+ process.stdout.write(line.trimEnd() + "\n");
170
+ }
171
+
172
+ process.stdout.write(" " + (hasColor() ? chalk.dim(sep) : sep) + "\n");
173
+ }
174
+
175
+ // ── TSV output ────────────────────────────────────────────────────────────
176
+
177
+ export function outputTSV<T extends object>(rows: T[], columns: ColumnDef<T>[]): void {
178
+ process.stdout.write(columns.map((c) => c.header).join("\t") + "\n");
179
+ for (const row of rows) {
180
+ const line = columns.map((col) => {
181
+ const val = col.format
182
+ ? col.format(getNestedValue(row, col.key as string), row)
183
+ : String(getNestedValue(row, col.key as string) ?? "");
184
+ return stripAnsi(val).replace(/\t/g, " ");
185
+ });
186
+ process.stdout.write(line.join("\t") + "\n");
187
+ }
188
+ }
189
+
190
+ // ── CSV output ────────────────────────────────────────────────────────────
191
+
192
+ function csvEscape(val: string): string {
193
+ if (val.includes(",") || val.includes('"') || val.includes("\n")) {
194
+ return `"${val.replace(/"/g, '""')}"`;
195
+ }
196
+ return val;
197
+ }
198
+
199
+ export function outputCSV<T extends object>(rows: T[], columns: ColumnDef<T>[]): void {
200
+ process.stdout.write(columns.map((c) => csvEscape(c.header)).join(",") + "\n");
201
+ for (const row of rows) {
202
+ const line = columns.map((col) => {
203
+ const val = col.format
204
+ ? col.format(getNestedValue(row, col.key as string), row)
205
+ : String(getNestedValue(row, col.key as string) ?? "");
206
+ return csvEscape(stripAnsi(val));
207
+ });
208
+ process.stdout.write(line.join(",") + "\n");
209
+ }
210
+ }
211
+
212
+ // ── ID-only output ────────────────────────────────────────────────────────
213
+
214
+ export function outputIds(ids: string[]): void {
215
+ for (const id of ids) {
216
+ process.stdout.write(id + "\n");
217
+ }
218
+ }
219
+
220
+ // ── Pagination footer ─────────────────────────────────────────────────────
221
+
222
+ export function paginationFooter(page: number, limit: number, total: number): void {
223
+ const from = (page - 1) * limit + 1;
224
+ const to = Math.min(page * limit, total);
225
+ const msg = ` Showing ${from}-${to} of ${total}`;
226
+ process.stdout.write("\n" + (hasColor() ? chalk.dim(msg) : msg) + "\n");
227
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "jsx": "react-jsx",
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "outDir": "dist",
10
+ "rootDir": "src",
11
+ "declaration": true,
12
+ "declarationMap": true,
13
+ "sourceMap": true,
14
+ "resolveJsonModule": true,
15
+ "esModuleInterop": true,
16
+ "forceConsistentCasingInFileNames": true,
17
+ "paths": {
18
+ "@/*": ["./src/*"]
19
+ }
20
+ },
21
+ "include": ["src"]
22
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/bin/hisaabo.ts"],
5
+ format: ["esm"],
6
+ target: "node20",
7
+ banner: {
8
+ js: "#!/usr/bin/env node",
9
+ },
10
+ external: ["react", "ink", "@inkjs/ui"],
11
+ outDir: "dist/bin",
12
+ clean: true,
13
+ });