@postshiba/cli 0.0.0-stage → 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 +21 -0
- package/README.md +149 -2
- package/bin/postshiba.js +11 -0
- package/package.json +41 -4
- package/skills/postshiba-cli/SKILL.md +146 -0
- package/src/cli.js +467 -0
- package/src/client.js +74 -0
- package/src/commands.js +300 -0
- package/src/config.js +91 -0
- package/src/doctor.js +124 -0
- package/src/domains.js +67 -0
- package/src/errors.js +33 -0
- package/src/help.js +91 -0
- package/src/login.js +107 -0
- package/src/menu.js +51 -0
- package/src/mode.js +16 -0
- package/src/pkg.js +9 -0
- package/src/prompts.js +118 -0
- package/src/render.js +177 -0
- package/src/send.js +283 -0
- package/src/skills.js +40 -0
package/src/send.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { basename, extname } from "node:path";
|
|
5
|
+
import { UsageError } from "./errors.js";
|
|
6
|
+
import { domainVerified } from "./render.js";
|
|
7
|
+
|
|
8
|
+
const MIME = {
|
|
9
|
+
".png": "image/png",
|
|
10
|
+
".jpg": "image/jpeg",
|
|
11
|
+
".jpeg": "image/jpeg",
|
|
12
|
+
".gif": "image/gif",
|
|
13
|
+
".webp": "image/webp",
|
|
14
|
+
".pdf": "application/pdf",
|
|
15
|
+
".txt": "text/plain",
|
|
16
|
+
".html": "text/html",
|
|
17
|
+
".htm": "text/html",
|
|
18
|
+
".json": "application/json",
|
|
19
|
+
".csv": "text/csv",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {AsyncIterable<string | Uint8Array>} stdin
|
|
24
|
+
*/
|
|
25
|
+
export async function readStream(stdin) {
|
|
26
|
+
const chunks = [];
|
|
27
|
+
for await (const chunk of stdin) {
|
|
28
|
+
chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8"));
|
|
29
|
+
}
|
|
30
|
+
return chunks.join("");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} raw
|
|
35
|
+
* @param {AsyncIterable<string | Uint8Array>} stdin
|
|
36
|
+
*/
|
|
37
|
+
export async function loadData(raw, stdin) {
|
|
38
|
+
let text;
|
|
39
|
+
if (raw === "-") text = await readStream(stdin);
|
|
40
|
+
else if (raw.startsWith("@")) text = await readFile(raw.slice(1), "utf8");
|
|
41
|
+
else text = raw;
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(text);
|
|
44
|
+
} catch {
|
|
45
|
+
throw new UsageError("Invalid JSON for --data");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {Record<string, any>} values
|
|
51
|
+
* @param {AsyncIterable<string | Uint8Array>} stdin
|
|
52
|
+
*/
|
|
53
|
+
export async function buildSendBody(values, stdin) {
|
|
54
|
+
const body = values.data !== undefined ? await loadData(values.data, stdin) : {};
|
|
55
|
+
if (values.from) body.from = values.from;
|
|
56
|
+
if (values.to) body.to = values.to;
|
|
57
|
+
if (values.cc) body.cc = values.cc;
|
|
58
|
+
if (values.bcc) body.bcc = values.bcc;
|
|
59
|
+
if (values["reply-to"]) body.reply_to = values["reply-to"];
|
|
60
|
+
if (values.subject) body.subject = values.subject;
|
|
61
|
+
if (values.text !== undefined) body.text = values.text;
|
|
62
|
+
if (values["text-file"]) body.text = await readFile(values["text-file"], "utf8");
|
|
63
|
+
if (values.html !== undefined) body.html = values.html;
|
|
64
|
+
if (values["html-file"]) body.html = await readFile(values["html-file"], "utf8");
|
|
65
|
+
|
|
66
|
+
const usingTemplate = Boolean(values.template || body.template);
|
|
67
|
+
if (values.template) {
|
|
68
|
+
body.template = {
|
|
69
|
+
id: values.template,
|
|
70
|
+
variables: { ...(body.template && body.template.variables), ...pairs(values.var) },
|
|
71
|
+
};
|
|
72
|
+
delete body.html;
|
|
73
|
+
delete body.text;
|
|
74
|
+
} else if (values.var) {
|
|
75
|
+
body.template = {
|
|
76
|
+
...(body.template ?? {}),
|
|
77
|
+
variables: { ...(body.template && body.template.variables), ...pairs(values.var) },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (values.header) body.headers = { ...body.headers, ...headers(values.header) };
|
|
82
|
+
if (values.arg) body.unique_args = { ...body.unique_args, ...pairs(values.arg) };
|
|
83
|
+
if (values.attach) {
|
|
84
|
+
body.attachments = [];
|
|
85
|
+
for (const file of values.attach) {
|
|
86
|
+
const buf = await readFile(file);
|
|
87
|
+
body.attachments.push({
|
|
88
|
+
filename: basename(file),
|
|
89
|
+
content_type: MIME[/** @type {keyof typeof MIME} */ (extname(file).toLowerCase())] || "application/octet-stream",
|
|
90
|
+
content: buf.toString("base64"),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (values.tenant) body.tenant = values.tenant;
|
|
95
|
+
|
|
96
|
+
if (!usingTemplate) {
|
|
97
|
+
if (body.cc === undefined) body.cc = [];
|
|
98
|
+
if (body.bcc === undefined) body.bcc = [];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!body.from || !hasRecipients(body.to)) {
|
|
102
|
+
throw new UsageError("send requires --from and at least one --to", { helpResource: "send" });
|
|
103
|
+
}
|
|
104
|
+
return body;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {Record<string, any>} values
|
|
109
|
+
* @param {string} [clusterId]
|
|
110
|
+
*/
|
|
111
|
+
export function sendRoute(values, clusterId) {
|
|
112
|
+
const sandbox = Boolean(values.sandbox);
|
|
113
|
+
const idempotencyKey = values["idempotency-key"];
|
|
114
|
+
const id = values.cluster ?? clusterId;
|
|
115
|
+
// Cluster-path send is for sandbox or idempotent replay; --cluster alone stays on /emails.
|
|
116
|
+
if (sandbox || idempotencyKey) {
|
|
117
|
+
if (!id) {
|
|
118
|
+
throw new UsageError(
|
|
119
|
+
sandbox
|
|
120
|
+
? "Missing --cluster. --sandbox requires --cluster."
|
|
121
|
+
: "Missing --cluster. --idempotency-key requires --cluster.",
|
|
122
|
+
{ helpResource: "send" },
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
path: "/api/v1/teams/:teamId/clusters/:clusterId/sends",
|
|
127
|
+
team: true,
|
|
128
|
+
clusterId: id,
|
|
129
|
+
sandbox,
|
|
130
|
+
idempotencyKey,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
path: "/api/v1/emails",
|
|
135
|
+
team: false,
|
|
136
|
+
clusterId: id,
|
|
137
|
+
sandbox: false,
|
|
138
|
+
idempotencyKey: undefined,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {Record<string, any>} values
|
|
144
|
+
* @param {object} ctx
|
|
145
|
+
* @param {import("./prompts.js").Prompts} ctx.prompts
|
|
146
|
+
* @param {(method: string, path: string) => Promise<unknown>} ctx.api
|
|
147
|
+
* @param {{ clusterId?: string, teamId?: string }} creds
|
|
148
|
+
*/
|
|
149
|
+
export async function completeSendFlags(values, ctx, creds) {
|
|
150
|
+
const next = { ...values };
|
|
151
|
+
if (!next.from) {
|
|
152
|
+
const domains = asList(await ctx.api("GET", `/api/v1/teams/${creds.teamId}/sending_domains`)).filter(domainVerified);
|
|
153
|
+
const suggestion = domains[0] && typeof domains[0].name === "string" ? `hello@${domains[0].name}` : undefined;
|
|
154
|
+
next.from = await ctx.prompts.text({
|
|
155
|
+
message: "From",
|
|
156
|
+
placeholder: "hello@mail.example.com",
|
|
157
|
+
defaultValue: suggestion,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (!next.to) {
|
|
161
|
+
const raw = await ctx.prompts.text({ message: "To (comma separated)", placeholder: "you@example.com" });
|
|
162
|
+
next.to = raw
|
|
163
|
+
.split(",")
|
|
164
|
+
.map((item) => item.trim())
|
|
165
|
+
.filter(Boolean);
|
|
166
|
+
}
|
|
167
|
+
if (!next.subject && !next.template && !next.data) {
|
|
168
|
+
next.subject = await ctx.prompts.text({ message: "Subject" });
|
|
169
|
+
}
|
|
170
|
+
if (!next.text && !next.html && !next.template && !next["html-file"] && !next["text-file"] && !next.data) {
|
|
171
|
+
const kind = await ctx.prompts.select({
|
|
172
|
+
message: "Body",
|
|
173
|
+
options: [
|
|
174
|
+
{ value: "text", label: "Plain text" },
|
|
175
|
+
{ value: "html", label: "HTML file" },
|
|
176
|
+
{ value: "template", label: "Published template" },
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
if (kind === "text") next.text = await ctx.prompts.text({ message: "Text" });
|
|
180
|
+
if (kind === "html") next["html-file"] = await ctx.prompts.text({ message: "HTML file path" });
|
|
181
|
+
if (kind === "template") {
|
|
182
|
+
const templates = asList(await ctx.api("GET", `/api/v1/teams/${creds.teamId}/templates`)).filter(
|
|
183
|
+
(item) => item.published,
|
|
184
|
+
);
|
|
185
|
+
if (templates.length === 0) throw new UsageError("No published templates.", { helpResource: "send" });
|
|
186
|
+
next.template = await ctx.prompts.select({
|
|
187
|
+
message: "Template",
|
|
188
|
+
options: templates.map((item) => ({
|
|
189
|
+
value: String(item.alias ?? item.id),
|
|
190
|
+
label: String(item.name ?? item.alias ?? item.id),
|
|
191
|
+
})),
|
|
192
|
+
});
|
|
193
|
+
/** @type {string[]} */
|
|
194
|
+
const vars = [];
|
|
195
|
+
while (true) {
|
|
196
|
+
const pair = await ctx.prompts.text({
|
|
197
|
+
message: "Variable KEY=VALUE (blank to finish)",
|
|
198
|
+
placeholder: "name=Ada",
|
|
199
|
+
});
|
|
200
|
+
if (!String(pair).trim()) break;
|
|
201
|
+
vars.push(String(pair).trim());
|
|
202
|
+
}
|
|
203
|
+
if (vars.length) next.var = vars;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (!next.cluster) {
|
|
207
|
+
if (creds.clusterId) next.cluster = creds.clusterId;
|
|
208
|
+
else if (creds.teamId) {
|
|
209
|
+
const clusters = asList(await ctx.api("GET", `/api/v1/teams/${creds.teamId}/clusters`));
|
|
210
|
+
if (clusters.length === 1) next.cluster = String(clusters[0].id);
|
|
211
|
+
else if (clusters.length > 1) {
|
|
212
|
+
next.cluster = await ctx.prompts.select({
|
|
213
|
+
message: "Cluster",
|
|
214
|
+
options: clusters.map((item) => ({
|
|
215
|
+
value: String(item.id),
|
|
216
|
+
label: String(item.name ?? item.id),
|
|
217
|
+
})),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return next;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {Record<string, unknown>} body
|
|
227
|
+
* @param {{ sandbox?: boolean, clusterId?: string }} route
|
|
228
|
+
*/
|
|
229
|
+
export function sendPreview(body, route) {
|
|
230
|
+
const to = Array.isArray(body.to) ? body.to.join(", ") : String(body.to ?? "");
|
|
231
|
+
const lines = [
|
|
232
|
+
`From ${body.from ?? ""}`,
|
|
233
|
+
`To ${to}`,
|
|
234
|
+
`Subject ${body.subject ?? (body.template ? `(template ${/** @type {{ id?: string }} */ (body.template).id})` : "")}`,
|
|
235
|
+
];
|
|
236
|
+
if (route.clusterId) lines.push(`Cluster ${route.clusterId}`);
|
|
237
|
+
if (route.sandbox) lines.push("Sandbox yes");
|
|
238
|
+
return lines.join("\n");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param {unknown} value
|
|
243
|
+
* @returns {Record<string, any>[]}
|
|
244
|
+
*/
|
|
245
|
+
function asList(value) {
|
|
246
|
+
return Array.isArray(value) ? value.filter((item) => item && typeof item === "object") : [];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @param {unknown} to
|
|
251
|
+
*/
|
|
252
|
+
function hasRecipients(to) {
|
|
253
|
+
if (Array.isArray(to)) return to.length > 0;
|
|
254
|
+
return Boolean(to);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {string[] | undefined} items
|
|
259
|
+
*/
|
|
260
|
+
function pairs(items) {
|
|
261
|
+
/** @type {Record<string, string>} */
|
|
262
|
+
const out = {};
|
|
263
|
+
for (const item of items ?? []) {
|
|
264
|
+
const at = item.indexOf("=");
|
|
265
|
+
if (at === -1) throw new UsageError(`Expected KEY=VALUE, got ${item}`);
|
|
266
|
+
out[item.slice(0, at)] = item.slice(at + 1);
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* @param {string[] | undefined} items
|
|
273
|
+
*/
|
|
274
|
+
function headers(items) {
|
|
275
|
+
/** @type {Record<string, string>} */
|
|
276
|
+
const out = {};
|
|
277
|
+
for (const item of items ?? []) {
|
|
278
|
+
const at = item.indexOf(":");
|
|
279
|
+
if (at === -1) throw new UsageError(`Expected "Name: value", got ${item}`, { helpResource: "send" });
|
|
280
|
+
out[item.slice(0, at).trim()] = item.slice(at + 1).trim();
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
}
|
package/src/skills.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { UsageError } from "./errors.js";
|
|
6
|
+
import { SKILL_FILE } from "./pkg.js";
|
|
7
|
+
|
|
8
|
+
const TARGETS = {
|
|
9
|
+
cursor: ".cursor/skills/postshiba-cli/SKILL.md",
|
|
10
|
+
claude: ".claude/skills/postshiba-cli/SKILL.md",
|
|
11
|
+
agents: ".agents/skills/postshiba-cli/SKILL.md",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {Record<string, any>} values
|
|
16
|
+
* @param {object} ctx
|
|
17
|
+
* @param {NodeJS.ProcessEnv} ctx.env
|
|
18
|
+
* @param {string} [ctx.cwd]
|
|
19
|
+
* @param {string} [ctx.home]
|
|
20
|
+
* @param {{ write: (chunk: string) => unknown }} ctx.stdout
|
|
21
|
+
*/
|
|
22
|
+
export async function installSkill(values, ctx) {
|
|
23
|
+
const targetName = values.target ?? "cursor";
|
|
24
|
+
const rel = TARGETS[/** @type {keyof typeof TARGETS} */ (targetName)];
|
|
25
|
+
if (!rel) {
|
|
26
|
+
throw new UsageError("Unknown --target. Use cursor, claude, or agents.", { helpResource: "skills" });
|
|
27
|
+
}
|
|
28
|
+
const root = values.global
|
|
29
|
+
? (ctx.home ?? ctx.env.HOME ?? ctx.env.USERPROFILE)
|
|
30
|
+
: (ctx.cwd ?? process.cwd());
|
|
31
|
+
if (!root) throw new UsageError("Missing home directory for --global.");
|
|
32
|
+
const dest = join(root, rel);
|
|
33
|
+
await mkdir(dirname(dest), { recursive: true });
|
|
34
|
+
const body = await readFile(SKILL_FILE);
|
|
35
|
+
await writeFile(dest, body);
|
|
36
|
+
ctx.stdout.write(`${dest}\n`);
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { TARGETS };
|