@rubicon-caliga/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.
- package/LICENSE +21 -0
- package/dist/args.d.ts +8 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +43 -0
- package/dist/args.js.map +1 -0
- package/dist/config.d.ts +12 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +28 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/format.d.ts +11 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +106 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +375 -0
- package/dist/index.js.map +1 -0
- package/dist/payments.d.ts +13 -0
- package/dist/payments.d.ts.map +1 -0
- package/dist/payments.js +52 -0
- package/dist/payments.js.map +1 -0
- package/dist/receipts.d.ts +12 -0
- package/dist/receipts.d.ts.map +1 -0
- package/dist/receipts.js +43 -0
- package/dist/receipts.js.map +1 -0
- package/package.json +45 -0
- package/src/args.ts +52 -0
- package/src/config.ts +38 -0
- package/src/errors.ts +15 -0
- package/src/format.ts +116 -0
- package/src/index.ts +422 -0
- package/src/payments.ts +66 -0
- package/src/receipts.ts +55 -0
package/src/receipts.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { ReadReceipt } from "@rubicon-caliga/agent-sdk";
|
|
5
|
+
import { CliError } from "./errors.js";
|
|
6
|
+
|
|
7
|
+
export interface StoredReceipt {
|
|
8
|
+
receiptId: string;
|
|
9
|
+
savedAt: string;
|
|
10
|
+
receipt: ReadReceipt;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function receiptsDir(): string {
|
|
14
|
+
return join(homedir(), ".rubicon", "receipts");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function receiptId(receipt: ReadReceipt): string {
|
|
18
|
+
return `${receipt.sessionId}-${receipt.articleId}`.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function saveReceipt(receipt: ReadReceipt): Promise<StoredReceipt> {
|
|
22
|
+
const stored = {
|
|
23
|
+
receiptId: receiptId(receipt),
|
|
24
|
+
savedAt: new Date().toISOString(),
|
|
25
|
+
receipt,
|
|
26
|
+
};
|
|
27
|
+
await mkdir(receiptsDir(), { recursive: true, mode: 0o700 });
|
|
28
|
+
await writeFile(join(receiptsDir(), `${stored.receiptId}.json`), `${JSON.stringify(stored, null, 2)}\n`, {
|
|
29
|
+
mode: 0o600,
|
|
30
|
+
});
|
|
31
|
+
return stored;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function listReceipts(): Promise<StoredReceipt[]> {
|
|
35
|
+
await mkdir(receiptsDir(), { recursive: true, mode: 0o700 });
|
|
36
|
+
const names = await readdir(receiptsDir());
|
|
37
|
+
const receipts = await Promise.all(
|
|
38
|
+
names
|
|
39
|
+
.filter((name) => name.endsWith(".json"))
|
|
40
|
+
.map(async (name) => JSON.parse(await readFile(join(receiptsDir(), name), "utf8")) as StoredReceipt),
|
|
41
|
+
);
|
|
42
|
+
return receipts.sort((left, right) => right.savedAt.localeCompare(left.savedAt));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function loadReceipt(id: string): Promise<StoredReceipt> {
|
|
46
|
+
const safeId = id.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(await readFile(join(receiptsDir(), `${safeId}.json`), "utf8")) as StoredReceipt;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
|
51
|
+
throw new CliError("RECEIPT_NOT_FOUND", `Receipt not found: ${id}`);
|
|
52
|
+
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|