pinga-cli 0.0.0 → 0.2.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.
@@ -0,0 +1,115 @@
1
+ import { CliError } from "./errors.js";
2
+ export const ENVELOPE_VERSION = "pinga.template.v1";
3
+ export const TEMPLATE_FILE = "template.json";
4
+ /**
5
+ * Prepara o export para ir para o git. Sai só o `exported_at`: muda a cada `pull`, e um diff que
6
+ * sempre aparece esconde o que mudou de verdade. O resto é o envelope `pinga.template.v1` como a
7
+ * API devolve, e o `import` aceita o arquivo de volta.
8
+ */
9
+ export function toFile(envelope) {
10
+ const { exported_at: _exportedAt, ...rest } = envelope;
11
+ return rest;
12
+ }
13
+ export function serialize(file) {
14
+ return `${JSON.stringify(file, null, 2)}\n`;
15
+ }
16
+ /** Lê e confere o mínimo do envelope; o resto quem valida é a API. */
17
+ export function parseTemplateFile(text, source) {
18
+ let parsed;
19
+ try {
20
+ parsed = JSON.parse(text);
21
+ }
22
+ catch {
23
+ throw new CliError(`${source} não é um JSON válido.`);
24
+ }
25
+ if (!isRecord(parsed))
26
+ throw new CliError(`${source} não é um envelope de template.`);
27
+ if (parsed["version"] !== ENVELOPE_VERSION) {
28
+ throw new CliError(`${source}: "version" precisa ser "${ENVELOPE_VERSION}".`);
29
+ }
30
+ const template = parsed["template"];
31
+ const versionData = parsed["version_data"];
32
+ if (!isRecord(template) || typeof template["slug"] !== "string" || template["slug"].length === 0) {
33
+ throw new CliError(`${source}: falta "template.slug".`);
34
+ }
35
+ if (!isRecord(versionData))
36
+ throw new CliError(`${source}: falta "version_data".`);
37
+ return parsed;
38
+ }
39
+ /** Template visual é editado pelo `document`; o cru, por `subject`/`mjml`/`text_body`. */
40
+ export function kindOf(file) {
41
+ return file.version_data.document ? "visual" : "raw";
42
+ }
43
+ const METADATA_FIELDS = ["name", "description", "from_address"];
44
+ /**
45
+ * Compara o arquivo com o export da versão ativa. Só entram os campos que o cliente edita: num
46
+ * template visual, `subject`, `mjml` e `text_body` são derivados do `document` e ficam de fora.
47
+ * `version_data.version`, `source` e `blocks` são informativos e nunca contam.
48
+ */
49
+ export function diff(local, remote) {
50
+ const metadata = METADATA_FIELDS.filter((field) => !sameValue(local.template[field], remote.template[field]));
51
+ const contentFields = kindOf(remote) === "visual"
52
+ ? ["document", "variables"]
53
+ : ["subject", "mjml", "text_body", "variables"];
54
+ const content = contentFields.filter((field) => !sameValue(contentDefault(field, local.version_data[field]), contentDefault(field, remote.version_data[field])));
55
+ return { metadata: [...metadata], content: [...content] };
56
+ }
57
+ /**
58
+ * Aplica na comparação os mesmos defaults que o envio usa (`updateParams`): `variables` ausente
59
+ * vira `[]` e `text_body` vazio vira `null`. Sem isso, um arquivo sem `variables` nunca converge
60
+ * com o remoto (que guarda `[]`) e cada push cria uma versão nova.
61
+ */
62
+ function contentDefault(field, value) {
63
+ if (field === "variables")
64
+ return value ?? [];
65
+ if (field === "text_body")
66
+ return value === "" ? null : value;
67
+ return value;
68
+ }
69
+ /** Corpo do `PUT /v1/templates/{id}` com só o que mudou. */
70
+ export function updateParams(local, remote, changes) {
71
+ const params = {};
72
+ for (const field of changes.metadata)
73
+ params[field] = local.template[field] ?? null;
74
+ if (changes.content.length > 0) {
75
+ params.variables = local.version_data.variables ?? [];
76
+ if (kindOf(remote) === "visual") {
77
+ params.document = local.version_data.document ?? null;
78
+ }
79
+ else {
80
+ params.subject = local.version_data.subject;
81
+ params.body = local.version_data.mjml;
82
+ params.text_body = local.version_data.text_body || null;
83
+ }
84
+ }
85
+ return params;
86
+ }
87
+ export function importParams(local) {
88
+ return local;
89
+ }
90
+ /** Igualdade estrutural: ignora a ordem das chaves e trata `null` e ausente como iguais. */
91
+ export function sameValue(a, b) {
92
+ return canonical(a) === canonical(b);
93
+ }
94
+ function canonical(value) {
95
+ return JSON.stringify(normalize(value)) ?? "null";
96
+ }
97
+ function normalize(value) {
98
+ if (value === undefined || value === null)
99
+ return null;
100
+ if (Array.isArray(value))
101
+ return value.map(normalize);
102
+ if (isRecord(value)) {
103
+ const out = {};
104
+ for (const key of Object.keys(value).sort()) {
105
+ const item = normalize(value[key]);
106
+ if (item !== null)
107
+ out[key] = item;
108
+ }
109
+ return out;
110
+ }
111
+ return value;
112
+ }
113
+ function isRecord(value) {
114
+ return typeof value === "object" && value !== null && !Array.isArray(value);
115
+ }
@@ -0,0 +1,2 @@
1
+ // Mantido em sincronia com package.json por test/version.test.ts.
2
+ export const VERSION = "0.2.1";
package/package.json CHANGED
@@ -1 +1,51 @@
1
- {"name":"pinga-cli","version":"0.0.0","description":"CLI oficial do Pinga — API de email transacional (pinga.api.br)","homepage":"https://pinga.api.br","repository":{"type":"git","url":"git+https://github.com/pinga-api/pinga-cli.git"},"license":"MIT","author":"Pinga <dev@pinga.api.br>","files":["README.md"]}
1
+ {
2
+ "name": "pinga-cli",
3
+ "version": "0.2.1",
4
+ "description": "CLI oficial do Pinga — API de email transacional (pinga.api.br)",
5
+ "license": "MIT",
6
+ "author": "Pinga <dev@pinga.api.br>",
7
+ "homepage": "https://docs.pinga.api.br/cli",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/pinga-api/pinga-cli.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/pinga-api/pinga-cli/issues"
14
+ },
15
+ "keywords": [
16
+ "pinga",
17
+ "email",
18
+ "transactional-email",
19
+ "cli"
20
+ ],
21
+ "type": "module",
22
+ "bin": {
23
+ "pinga": "dist/cli.js"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md",
28
+ "LICENSE"
29
+ ],
30
+ "engines": {
31
+ "node": ">=20"
32
+ },
33
+ "scripts": {
34
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && chmod +x dist/cli.js",
35
+ "typecheck": "tsc -p tsconfig.json --noEmit",
36
+ "test": "vitest run",
37
+ "test:watch": "vitest",
38
+ "smoke": "vitest run --config vitest.smoke.config.ts",
39
+ "dev": "bun src/cli.ts",
40
+ "prepublishOnly": "bun run build",
41
+ "build:bin": "bun build src/cli.ts --compile --minify --outfile bin/pinga"
42
+ },
43
+ "dependencies": {
44
+ "@pinga/sdk": "^0.1.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^22.10.0",
48
+ "typescript": "^5.7.0",
49
+ "vitest": "^3.0.0"
50
+ }
51
+ }