vecsnap 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/README.md +14 -2
- package/dist/main.js +11 -0
- package/dist/run.js +92 -0
- package/package.json +26 -4
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# vecsnap — turn an image into a clean SVG from the command line
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```sh
|
|
4
|
+
export VECSNAP_API_KEY=vs_test_… # vecsnap.com/app/developers — a test key is free
|
|
5
|
+
npx vecsnap logo.png # → logo.svg
|
|
6
|
+
npx vecsnap logo.png -o mark.svg --max-colors 4 --format design
|
|
7
|
+
npx vecsnap https://example.com/logo.png --json
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Exit codes: 0 done · 1 the API refused (its code printed) · 2 a wrong command · 3 the image ended without a result.
|
|
11
|
+
Docs: https://vecsnap.com/docs/sdks/cli
|
|
12
|
+
|
|
13
|
+
## Changes in 0.2.0
|
|
14
|
+
|
|
15
|
+
Built on `@vecsnap/sdk` 0.2.0, which speaks the API as it is today. The commands are unchanged.
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** The `vecsnap` command: `run.ts` with the real SDK, the real files and the terminal. */
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { VecSnap } from "@vecsnap/sdk";
|
|
5
|
+
import { run } from "./run.js";
|
|
6
|
+
process.exitCode = await run(process.argv.slice(2), () => new VecSnap(), {
|
|
7
|
+
readFile: async (path) => new Uint8Array(await readFile(path)),
|
|
8
|
+
writeFile: (path, bytes) => writeFile(path, bytes),
|
|
9
|
+
out: (line) => console.log(line),
|
|
10
|
+
err: (line) => console.error(line),
|
|
11
|
+
});
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export const USAGE = `Usage: vecsnap <image or https URL> [-o <file.svg>] [--max-colors <2-24>] [--format web|design] [--json]
|
|
2
|
+
|
|
3
|
+
Turns an image into a clean SVG. Set VECSNAP_API_KEY first (vecsnap.com/app/developers): a vs_test_ key is free.`;
|
|
4
|
+
/** The formats `--format` may name: what every result offers (`results.md`). A typo is refused BEFORE a credit is spent. */
|
|
5
|
+
export const FORMATS = ["web", "design"];
|
|
6
|
+
/** The command line, or the sentence that says what is wrong with it. */
|
|
7
|
+
export function parse(args) {
|
|
8
|
+
const parsed = { input: "", output: null, maxColors: null, format: "web", json: false };
|
|
9
|
+
for (let i = 0; i < args.length; i++) {
|
|
10
|
+
const arg = args[i];
|
|
11
|
+
// An option's value: the next word, never another option (`-o --json` is a mistake, not a file named "--json").
|
|
12
|
+
const value = () => {
|
|
13
|
+
const next = args[i + 1];
|
|
14
|
+
if (next === undefined || next.startsWith("-"))
|
|
15
|
+
return null;
|
|
16
|
+
i += 1;
|
|
17
|
+
return next;
|
|
18
|
+
};
|
|
19
|
+
if (arg === "-o" || arg === "--output" || arg === "--max-colors" || arg === "--format") {
|
|
20
|
+
const given = value();
|
|
21
|
+
if (given === null)
|
|
22
|
+
return { error: `${arg} needs a value` };
|
|
23
|
+
if (arg === "--max-colors")
|
|
24
|
+
parsed.maxColors = Number(given);
|
|
25
|
+
else if (arg === "--format")
|
|
26
|
+
parsed.format = given;
|
|
27
|
+
else
|
|
28
|
+
parsed.output = given;
|
|
29
|
+
}
|
|
30
|
+
else if (arg === "--json")
|
|
31
|
+
parsed.json = true;
|
|
32
|
+
else if (arg === "-h" || arg === "--help")
|
|
33
|
+
return { error: "" };
|
|
34
|
+
else if (arg.startsWith("-"))
|
|
35
|
+
return { error: `unknown option ${arg}` };
|
|
36
|
+
else if (parsed.input)
|
|
37
|
+
return { error: "one image at a time" };
|
|
38
|
+
else
|
|
39
|
+
parsed.input = arg;
|
|
40
|
+
}
|
|
41
|
+
if (!parsed.input)
|
|
42
|
+
return { error: "which image?" };
|
|
43
|
+
if (!FORMATS.includes(parsed.format))
|
|
44
|
+
return { error: `--format is ${FORMATS.join(" or ")}` };
|
|
45
|
+
if (parsed.maxColors !== null && !(Number.isInteger(parsed.maxColors) && parsed.maxColors >= 2 && parsed.maxColors <= 24)) {
|
|
46
|
+
return { error: "--max-colors is a whole number from 2 to 24" };
|
|
47
|
+
}
|
|
48
|
+
if (!parsed.output && parsed.json)
|
|
49
|
+
return parsed;
|
|
50
|
+
if (!parsed.output)
|
|
51
|
+
parsed.output = svgNameFor(parsed.input);
|
|
52
|
+
return parsed;
|
|
53
|
+
}
|
|
54
|
+
/** `logo.png` → `logo.svg`; a URL's last segment the same way. */
|
|
55
|
+
export function svgNameFor(input) {
|
|
56
|
+
const name = /^https?:\/\//.test(input) ? (new URL(input).pathname.split("/").pop() || "image") : input;
|
|
57
|
+
return name.replace(/\.[A-Za-z0-9]+$/, "") + ".svg";
|
|
58
|
+
}
|
|
59
|
+
export async function run(args, sdk, io) {
|
|
60
|
+
const parsed = parse(args);
|
|
61
|
+
if ("error" in parsed) {
|
|
62
|
+
if (parsed.error)
|
|
63
|
+
io.err(`vecsnap: ${parsed.error}`);
|
|
64
|
+
io.err(USAGE);
|
|
65
|
+
return parsed.error ? 2 : 0;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const client = sdk();
|
|
69
|
+
const remote = /^https?:\/\//.test(parsed.input);
|
|
70
|
+
const result = await client.vectorize({
|
|
71
|
+
...(remote ? { url: parsed.input } : { file: await io.readFile(parsed.input), filename: parsed.input.split("/").pop() }),
|
|
72
|
+
...(parsed.maxColors !== null ? { options: { maxColors: parsed.maxColors } } : {}),
|
|
73
|
+
});
|
|
74
|
+
if (parsed.json)
|
|
75
|
+
io.out(JSON.stringify(result, null, 2));
|
|
76
|
+
if (result.status !== "completed") {
|
|
77
|
+
io.err(`vecsnap: ${parsed.input} ended without a result: ${result.failure?.message ?? result.status}`);
|
|
78
|
+
return 3;
|
|
79
|
+
}
|
|
80
|
+
if (parsed.output) {
|
|
81
|
+
await io.writeFile(parsed.output, await client.download(result, parsed.format));
|
|
82
|
+
if (!parsed.json)
|
|
83
|
+
io.out(`${parsed.output} ${result.result?.anchorPoints ?? "?"} anchor points (conversion ${result.id})`);
|
|
84
|
+
}
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
const coded = error;
|
|
89
|
+
io.err(`vecsnap: ${coded.code ? `${coded.code}: ` : ""}${coded.message ?? String(error)}${coded.requestId ? ` (request ${coded.requestId})` : ""}`);
|
|
90
|
+
return coded.code === "missing_api_key" ? 2 : 1;
|
|
91
|
+
}
|
|
92
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vecsnap",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Turn an image into a clean SVG from the command line: npx vecsnap logo.png",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"vecsnap": "./dist/main.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"test": "node --test test/*.test.ts"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@vecsnap/sdk": "^0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"typescript": "5.9.3"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://vecsnap.com/docs/sdks/cli"
|
|
28
|
+
}
|