delivrie-cli 0.1.0 → 0.2.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 +1 -1
- package/README.md +2 -1
- package/bin/delivrie.js +1 -1
- package/dist/api.js +58 -0
- package/dist/args.js +47 -0
- package/dist/cli.js +697 -0
- package/dist/config.js +38 -0
- package/dist/format.js +42 -0
- package/dist/version.js +1 -0
- package/package.json +12 -5
- package/src/api.js +0 -50
- package/src/args.js +0 -30
- package/src/cli.js +0 -485
- package/src/config.js +0 -28
- package/src/format.js +0 -36
- package/src/version.js +0 -1
package/dist/config.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Where `delivrie login` keeps the API key: ~/.config/delivrie/config.json (or $XDG_CONFIG_HOME, or %APPDATA% on
|
|
2
|
+
// Windows), readable only by you. DELIVRIE_API_KEY always wins over the saved key.
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
export function configPath(env = process.env) {
|
|
7
|
+
const dir = env.DELIVRIE_CONFIG_DIR
|
|
8
|
+
|| (process.platform === "win32" && env.APPDATA ? path.join(env.APPDATA, "delivrie")
|
|
9
|
+
: path.join(env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "delivrie"));
|
|
10
|
+
return path.join(dir, "config.json");
|
|
11
|
+
}
|
|
12
|
+
export function readConfig(env = process.env) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(fs.readFileSync(configPath(env), "utf8")) || {};
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function writeConfig(cfg, env = process.env) {
|
|
21
|
+
const file = configPath(env);
|
|
22
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
23
|
+
fs.writeFileSync(file, JSON.stringify(cfg, null, 2) + "\n", { mode: 0o600 });
|
|
24
|
+
try {
|
|
25
|
+
fs.chmodSync(file, 0o600);
|
|
26
|
+
}
|
|
27
|
+
catch { /* Windows */ }
|
|
28
|
+
return file;
|
|
29
|
+
}
|
|
30
|
+
export function clearConfig(env = process.env) {
|
|
31
|
+
try {
|
|
32
|
+
fs.unlinkSync(configPath(env));
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function makeOut(stream = process.stdout, env = process.env) {
|
|
2
|
+
const color = !!stream.isTTY && !env.NO_COLOR && env.TERM !== "dumb";
|
|
3
|
+
const c = (code) => (s) => (color ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
4
|
+
const st = { bold: c(1), dim: c(2), red: c(31), green: c(32), yellow: c(33), cyan: c(36) };
|
|
5
|
+
const visible = (s) => String(s).replace(/\x1b\[[0-9;]*m/g, "");
|
|
6
|
+
function table(rows, cols) {
|
|
7
|
+
if (!rows.length)
|
|
8
|
+
return "";
|
|
9
|
+
const head = cols.map((k) => st.dim(k.label));
|
|
10
|
+
const body = rows.map((r) => cols.map((k) => String(k.get(r) ?? "")));
|
|
11
|
+
const widths = cols.map((k, i) => Math.max(visible(head[i]).length, ...body.map((row) => visible(row[i]).length)));
|
|
12
|
+
const line = (cells) => cells.map((v, i) => (i === cells.length - 1 ? v : v + " ".repeat(widths[i] - visible(v).length))).join(" ");
|
|
13
|
+
return [line(head), ...body.map(line)].join("\n");
|
|
14
|
+
}
|
|
15
|
+
const statusColor = (s) => {
|
|
16
|
+
const v = String(s || "");
|
|
17
|
+
if (/^(delivered|verified|sent|enabled|opened|clicked|published|full)$/.test(v))
|
|
18
|
+
return st.green(v);
|
|
19
|
+
if (/^(bounced|failed|complained|disabled|canceled)$/.test(v))
|
|
20
|
+
return st.red(v);
|
|
21
|
+
if (/^(pending|queued|scheduled|sending|delivery_delayed|not_started|temporary_failure|draft)$/.test(v))
|
|
22
|
+
return st.yellow(v);
|
|
23
|
+
return v;
|
|
24
|
+
};
|
|
25
|
+
return { ...st, color, table, statusColor };
|
|
26
|
+
}
|
|
27
|
+
export function when(iso) {
|
|
28
|
+
if (!iso)
|
|
29
|
+
return "";
|
|
30
|
+
const d = new Date(iso);
|
|
31
|
+
if (Number.isNaN(d.getTime()))
|
|
32
|
+
return String(iso);
|
|
33
|
+
const s = (Date.now() - d.getTime()) / 1000;
|
|
34
|
+
if (s >= 0 && s < 60)
|
|
35
|
+
return "just now";
|
|
36
|
+
if (s >= 0 && s < 3600)
|
|
37
|
+
return `${Math.floor(s / 60)} min ago`;
|
|
38
|
+
if (s >= 0 && s < 86400)
|
|
39
|
+
return `${Math.floor(s / 3600)} h ago`;
|
|
40
|
+
return d.toISOString().slice(0, 16).replace("T", " ");
|
|
41
|
+
}
|
|
42
|
+
export const list = (v) => (Array.isArray(v) ? v.join(", ") : (v ?? ""));
|
package/dist/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = "0.2.0";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "delivrie-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The Delivrie command-line tool: send email, check deliveries, and manage domains, API keys, webhooks and templates from your terminal.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"delivrie",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"homepage": "https://delivrie.io/docs/cli",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "git+https://github.com/
|
|
16
|
+
"url": "git+https://github.com/bysurce/delivrie.git",
|
|
17
17
|
"directory": "sdk/cli"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"author": "Surce Inc <support@delivrie.io>",
|
|
24
24
|
"type": "module",
|
|
25
25
|
"bin": {
|
|
26
|
-
"delivrie": "
|
|
26
|
+
"delivrie": "bin/delivrie.js"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"bin",
|
|
30
|
-
"
|
|
30
|
+
"dist",
|
|
31
31
|
"README.md",
|
|
32
32
|
"LICENSE"
|
|
33
33
|
],
|
|
@@ -35,6 +35,13 @@
|
|
|
35
35
|
"node": ">=18"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
|
-
"
|
|
38
|
+
"build": "node scripts/build.mjs",
|
|
39
|
+
"pretest": "npm run build",
|
|
40
|
+
"test": "node --test test/cli.test.js",
|
|
41
|
+
"prepublishOnly": "npm test"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "26.6.3",
|
|
45
|
+
"typescript": "6.0.3"
|
|
39
46
|
}
|
|
40
47
|
}
|
package/src/api.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// A small API client for the CLI: bearer key, JSON in and out, retries for rate limits and server errors.
|
|
2
|
-
// Sends (POST/PATCH) are only retried with an idempotency key, so a retry never sends an email twice.
|
|
3
|
-
import { VERSION } from "./version.js";
|
|
4
|
-
|
|
5
|
-
export class ApiError extends Error {
|
|
6
|
-
constructor(status, name, message) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.status = status;
|
|
9
|
-
this.code = name;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
14
|
-
|
|
15
|
-
export function createClient({ key, baseUrl, timeout = 30000, maxRetries = 2, fetchImpl = globalThis.fetch }) {
|
|
16
|
-
const base = baseUrl.replace(/\/+$/, "");
|
|
17
|
-
async function request(method, path, body, { query, idempotencyKey } = {}) {
|
|
18
|
-
let url = base + path;
|
|
19
|
-
if (query) {
|
|
20
|
-
const q = Object.entries(query).filter(([, v]) => v !== undefined && v !== null && v !== "").map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`).join("&");
|
|
21
|
-
if (q) url += (url.includes("?") ? "&" : "?") + q;
|
|
22
|
-
}
|
|
23
|
-
const headers = { authorization: `Bearer ${key}`, accept: "application/json", "user-agent": `delivrie-cli/${VERSION}` };
|
|
24
|
-
if (body !== undefined) headers["content-type"] = "application/json";
|
|
25
|
-
if (idempotencyKey) headers["idempotency-key"] = idempotencyKey;
|
|
26
|
-
const retryable = method === "GET" || method === "DELETE" || !!idempotencyKey;
|
|
27
|
-
for (let attempt = 0; ; attempt++) {
|
|
28
|
-
let res;
|
|
29
|
-
try {
|
|
30
|
-
res = await fetchImpl(url, { method, headers, body: body === undefined ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(timeout) });
|
|
31
|
-
} catch (e) {
|
|
32
|
-
if (retryable && attempt < maxRetries) { await sleep(500 * 2 ** attempt); continue; }
|
|
33
|
-
const timedOut = e?.name === "TimeoutError" || e?.name === "AbortError";
|
|
34
|
-
throw new ApiError(0, timedOut ? "timeout" : "network_error", timedOut ? `No response from ${base} within ${timeout / 1000}s` : `Couldn't reach ${base} (${e?.cause?.code || e?.message || "network error"})`);
|
|
35
|
-
}
|
|
36
|
-
const text = await res.text();
|
|
37
|
-
let json = null;
|
|
38
|
-
try { json = text ? JSON.parse(text) : null; } catch { /* not JSON */ }
|
|
39
|
-
if (res.ok) return json;
|
|
40
|
-
const name = (json && json.name) || (res.status === 401 ? "invalid_api_key" : res.status === 404 ? "not_found" : res.status === 429 ? "rate_limit_exceeded" : "application_error");
|
|
41
|
-
if ((res.status === 429 || res.status >= 500) && retryable && attempt < maxRetries) {
|
|
42
|
-
const after = Number(res.headers.get("retry-after"));
|
|
43
|
-
await sleep(Number.isFinite(after) && after > 0 ? Math.min(after, 30) * 1000 : 500 * 2 ** attempt);
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
throw new ApiError(res.status, name, (json && json.message) || text.slice(0, 300) || `Request failed with status ${res.status}`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return { request, baseUrl: base };
|
|
50
|
-
}
|
package/src/args.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
// Tiny argument parser: `--name value`, `--name=value`, `-x value`, repeatable flags, boolean switches, and `--`.
|
|
2
|
-
export function parseArgs(argv, { booleans = [], repeatable = [], aliases = {} } = {}) {
|
|
3
|
-
const flags = {}, positionals = [];
|
|
4
|
-
const set = (k, v) => {
|
|
5
|
-
k = aliases[k] || k;
|
|
6
|
-
if (repeatable.includes(k)) (flags[k] ||= []).push(v);
|
|
7
|
-
else flags[k] = v;
|
|
8
|
-
};
|
|
9
|
-
for (let i = 0; i < argv.length; i++) {
|
|
10
|
-
const a = argv[i];
|
|
11
|
-
if (a === "--") { positionals.push(...argv.slice(i + 1)); break; }
|
|
12
|
-
if (a.startsWith("--") || (a.startsWith("-") && a.length === 2 && !/^-\d/.test(a))) {
|
|
13
|
-
let k = a.replace(/^--?/, ""), v;
|
|
14
|
-
const eq = k.indexOf("=");
|
|
15
|
-
if (eq >= 0) { v = k.slice(eq + 1); k = k.slice(0, eq); }
|
|
16
|
-
if (k.startsWith("no-") && booleans.includes(aliases[k.slice(3)] || k.slice(3))) { set(k.slice(3), false); continue; }
|
|
17
|
-
const name = aliases[k] || k;
|
|
18
|
-
if (booleans.includes(name)) { set(k, v === undefined ? true : !/^(false|0|no)$/i.test(v)); continue; }
|
|
19
|
-
if (v === undefined) {
|
|
20
|
-
const next = argv[i + 1];
|
|
21
|
-
if (next === undefined || (next.startsWith("--") && next.length > 2)) throw new UsageError(`--${name} needs a value`);
|
|
22
|
-
v = next; i++;
|
|
23
|
-
}
|
|
24
|
-
set(k, v);
|
|
25
|
-
} else positionals.push(a);
|
|
26
|
-
}
|
|
27
|
-
return { flags, positionals };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export class UsageError extends Error {}
|