+123
| #!/usr/bin/env node | ||
| import { Command } from "commander"; | ||
| import { input, select } from "@inquirer/prompts"; | ||
| import Table from "cli-table3"; | ||
| import consola, { consola as consola$1 } from "consola"; | ||
| import { mkdirSync, readFileSync, writeFileSync } from "fs"; | ||
| import { homedir } from "os"; | ||
| import path from "path"; | ||
| //#region src/basefs.ts | ||
| const dir = path.join(homedir(), ".config", "subscription-cli", "subscription.json"); | ||
| const getSubscriptions = () => { | ||
| try { | ||
| const result = readFileSync(dir, "utf-8"); | ||
| return JSON.parse(result).filter(Boolean); | ||
| } catch (err) { | ||
| return []; | ||
| } | ||
| }; | ||
| const writeSubscription = (SharedArgs) => { | ||
| mkdirSync(path.dirname(dir), { recursive: true }); | ||
| const json = JSON.stringify(SharedArgs, null, 2); | ||
| try { | ||
| writeFileSync(dir, json, "utf-8"); | ||
| consola.success("done add subscription"); | ||
| } catch (error) { | ||
| consola.error("ファイルを作成することができませんでした"); | ||
| } | ||
| }; | ||
| const deleteSubscription = (id) => { | ||
| writeSubscription(getSubscriptions().filter((n) => n.id !== id)); | ||
| }; | ||
| const tagsSubscription = (tags) => { | ||
| return getSubscriptions().filter((item) => tags.every((n) => item.tags.includes(n))); | ||
| }; | ||
| //#endregion | ||
| //#region src/table.ts | ||
| const spreadSubscription = (get) => { | ||
| const list = get ?? getSubscriptions(); | ||
| if (list.length === 0) { | ||
| consola$1.info("No subscriptions found"); | ||
| return; | ||
| } | ||
| const table = new Table({ head: [ | ||
| "name", | ||
| "cycle", | ||
| "tags", | ||
| "price" | ||
| ] }); | ||
| for (const sub of list) table.push([ | ||
| String(sub.name), | ||
| String(sub.cycle), | ||
| String(sub.tags.join(" | ")), | ||
| sub.currency === "USD" ? String(`$${sub.price}`) : String(`\\${sub.price}`) | ||
| ]); | ||
| const usdtotal = list.filter((n) => n.currency === "USD").reduce((sum, n) => sum + n.price, 0); | ||
| const jpytotal = list.filter((n) => n.currency === "JPY").reduce((sum, n) => sum + n.price, 0); | ||
| table.push([ | ||
| "", | ||
| "", | ||
| "JPY TOTAL", | ||
| `\\${jpytotal}` | ||
| ], [ | ||
| "", | ||
| "", | ||
| "USD TOTAL", | ||
| `$${usdtotal}` | ||
| ]); | ||
| consola$1.log(table.toString()); | ||
| }; | ||
| //#endregion | ||
| //#region src/index.ts | ||
| const runCLI = () => { | ||
| const program = new Command(); | ||
| program.name("subsc-cli"); | ||
| program.command("list").action(() => { | ||
| spreadSubscription(); | ||
| }); | ||
| program.command("add").action(async () => { | ||
| const name = await input({ message: "subscription name" }); | ||
| const price = await input({ message: "payment subscribe service" }); | ||
| const currency = await select({ | ||
| message: "currency", | ||
| choices: [{ | ||
| label: "JPY", | ||
| value: "JPY" | ||
| }, { | ||
| label: "USD", | ||
| value: "USD" | ||
| }] | ||
| }); | ||
| const cycle = await select({ | ||
| message: "cycle", | ||
| choices: [{ | ||
| label: "monthly", | ||
| value: "monthly" | ||
| }, { | ||
| label: "yearly", | ||
| value: "yearly" | ||
| }] | ||
| }); | ||
| const tag = (await input({ message: "tags" })).split(",").map((tag) => tag.trim()).filter(Boolean); | ||
| const get = getSubscriptions(); | ||
| get.push({ | ||
| id: Math.max(0, ...get.map((s) => s.id)) + 1, | ||
| name, | ||
| price: Number(price), | ||
| currency, | ||
| cycle, | ||
| tags: tag | ||
| }); | ||
| writeSubscription(get); | ||
| }); | ||
| program.command("delete").argument("<number>").action((number) => { | ||
| deleteSubscription(Number(number)); | ||
| }); | ||
| program.command("tags").argument("<...text>").action((text) => { | ||
| spreadSubscription(tagsSubscription(text)); | ||
| }); | ||
| program.parse(); | ||
| }; | ||
| runCLI(); | ||
| //#endregion | ||
| export {}; |
+1
-1
| { | ||
| "name": "subsc-cli", | ||
| "version": "0.2.6", | ||
| "version": "0.3.0", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
6382
105.28%5
25%150
455.56%2
100%