Repository navigation
Expand file tree
/
Copy pathcli.mjs
More file actions
executable file
·53 lines (45 loc) · 1.91 KB
/
Copy pathcli.mjs
File metadata and controls
executable file
·53 lines (45 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env node
// imagethis CLI — the fallback engine for the skill when the MCP isn't loaded.
// Generates an image and prints one JSON object with the saved path(s).
//
// Usage: node cli.mjs --prompt "..." [--model flux-1.1-pro] [--aspect 16:9]
// node cli.mjs --list
import { mkdirSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { generateImage, listModels } from './lib.mjs';
const EXT = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/webp': 'webp' };
function parseArgs(argv) {
const o = { prompt: '', model: undefined, aspectRatio: undefined, list: false };
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === '--prompt') o.prompt = argv[++i];
else if (a === '--model') o.model = argv[++i];
else if (a === '--aspect') o.aspectRatio = argv[++i];
else if (a === '--list') o.list = true;
}
return o;
}
function emit(obj, code) {
process.stdout.write(JSON.stringify(obj, null, 2) + '\n');
process.exit(code);
}
const REASON_EXIT = { empty_prompt: 2, unknown_model: 3, backend_not_configured: 4, api_error: 5 };
async function main() {
const o = parseArgs(process.argv.slice(2));
if (o.list) emit({ ok: true, models: listModels() }, 0);
const r = await generateImage({ prompt: o.prompt, model: o.model, aspectRatio: o.aspectRatio });
if (!r.ok) emit(r, REASON_EXIT[r.reason] ?? 1);
const dir = join(homedir(), '.imagethis', String(Date.now()));
mkdirSync(dir, { recursive: true });
const images = r.images.map((img, i) => {
const path = join(dir, `image-${i + 1}.${EXT[img.contentType] || 'png'}`);
writeFileSync(path, img.bytes);
return { path, url: img.url };
});
emit({ ok: true, model: r.model, backend: r.backend, images }, 0);
}
main().catch((err) => {
process.stdout.write(JSON.stringify({ ok: false, reason: 'fatal', detail: String(err) }) + '\n');
process.exit(1);
});