terminalmarket 0.1.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 TerminalMarket
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # terminalmarket (CLI)
2
+
3
+ TerminalMarket CLI client for `terminalmarket.app`.
4
+
5
+ ## Install (global)
6
+
7
+ ```bash
8
+ npm i -g terminalmarket
9
+ ```
10
+
11
+ ## Configure API base (optional)
12
+
13
+ Defaults to:
14
+
15
+ - `https://terminalmarket.app/api`
16
+
17
+ Change it:
18
+
19
+ ```bash
20
+ tm config set api https://terminalmarket.app/api
21
+ tm config get api
22
+ ```
23
+
24
+ ## Commands
25
+
26
+ - `tm products [--limit N]`
27
+ - `tm categories`
28
+ - `tm category <category> [--limit N]`
29
+ - `tm search <query> [--limit N]` *(client-side search over /products)*
30
+ - `tm view <productId>`
31
+ - `tm buy <productId> [--no-open]` *(posts /clicks, then opens product.buyUrl)*
32
+
33
+ ## Notes
34
+
35
+ This CLI is built to match the current backend routes:
36
+
37
+ - `GET /api/products`
38
+ - `GET /api/products/:id`
39
+ - `GET /api/products/category/:category`
40
+ - `GET /api/categories`
41
+ - `POST /api/clicks`
package/bin/tm.js ADDED
@@ -0,0 +1,209 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from "commander";
4
+ import chalk from "chalk";
5
+ import open from "open";
6
+
7
+ import { apiGet, apiPost } from "../src/api.js";
8
+ import { getApiBase, setApiBase } from "../src/config.js";
9
+ import { printTable, pickProductFields, containsQuery } from "../src/format.js";
10
+
11
+ const program = new Command();
12
+
13
+ program
14
+ .name("tm")
15
+ .description("TerminalMarket CLI — marketplace for developers")
16
+ .version("0.1.1");
17
+
18
+ // -----------------
19
+ // config
20
+ // -----------------
21
+ const config = program
22
+ .command("config")
23
+ .description("Get/set CLI config");
24
+
25
+ config
26
+ .command("get <key>")
27
+ .description("Get a config value (e.g. api)")
28
+ .action((key) => {
29
+ if (key === "api") {
30
+ console.log(getApiBase());
31
+ return;
32
+ }
33
+ console.error(chalk.red(`Unknown key: ${key}`));
34
+ process.exitCode = 1;
35
+ });
36
+
37
+ config
38
+ .command("set <key> <value>")
39
+ .description("Set a config value (e.g. api)")
40
+ .action((key, value) => {
41
+ if (key === "api") {
42
+ setApiBase(value);
43
+ console.log(chalk.green(`api = ${getApiBase()}`));
44
+ return;
45
+ }
46
+ console.error(chalk.red(`Unknown key: ${key}`));
47
+ process.exitCode = 1;
48
+ });
49
+
50
+ // -----------------
51
+ // categories
52
+ // -----------------
53
+ program
54
+ .command("categories")
55
+ .description("List categories")
56
+ .action(async () => {
57
+ try {
58
+ const cats = await apiGet("/categories");
59
+ const rows = (cats || []).map((c) => ({ category: String(c) }));
60
+ printTable(rows, [{ key: "category", title: "category" }]);
61
+ } catch (e) {
62
+ console.error(chalk.red(e?.message || String(e)));
63
+ process.exitCode = 1;
64
+ }
65
+ });
66
+
67
+ // -----------------
68
+ // products
69
+ // -----------------
70
+ program
71
+ .command("products")
72
+ .description("List products")
73
+ .option("-l, --limit <n>", "Limit results", "20")
74
+ .action(async (opts) => {
75
+ try {
76
+ const limit = Math.max(1, Math.min(200, Number.parseInt(opts.limit, 10) || 20));
77
+ const products = await apiGet("/products");
78
+ const rows = (products || []).slice(0, limit).map(pickProductFields);
79
+ printTable(rows, [
80
+ { key: "id", title: "id" },
81
+ { key: "name", title: "name" },
82
+ { key: "price", title: "price" },
83
+ { key: "category", title: "category" },
84
+ ]);
85
+ } catch (e) {
86
+ console.error(chalk.red(e?.message || String(e)));
87
+ process.exitCode = 1;
88
+ }
89
+ });
90
+
91
+ program
92
+ .command("category <category>")
93
+ .description("List products in a category")
94
+ .option("-l, --limit <n>", "Limit results", "20")
95
+ .action(async (category, opts) => {
96
+ try {
97
+ const limit = Math.max(1, Math.min(200, Number.parseInt(opts.limit, 10) || 20));
98
+ const products = await apiGet(`/products/category/${encodeURIComponent(category)}`);
99
+ const rows = (products || []).slice(0, limit).map(pickProductFields);
100
+ printTable(rows, [
101
+ { key: "id", title: "id" },
102
+ { key: "name", title: "name" },
103
+ { key: "price", title: "price" },
104
+ { key: "category", title: "category" },
105
+ ]);
106
+ } catch (e) {
107
+ console.error(chalk.red(e?.message || String(e)));
108
+ process.exitCode = 1;
109
+ }
110
+ });
111
+
112
+ program
113
+ .command("search <query>")
114
+ .description("Search products (client-side)")
115
+ .option("-l, --limit <n>", "Limit results", "20")
116
+ .action(async (query, opts) => {
117
+ try {
118
+ const limit = Math.max(1, Math.min(200, Number.parseInt(opts.limit, 10) || 20));
119
+ const products = await apiGet("/products");
120
+ const q = String(query || "").trim();
121
+ const matched = (products || []).filter((p) => containsQuery(p, q));
122
+ const rows = matched.slice(0, limit).map(pickProductFields);
123
+ printTable(rows, [
124
+ { key: "id", title: "id" },
125
+ { key: "name", title: "name" },
126
+ { key: "price", title: "price" },
127
+ { key: "category", title: "category" },
128
+ ]);
129
+ if (matched.length > limit) {
130
+ console.log(chalk.dim(`Showing ${limit} of ${matched.length}. Use --limit to show more.`));
131
+ }
132
+ } catch (e) {
133
+ console.error(chalk.red(e?.message || String(e)));
134
+ process.exitCode = 1;
135
+ }
136
+ });
137
+
138
+ program
139
+ .command("view <productId>")
140
+ .description("View a product")
141
+ .action(async (productId) => {
142
+ try {
143
+ const p = await apiGet(`/products/${encodeURIComponent(productId)}`);
144
+ if (!p) {
145
+ console.error(chalk.red("Not found"));
146
+ process.exitCode = 1;
147
+ return;
148
+ }
149
+ console.log(chalk.bold(p.name || p.title || `Product ${productId}`));
150
+ if (p.description) console.log(p.description);
151
+ console.log("");
152
+ console.log(`${chalk.dim("id:")} ${p.id}`);
153
+ if (p.category) console.log(`${chalk.dim("category:")} ${p.category}`);
154
+ if (p.price) console.log(`${chalk.dim("price:")} ${p.price}`);
155
+ if (p.buyUrl) console.log(`${chalk.dim("buyUrl:")} ${p.buyUrl}`);
156
+ if (p.storeId) console.log(`${chalk.dim("storeId:")} ${p.storeId}`);
157
+ } catch (e) {
158
+ console.error(chalk.red(e?.message || String(e)));
159
+ process.exitCode = 1;
160
+ }
161
+ });
162
+
163
+ program
164
+ .command("buy <productId>")
165
+ .description("Record a click then open product buyUrl")
166
+ .option("--no-open", "Do not open a browser")
167
+ .action(async (productId, opts) => {
168
+ try {
169
+ const p = await apiGet(`/products/${encodeURIComponent(productId)}`);
170
+ if (!p) {
171
+ console.error(chalk.red("Not found"));
172
+ process.exitCode = 1;
173
+ return;
174
+ }
175
+
176
+ // best-effort click tracking
177
+ try {
178
+ await apiPost("/clicks", {
179
+ source: "cli",
180
+ productId: p.id,
181
+ storeId: p.storeId ?? null,
182
+ offerId: null,
183
+ });
184
+ } catch {
185
+ // ignore tracking failures
186
+ }
187
+
188
+ const url = p.buyUrl;
189
+ if (!url) {
190
+ console.error(chalk.red("This product has no buyUrl."));
191
+ process.exitCode = 1;
192
+ return;
193
+ }
194
+
195
+ console.log(chalk.green("Opening:"), url);
196
+ if (opts.open !== false) {
197
+ await open(url);
198
+ }
199
+ } catch (e) {
200
+ console.error(chalk.red(e?.message || String(e)));
201
+ process.exitCode = 1;
202
+ }
203
+ });
204
+
205
+ program.parse(process.argv);
206
+
207
+ if (!process.argv.slice(2).length) {
208
+ program.outputHelp();
209
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "terminalmarket",
3
+ "version": "0.1.1",
4
+ "description": "TerminalMarket CLI — marketplace for developers (client for terminalmarket.app)",
5
+ "bin": {
6
+ "tm": "./bin/tm.js"
7
+ },
8
+ "type": "module",
9
+ "keywords": [
10
+ "cli",
11
+ "marketplace",
12
+ "terminal",
13
+ "developers",
14
+ "food",
15
+ "subscription"
16
+ ],
17
+ "author": "TerminalMarket",
18
+ "license": "MIT",
19
+ "dependencies": {
20
+ "chalk": "^5.3.0",
21
+ "commander": "^12.1.0",
22
+ "conf": "^12.0.0",
23
+ "node-fetch": "^3.3.2",
24
+ "open": "^9.1.0"
25
+ }
26
+ }
package/src/api.js ADDED
@@ -0,0 +1,34 @@
1
+ import fetch from "node-fetch";
2
+ import { getApiBase } from "./config.js";
3
+
4
+ function joinUrl(base, path) {
5
+ if (!base) return path;
6
+ return base.replace(/\/$/, "") + "/" + path.replace(/^\//, "");
7
+ }
8
+
9
+ export async function apiGet(path) {
10
+ const url = joinUrl(getApiBase(), path);
11
+ const res = await fetch(url, { method: "GET" });
12
+ if (!res.ok) {
13
+ const text = await res.text().catch(() => "");
14
+ throw new Error(`GET ${url} failed: ${res.status} ${res.statusText}${text ? " — " + text : ""}`);
15
+ }
16
+ return res.json();
17
+ }
18
+
19
+ export async function apiPost(path, body) {
20
+ const url = joinUrl(getApiBase(), path);
21
+ const res = await fetch(url, {
22
+ method: "POST",
23
+ headers: { "Content-Type": "application/json" },
24
+ body: JSON.stringify(body ?? {})
25
+ });
26
+ if (!res.ok) {
27
+ const text = await res.text().catch(() => "");
28
+ throw new Error(`POST ${url} failed: ${res.status} ${res.statusText}${text ? " — " + text : ""}`);
29
+ }
30
+ // clicks endpoint can return empty or json
31
+ const ct = res.headers.get("content-type") || "";
32
+ if (ct.includes("application/json")) return res.json();
33
+ return { ok: true };
34
+ }
package/src/config.js ADDED
@@ -0,0 +1,12 @@
1
+ import Conf from "conf";
2
+
3
+ const conf = new Conf({ projectName: "terminalmarket" });
4
+
5
+ export function getApiBase() {
6
+ return conf.get("apiBase", "https://terminalmarket.app/api");
7
+ }
8
+
9
+ export function setApiBase(value) {
10
+ conf.set("apiBase", value);
11
+ return value;
12
+ }
package/src/format.js ADDED
@@ -0,0 +1,37 @@
1
+ import chalk from "chalk";
2
+
3
+ export function printTable(rows, columns) {
4
+ if (!rows?.length) {
5
+ console.log(chalk.gray("No results."));
6
+ return;
7
+ }
8
+ const widths = {};
9
+ for (const col of columns) {
10
+ widths[col.key] = Math.max(col.title.length, ...rows.map(r => String(r[col.key] ?? "").length));
11
+ }
12
+ const header = columns.map(c => chalk.bold(String(c.title).padEnd(widths[c.key]))).join(" ");
13
+ console.log(header);
14
+ console.log(columns.map(c => "-".repeat(widths[c.key])).join(" "));
15
+ for (const r of rows) {
16
+ const line = columns.map(c => String(r[c.key] ?? "").padEnd(widths[c.key])).join(" ");
17
+ console.log(line);
18
+ }
19
+ }
20
+
21
+ export function pickProductFields(p) {
22
+ return {
23
+ id: p?.id ?? "",
24
+ name: p?.name ?? p?.title ?? "",
25
+ category: p?.category ?? "",
26
+ price: p?.price ? `$${p.price}` : (p?.priceDisplay ?? ""),
27
+ buyUrl: p?.buyUrl ?? ""
28
+ };
29
+ }
30
+
31
+ export function containsQuery(p, q) {
32
+ const hay = [
33
+ p?.name, p?.title, p?.description, p?.shortDescription, p?.category,
34
+ ...(Array.isArray(p?.tags) ? p.tags : [])
35
+ ].filter(Boolean).join(" ").toLowerCase();
36
+ return hay.includes(q);
37
+ }