waba-devtools 0.0.0-stage → 0.1.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/lib/report.js ADDED
@@ -0,0 +1,229 @@
1
+ /**
2
+ * Human-readable report lines. `--json` prints the report object instead.
3
+ *
4
+ * @param {object} report - Result of a command.
5
+ * @returns {string[]}
6
+ */
7
+ export function formatReport(report) {
8
+ switch (report.command) {
9
+ case 'retrieve': {
10
+ return [
11
+ `Retrieved ${report.files.length} template(s) from ${report.stage} (WABA ${report.wabaId}):`,
12
+ ...report.files.map((file) => ` ${file.result.padEnd(9)} ${file.path}`),
13
+ ];
14
+ }
15
+ case 'import': {
16
+ return [
17
+ `Imported ${report.files.length} row(s) from ${report.csv}:`,
18
+ ...cleanupLines(report.csvCleanup),
19
+ ...report.files.map((file) => ` ${file.result.padEnd(9)} ${file.path}`),
20
+ ...rowErrorLines(report.rowErrors),
21
+ ];
22
+ }
23
+ case 'deploy': {
24
+ return deployLines(report);
25
+ }
26
+ case 'list': {
27
+ return report.wabas.flatMap((waba) => [
28
+ `${waba.stage} — WABA ${waba.wabaId}:`,
29
+ ...(waba.error
30
+ ? [` FAILED ${waba.error}`]
31
+ : [
32
+ ...waba.templates.map(
33
+ (template) =>
34
+ ` ${template.name} (${template.language}) ${template.category} [${template.status}]`,
35
+ ),
36
+ ` = ${waba.templates.length} template(s)`,
37
+ ]),
38
+ ]);
39
+ }
40
+ case 'discover': {
41
+ const { tokenInfo } = report;
42
+ const tokenLines = tokenInfo
43
+ ? [
44
+ ` Token: ${tokenInfo.type ?? 'unknown type'}, ${
45
+ tokenInfo.expiresAt ? `expires ${tokenInfo.expiresAt}` : 'never expires'
46
+ }`,
47
+ ...(tokenInfo.type === 'USER'
48
+ ? [
49
+ ' Warning: a user token stops working within hours. Use a system-user ' +
50
+ 'token (README: Access tokens).',
51
+ ]
52
+ : []),
53
+ ]
54
+ : [];
55
+ return [
56
+ ...tokenLines,
57
+ ...report.wabas.map((waba) =>
58
+ waba.error
59
+ ? ` FAILED WABA ${waba.wabaId}: ${waba.error}`
60
+ : ` WABA ${waba.wabaId} ${waba.name ?? ''} ${waba.phoneNumbers
61
+ .map((phone) => `${phone.displayPhoneNumber} (${phone.verifiedName})`)
62
+ .join(', ')}`,
63
+ ),
64
+ ];
65
+ }
66
+ case 'backup': {
67
+ return report.sets.map((set) =>
68
+ set.error
69
+ ? ` FAILED ${set.stage} WABA ${set.wabaId}: ${set.error}`
70
+ : ` OK ${set.stage} WABA ${set.wabaId} → ${set.backup} (${set.templateCount} templates)`,
71
+ );
72
+ }
73
+ case 'compare': {
74
+ return compareLines(report);
75
+ }
76
+ case 'check': {
77
+ return report.entries.flatMap((entry) =>
78
+ entry.ok
79
+ ? [
80
+ ` OK ${entry.stage} WABA ${entry.wabaId} (${entry.wabaName ?? 'no name'})`,
81
+ ...(entry.warnings ?? []).map(
82
+ (warning) => ` WARN ${warning.message}`,
83
+ ),
84
+ ]
85
+ : ` FAILED ${entry.stage} WABA ${entry.wabaId}: ${entry.error}`,
86
+ );
87
+ }
88
+ default: {
89
+ return [JSON.stringify(report)];
90
+ }
91
+ }
92
+ }
93
+
94
+ /**
95
+ * @param {{wabaId: string, stage?: string, createdAt: string, phase?: string}} info
96
+ * @returns {string}
97
+ */
98
+ function setLine(info) {
99
+ if (info.phase === 'live') {
100
+ return `WABA ${info.wabaId} (${info.stage ?? '-'}) right now`;
101
+ }
102
+ const phase = info.phase ? ` ${info.phase}` : '';
103
+ return `WABA ${info.wabaId} (${info.stage ?? '-'}) backup ${info.createdAt}${phase}`;
104
+ }
105
+
106
+ /**
107
+ * @param {{name: string, language: string}} template
108
+ * @returns {string}
109
+ */
110
+ function itemLine(template) {
111
+ return ` ${template.name} (${template.language})`;
112
+ }
113
+
114
+ /**
115
+ * Compare report: the two sets, then added / removed / changed with field details.
116
+ *
117
+ * @param {object} report
118
+ * @returns {string[]}
119
+ */
120
+ function compareLines(report) {
121
+ const lines = [`Before: ${setLine(report.before)}`, `After: ${setLine(report.after)}`];
122
+ if (!report.sameWaba) {
123
+ lines.push('Note: before and after are different WABAs.');
124
+ }
125
+ lines.push(
126
+ `Added (${report.added.length}):`,
127
+ ...report.added.map((template) => itemLine(template)),
128
+ `Removed (${report.removed.length}):`,
129
+ ...report.removed.map((template) => itemLine(template)),
130
+ `Changed (${report.changed.length}):`,
131
+ );
132
+ for (const template of report.changed) {
133
+ lines.push(itemLine(template));
134
+ for (const change of template.changes) {
135
+ lines.push(
136
+ ` ${change.field}:`,
137
+ ` before: ${change.before ?? '—'}`,
138
+ ` after: ${change.after ?? '—'}`,
139
+ );
140
+ }
141
+ }
142
+ lines.push(`Unchanged: ${report.unchanged}`);
143
+ return lines;
144
+ }
145
+
146
+ /**
147
+ * One line about the CSV cleanup, when anything was cleaned up.
148
+ *
149
+ * @param {import('./csv.js').CsvCleanup} [cleanup]
150
+ * @returns {string[]}
151
+ */
152
+ function cleanupLines(cleanup) {
153
+ if (
154
+ !cleanup ||
155
+ cleanup.removedColumns + cleanup.removedRecords + cleanup.skippedRecords === 0
156
+ ) {
157
+ return [];
158
+ }
159
+ const rows = cleanup.removedRecords + cleanup.skippedRecords;
160
+ return [
161
+ ` CSV cleanup: removed ${cleanup.removedColumns} empty column(s) on the right and ${rows} empty row(s).`,
162
+ ];
163
+ }
164
+
165
+ /**
166
+ * @param {{row: number, message: string}[]} rowErrors
167
+ * @returns {string[]}
168
+ */
169
+ function rowErrorLines(rowErrors = []) {
170
+ if (rowErrors.length === 0) {
171
+ return [];
172
+ }
173
+ return [
174
+ `CSV rows with errors (${rowErrors.length}):`,
175
+ ...rowErrors.map((error) => ` row ${error.row}: ${error.message}`),
176
+ ];
177
+ }
178
+
179
+ /**
180
+ * @param {object} report - Deploy report.
181
+ * @returns {string[]}
182
+ */
183
+ function deployLines(report) {
184
+ const lines = [];
185
+ const from =
186
+ report.source.type === 'waba'
187
+ ? `${report.source.stage} (WABA ${report.source.wabaId})`
188
+ : (report.source.csv ?? report.source.dir);
189
+ lines.push(
190
+ `${report.dryRun ? 'Dry run — nothing is sent. ' : ''}Source: ${from}`,
191
+ ...cleanupLines(report.source.cleanup),
192
+ );
193
+ for (const target of report.targets) {
194
+ lines.push(`${target.stage} — WABA ${target.wabaId}:`);
195
+ if (target.error) {
196
+ lines.push(` FAILED ${target.error}`);
197
+ continue;
198
+ }
199
+ for (const [label, value] of [
200
+ ['backup before', target.backup],
201
+ ['backup after', target.backupAfter],
202
+ ['WARNING', target.backupAfterError],
203
+ ]) {
204
+ if (value) {
205
+ lines.push(` ${label}: ${value}`);
206
+ }
207
+ }
208
+ for (const action of target.actions) {
209
+ const status = action.status ? ` [${action.status}]` : '';
210
+ const after = action.statusAfter ? ` → ${action.statusAfter}` : '';
211
+ const result = action.result ? ` ${action.result}${after}` : '';
212
+ const detail = action.error ?? action.note;
213
+ lines.push(
214
+ ` ${action.action.padEnd(9)} ${action.name} (${action.language})${status}${result}` +
215
+ (detail ? ` — ${detail}` : ''),
216
+ );
217
+ }
218
+ const counts = {};
219
+ for (const action of target.actions) {
220
+ counts[action.action] = (counts[action.action] ?? 0) + 1;
221
+ }
222
+ const summary = Object.entries(counts)
223
+ .map(([action, count]) => `${count} ${action}`)
224
+ .join(', ');
225
+ lines.push(` = ${summary || 'nothing to do'}`);
226
+ }
227
+ lines.push(...rowErrorLines(report.rowErrors));
228
+ return lines;
229
+ }
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
- "name": "waba-devtools",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "waba-devtools",
3
+ "version": "0.1.0",
4
+ "description": "WABA DevTools (waba) — retrieve, import from CSV and deploy WhatsApp Business message templates across WABAs and pipeline stages",
5
+ "author": "Jörn Berkefeld <joern.berkefeld@gmail.com>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/JoernBerkefeld/waba-devtools.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/JoernBerkefeld/waba-devtools/issues"
13
+ },
14
+ "homepage": "https://github.com/JoernBerkefeld/waba-devtools#readme",
15
+ "keywords": [
16
+ "whatsapp",
17
+ "waba",
18
+ "whatsapp-business",
19
+ "message-templates",
20
+ "meta",
21
+ "graph-api",
22
+ "devops",
23
+ "cli"
24
+ ],
25
+ "type": "module",
26
+ "bin": {
27
+ "waba": "./bin/waba.js"
28
+ },
29
+ "main": "./lib/index.js",
30
+ "exports": {
31
+ ".": "./lib/index.js"
32
+ },
33
+ "files": ["bin", "lib", "README.md", "LICENSE"],
34
+ "scripts": {
35
+ "test": "node --test test/*.test.js",
36
+ "lint": "eslint .",
37
+ "lint:fix": "eslint --fix .",
38
+ "format": "prettier --write .",
39
+ "format:check": "prettier --check .",
40
+ "verify:own-dependencies-latest": "node scripts/verify-own-dependencies-latest.mjs",
41
+ "preversion": "npm run verify:own-dependencies-latest",
42
+ "prepare": "husky || true"
43
+ },
44
+ "engines": {
45
+ "node": "^22.22.2 || >=24.15.0"
46
+ },
47
+ "lint-staged": {
48
+ "*.{js,mjs,cjs}": ["eslint --fix"]
49
+ },
50
+ "dependencies": {
51
+ "@joern.berkefeld/csv-parser": "^5.0.0"
52
+ },
53
+ "devDependencies": {
54
+ "@eslint/js": "^10.0.1",
55
+ "eslint": "^10.15.0",
56
+ "eslint-config-prettier": "^10.1.8",
57
+ "eslint-plugin-jsdoc": "^65.5.0",
58
+ "eslint-plugin-prettier": "^5.5.7",
59
+ "eslint-plugin-unicorn": "^80.0.0",
60
+ "globals": "^17.14.0",
61
+ "husky": "^9.1.7",
62
+ "lint-staged": "^17.6.1",
63
+ "prettier": "^3.10.1"
64
+ }
65
+ }