waba-devtools 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 +859 -2
- package/bin/waba.js +4 -0
- package/lib/budget.js +275 -0
- package/lib/cli.js +503 -0
- package/lib/commands.js +936 -0
- package/lib/compare.js +400 -0
- package/lib/config.js +280 -0
- package/lib/csv.js +778 -0
- package/lib/errors.js +68 -0
- package/lib/files.js +112 -0
- package/lib/graph.js +264 -0
- package/lib/index.js +29 -0
- package/lib/normalize.js +278 -0
- package/lib/plan.js +110 -0
- package/lib/report.js +328 -0
- package/package.json +64 -5
package/lib/cli.js
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import {
|
|
5
|
+
check,
|
|
6
|
+
createBackup,
|
|
7
|
+
deploy,
|
|
8
|
+
discoverWabas,
|
|
9
|
+
importCsv,
|
|
10
|
+
listTemplates,
|
|
11
|
+
retrieve,
|
|
12
|
+
} from './commands.js';
|
|
13
|
+
import { compareBackups, compareWabas, compareWithLive } from './compare.js';
|
|
14
|
+
import { loadConfig, loadDotenvFile, parseJson } from './config.js';
|
|
15
|
+
import { UsageError } from './errors.js';
|
|
16
|
+
import { DEFAULT_LIMIT_PERCENT, META_CREATES_PER_HOUR } from './budget.js';
|
|
17
|
+
import { formatReport, nextRetry } from './report.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Exit codes, documented in the README.
|
|
21
|
+
*/
|
|
22
|
+
export const EXIT = { ok: 0, failed: 1, usage: 2 };
|
|
23
|
+
|
|
24
|
+
const HELP = `waba — WABA DevTools: deploy WhatsApp message templates between WABAs
|
|
25
|
+
|
|
26
|
+
Usage:
|
|
27
|
+
waba retrieve <source> [-m <name>…] [--json]
|
|
28
|
+
waba import --csv <file> [--json]
|
|
29
|
+
waba deploy <target> [-m <name>…] [--dry-run] [--json] (from the template files)
|
|
30
|
+
waba deploy <source> <target> [-m <name>…] [--dry-run] [--json] (WABA to WABA)
|
|
31
|
+
waba deploy --csv <file> <target> [--stop-on-error] [--dry-run] [--json]
|
|
32
|
+
waba deploy [<source>] <target> --prune [--yes]
|
|
33
|
+
waba deploy … --backup <folder> (snapshot targets first)
|
|
34
|
+
waba deploy … --limit-percent <n> [--wait] (share of Meta's limits)
|
|
35
|
+
waba list <source> | <target> [--json]
|
|
36
|
+
waba check [--stage <stage>] [--json]
|
|
37
|
+
waba discover --token-env <NAME> [--business <id>] [--json]
|
|
38
|
+
waba compare <backup set> <backup set> [--json] (older vs newer, by backup time)
|
|
39
|
+
waba compare <backup set> --live [--waba <id>] [--json] (backup vs the WABA right now)
|
|
40
|
+
waba compare --before-waba <id> --after-waba <id> [--json] (two WABAs right now)
|
|
41
|
+
waba backup --stage <stage> | --waba <id>… --backup <folder> [--json] (manual backup)
|
|
42
|
+
|
|
43
|
+
<source> --from <stage> and/or --from-waba <id>
|
|
44
|
+
<target> --to <stage> and/or --to-waba <id>…
|
|
45
|
+
|
|
46
|
+
Options:
|
|
47
|
+
-m, --template <name> Only this template (all its languages). Repeatable.
|
|
48
|
+
--from <stage> Source: the WABA of this stage.
|
|
49
|
+
--from-waba <id> Source: this WABA. Needed when the --from stage has several.
|
|
50
|
+
--to <stage> Target: every WABA of this stage.
|
|
51
|
+
--to-waba <id> Target: only this WABA. Repeatable. With --to: must be in that stage.
|
|
52
|
+
--dry-run Show the planned actions and live statuses; send nothing.
|
|
53
|
+
--prune Full-set deploys only: delete templates that exist only in the target.
|
|
54
|
+
Without --yes the deletes are listed but not run — creates and updates
|
|
55
|
+
ARE sent. Preview everything with --dry-run.
|
|
56
|
+
--yes Confirm the deletes of --prune.
|
|
57
|
+
--expect <file> Send only what this preview showed: the --json output of a previous
|
|
58
|
+
deploy --dry-run. A WABA whose plan changed since gets nothing.
|
|
59
|
+
--backup <folder> Before changing a WABA, save all its templates as a backup set
|
|
60
|
+
<folder>/<wabaId>_<time>_before/.
|
|
61
|
+
--backup-after With --backup: also save each changed WABA after the changes
|
|
62
|
+
(<wabaId>_<time>_after/).
|
|
63
|
+
--from-dir <folder> Source: template files in this folder instead of templatesDir
|
|
64
|
+
(e.g. a backup set: <backup>/<wabaId>_<time>).
|
|
65
|
+
--limit-percent <n> Share of Meta's limits to use, 1 to 100 (default 80): template creates
|
|
66
|
+
per WABA and hour (Meta allows 100) and Meta's API usage (writing
|
|
67
|
+
stops at this %). More creates are deferred: the report shows
|
|
68
|
+
the batch schedule and when to run the same command again (exit code 1).
|
|
69
|
+
--stop-on-error deploy --csv: when any CSV row has an error, send nothing at all
|
|
70
|
+
(default: rows with errors are left out, the others are sent).
|
|
71
|
+
--wait deploy: keep running and send each batch when its time comes, until
|
|
72
|
+
nothing is deferred. Ctrl+C is safe; rerun the command to continue.
|
|
73
|
+
--token-env <NAME> discover: the environment variable that holds the access token.
|
|
74
|
+
--business <id> discover: the business portfolio whose WABAs to list (needed when Meta
|
|
75
|
+
does not name the WABAs for the token).
|
|
76
|
+
--live compare: compare with the WABA's templates right now (nothing is saved).
|
|
77
|
+
--waba <id> backup: this WABA (repeatable); compare --live: another WABA than the
|
|
78
|
+
backup's own.
|
|
79
|
+
--before-waba <id> compare: this WABA (right now) is "before" …
|
|
80
|
+
--after-waba <id> … and this one "after". Nothing is saved.
|
|
81
|
+
--json Print the report as JSON.
|
|
82
|
+
-h, --help Show this help.
|
|
83
|
+
-v, --version Show the version.
|
|
84
|
+
|
|
85
|
+
Configuration: .wabadevrc.json in the current folder. Tokens: environment variables named
|
|
86
|
+
in the config (a .env file in the current folder is loaded too).`;
|
|
87
|
+
|
|
88
|
+
const OPTIONS = {
|
|
89
|
+
from: { type: 'string' },
|
|
90
|
+
to: { type: 'string' },
|
|
91
|
+
'from-waba': { type: 'string' },
|
|
92
|
+
'to-waba': { type: 'string', multiple: true },
|
|
93
|
+
stage: { type: 'string' },
|
|
94
|
+
waba: { type: 'string', multiple: true },
|
|
95
|
+
live: { type: 'boolean' },
|
|
96
|
+
'before-waba': { type: 'string' },
|
|
97
|
+
'after-waba': { type: 'string' },
|
|
98
|
+
csv: { type: 'string' },
|
|
99
|
+
template: { type: 'string', short: 'm', multiple: true },
|
|
100
|
+
'dry-run': { type: 'boolean' },
|
|
101
|
+
backup: { type: 'string' },
|
|
102
|
+
'backup-after': { type: 'boolean' },
|
|
103
|
+
expect: { type: 'string' },
|
|
104
|
+
'from-dir': { type: 'string' },
|
|
105
|
+
'limit-percent': { type: 'string' },
|
|
106
|
+
wait: { type: 'boolean' },
|
|
107
|
+
'stop-on-error': { type: 'boolean' },
|
|
108
|
+
prune: { type: 'boolean' },
|
|
109
|
+
yes: { type: 'boolean' },
|
|
110
|
+
'token-env': { type: 'string' },
|
|
111
|
+
business: { type: 'string' },
|
|
112
|
+
json: { type: 'boolean' },
|
|
113
|
+
help: { type: 'boolean', short: 'h' },
|
|
114
|
+
version: { type: 'boolean', short: 'v' },
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Options each command accepts (besides --json and --help).
|
|
119
|
+
*/
|
|
120
|
+
const ALLOWED = new Map([
|
|
121
|
+
['retrieve', ['from', 'from-waba', 'template']],
|
|
122
|
+
['import', ['csv']],
|
|
123
|
+
[
|
|
124
|
+
'deploy',
|
|
125
|
+
[
|
|
126
|
+
'from',
|
|
127
|
+
'from-waba',
|
|
128
|
+
'to',
|
|
129
|
+
'to-waba',
|
|
130
|
+
'csv',
|
|
131
|
+
'template',
|
|
132
|
+
'dry-run',
|
|
133
|
+
'prune',
|
|
134
|
+
'yes',
|
|
135
|
+
'backup',
|
|
136
|
+
'backup-after',
|
|
137
|
+
'expect',
|
|
138
|
+
'from-dir',
|
|
139
|
+
'limit-percent',
|
|
140
|
+
'wait',
|
|
141
|
+
'stop-on-error',
|
|
142
|
+
],
|
|
143
|
+
],
|
|
144
|
+
['check', ['stage']],
|
|
145
|
+
['list', ['from', 'from-waba', 'to', 'to-waba']],
|
|
146
|
+
['discover', ['token-env', 'business']],
|
|
147
|
+
['compare', ['live', 'waba', 'before-waba', 'after-waba']],
|
|
148
|
+
['backup', ['stage', 'waba', 'backup']],
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Runs the CLI. Returns the exit code instead of exiting, so it can be tested.
|
|
153
|
+
*
|
|
154
|
+
* @param {string[]} argv - Arguments after the executable and script.
|
|
155
|
+
* @param {object} [io]
|
|
156
|
+
* @param {string} [io.cwd]
|
|
157
|
+
* @param {NodeJS.ProcessEnv} [io.environment]
|
|
158
|
+
* @param {(text: string) => void} [io.out]
|
|
159
|
+
* @param {(text: string) => void} [io.err]
|
|
160
|
+
* @param {typeof fetch} [io.fetchImpl]
|
|
161
|
+
* @param {(ms: number) => Promise.<void>} [io.sleep] - Waits for `deploy --wait` (tests).
|
|
162
|
+
* @returns {Promise.<number>}
|
|
163
|
+
*/
|
|
164
|
+
export async function main(argv, io = {}) {
|
|
165
|
+
const {
|
|
166
|
+
cwd = process.cwd(),
|
|
167
|
+
environment = process.env,
|
|
168
|
+
out = (text) => process.stdout.write(`${text}\n`),
|
|
169
|
+
err: error = (text) => process.stderr.write(`${text}\n`),
|
|
170
|
+
fetchImpl,
|
|
171
|
+
sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
172
|
+
} = io;
|
|
173
|
+
let parsed;
|
|
174
|
+
try {
|
|
175
|
+
parsed = parseArgs({ args: argv, options: OPTIONS, allowPositionals: true, strict: true });
|
|
176
|
+
} catch (ex) {
|
|
177
|
+
error(`${ex.message}\n\n${HELP}`);
|
|
178
|
+
return EXIT.usage;
|
|
179
|
+
}
|
|
180
|
+
const { values, positionals } = parsed;
|
|
181
|
+
if (values.version) {
|
|
182
|
+
const package_ = JSON.parse(
|
|
183
|
+
readFileSync(new URL('../package.json', import.meta.url), 'utf8'),
|
|
184
|
+
);
|
|
185
|
+
out(package_.version);
|
|
186
|
+
return EXIT.ok;
|
|
187
|
+
}
|
|
188
|
+
const [command, ...extra] = positionals;
|
|
189
|
+
if (!command || values.help) {
|
|
190
|
+
out(HELP);
|
|
191
|
+
return values.help ? EXIT.ok : EXIT.usage;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
const allowed = ALLOWED.get(command);
|
|
195
|
+
if (!allowed || (command !== 'compare' && extra.length > 0)) {
|
|
196
|
+
throw new UsageError(`Unknown command "${positionals.join(' ')}".`);
|
|
197
|
+
}
|
|
198
|
+
const isTwoWabas = Boolean(values['before-waba'] || values['after-waba']);
|
|
199
|
+
if (
|
|
200
|
+
command === 'compare' &&
|
|
201
|
+
isTwoWabas &&
|
|
202
|
+
(extra.length > 0 || values.live || values.waba)
|
|
203
|
+
) {
|
|
204
|
+
throw new UsageError(
|
|
205
|
+
'compare --before-waba/--after-waba compares two WABAs live; give no backup set, --live or --waba.',
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (command === 'compare' && values.waba && (!values.live || values.waba.length > 1)) {
|
|
209
|
+
throw new UsageError('compare takes one --waba, together with --live.');
|
|
210
|
+
}
|
|
211
|
+
if (command === 'compare' && values.live && extra.length !== 1) {
|
|
212
|
+
throw new UsageError('compare --live needs one backup set folder.');
|
|
213
|
+
}
|
|
214
|
+
const unsupported = Object.keys(values).filter(
|
|
215
|
+
(key) => !['json', 'help'].includes(key) && !allowed.includes(key),
|
|
216
|
+
);
|
|
217
|
+
if (unsupported.length > 0) {
|
|
218
|
+
throw new UsageError(`"${command}" does not take --${unsupported.join(', --')}.`);
|
|
219
|
+
}
|
|
220
|
+
loadDotenvFile(cwd, environment);
|
|
221
|
+
if (!isTwoWabas && command === 'compare' && !values.live) {
|
|
222
|
+
if (extra.length !== 2) {
|
|
223
|
+
throw new UsageError('compare needs two backup set folders.');
|
|
224
|
+
}
|
|
225
|
+
const report = await compareBackups(
|
|
226
|
+
path.resolve(cwd, extra[0]),
|
|
227
|
+
path.resolve(cwd, extra[1]),
|
|
228
|
+
);
|
|
229
|
+
out(
|
|
230
|
+
values.json
|
|
231
|
+
? JSON.stringify(report, undefined, 2)
|
|
232
|
+
: formatReport(report).join('\n'),
|
|
233
|
+
);
|
|
234
|
+
return EXIT.ok;
|
|
235
|
+
}
|
|
236
|
+
if (command === 'discover') {
|
|
237
|
+
const report = await discover(
|
|
238
|
+
values['token-env'],
|
|
239
|
+
values.business,
|
|
240
|
+
environment,
|
|
241
|
+
fetchImpl,
|
|
242
|
+
);
|
|
243
|
+
out(
|
|
244
|
+
values.json
|
|
245
|
+
? JSON.stringify(report, undefined, 2)
|
|
246
|
+
: formatReport(report).join('\n'),
|
|
247
|
+
);
|
|
248
|
+
return report.ok ? EXIT.ok : EXIT.failed;
|
|
249
|
+
}
|
|
250
|
+
const config = loadConfig(cwd);
|
|
251
|
+
for (const warning of config.warnings) {
|
|
252
|
+
error(`Warning: ${warning}`);
|
|
253
|
+
}
|
|
254
|
+
const common = { cwd, config, environment, fetchImpl };
|
|
255
|
+
const names = values.template ?? [];
|
|
256
|
+
let report;
|
|
257
|
+
switch (command) {
|
|
258
|
+
case 'retrieve': {
|
|
259
|
+
requireOption(values.from ?? values['from-waba'], 'from or --from-waba');
|
|
260
|
+
report = await retrieve({
|
|
261
|
+
...common,
|
|
262
|
+
from: values.from,
|
|
263
|
+
fromWaba: values['from-waba'],
|
|
264
|
+
names,
|
|
265
|
+
});
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
case 'import': {
|
|
269
|
+
requireOption(values.csv, 'csv');
|
|
270
|
+
report = await importCsv({ ...common, csv: values.csv });
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
case 'deploy': {
|
|
274
|
+
requireOption(values.to ?? values['to-waba'], 'to or --to-waba');
|
|
275
|
+
if (values.wait && (values['dry-run'] || values.expect)) {
|
|
276
|
+
throw new UsageError('--wait does not work with --dry-run or --expect.');
|
|
277
|
+
}
|
|
278
|
+
const limitPercent =
|
|
279
|
+
values['limit-percent'] === undefined
|
|
280
|
+
? undefined
|
|
281
|
+
: parseCount(values['limit-percent'], 'limit-percent');
|
|
282
|
+
if (limitPercent > DEFAULT_LIMIT_PERCENT) {
|
|
283
|
+
error(
|
|
284
|
+
`Warning: --limit-percent ${limitPercent} leaves little room below Meta's limits ` +
|
|
285
|
+
`(${META_CREATES_PER_HOUR} template creates per WABA and hour, and its API ` +
|
|
286
|
+
'allowance). Templates created and deleted again within the hour are not ' +
|
|
287
|
+
'visible to waba, so Meta may still refuse creates.',
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
const deployOptions = {
|
|
291
|
+
...common,
|
|
292
|
+
to: values.to,
|
|
293
|
+
from: values.from,
|
|
294
|
+
toWabas: values['to-waba'] ?? [],
|
|
295
|
+
fromWaba: values['from-waba'],
|
|
296
|
+
csv: values.csv,
|
|
297
|
+
stopOnRowErrors: values['stop-on-error'] === true,
|
|
298
|
+
names,
|
|
299
|
+
dryRun: values['dry-run'] === true,
|
|
300
|
+
prune: values.prune === true,
|
|
301
|
+
yes: values.yes === true,
|
|
302
|
+
backupDir: values.backup && path.resolve(cwd, values.backup),
|
|
303
|
+
backupAfter: values['backup-after'] === true,
|
|
304
|
+
expectedActions: values.expect
|
|
305
|
+
? readExpectedActions(path.resolve(cwd, values.expect))
|
|
306
|
+
: undefined,
|
|
307
|
+
sourceDir: values['from-dir'] && path.resolve(cwd, values['from-dir']),
|
|
308
|
+
...(limitPercent !== undefined && { limitPercent }),
|
|
309
|
+
};
|
|
310
|
+
report = await deploy(deployOptions);
|
|
311
|
+
while (values.wait && isOnlyDeferred(report)) {
|
|
312
|
+
if (!values.json) {
|
|
313
|
+
out(formatReport(report).join('\n'));
|
|
314
|
+
}
|
|
315
|
+
await waitUntil(nextRetry(report), { sleep, say: values.json ? error : out });
|
|
316
|
+
report = await deploy(deployOptions);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case 'compare': {
|
|
321
|
+
if (isTwoWabas) {
|
|
322
|
+
report = await compareWabas({
|
|
323
|
+
...common,
|
|
324
|
+
beforeWaba: values['before-waba'],
|
|
325
|
+
afterWaba: values['after-waba'],
|
|
326
|
+
});
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
report = await compareWithLive({
|
|
330
|
+
...common,
|
|
331
|
+
set: path.resolve(cwd, extra[0]),
|
|
332
|
+
wabaId: values.waba?.[0],
|
|
333
|
+
});
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
case 'backup': {
|
|
337
|
+
requireOption(values.backup, 'backup');
|
|
338
|
+
report = await createBackup({
|
|
339
|
+
...common,
|
|
340
|
+
stage: values.stage,
|
|
341
|
+
wabaIds: values.waba ?? [],
|
|
342
|
+
backupDir: path.resolve(cwd, values.backup),
|
|
343
|
+
});
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
case 'list': {
|
|
347
|
+
report = await listTemplates({
|
|
348
|
+
...common,
|
|
349
|
+
from: values.from,
|
|
350
|
+
fromWaba: values['from-waba'],
|
|
351
|
+
to: values.to,
|
|
352
|
+
toWabas: values['to-waba'] ?? [],
|
|
353
|
+
});
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
default: {
|
|
357
|
+
report = await check({ ...common, stage: values.stage });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
out(values.json ? JSON.stringify(report, undefined, 2) : formatReport(report).join('\n'));
|
|
361
|
+
return report.ok ? EXIT.ok : EXIT.failed;
|
|
362
|
+
} catch (ex) {
|
|
363
|
+
if (values.json) {
|
|
364
|
+
out(
|
|
365
|
+
JSON.stringify(
|
|
366
|
+
{
|
|
367
|
+
command,
|
|
368
|
+
ok: false,
|
|
369
|
+
error: ex.message,
|
|
370
|
+
...(ex instanceof UsageError && ex.code && { code: ex.code }),
|
|
371
|
+
},
|
|
372
|
+
undefined,
|
|
373
|
+
2,
|
|
374
|
+
),
|
|
375
|
+
);
|
|
376
|
+
} else {
|
|
377
|
+
error(`Error: ${ex.message}`);
|
|
378
|
+
}
|
|
379
|
+
return ex instanceof UsageError ? EXIT.usage : EXIT.failed;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* `discover` needs no config: the token comes from the named environment variable.
|
|
385
|
+
*
|
|
386
|
+
* @param {string|undefined} variableName - `--token-env` value.
|
|
387
|
+
* @param {string|undefined} businessId - `--business` value.
|
|
388
|
+
* @param {NodeJS.ProcessEnv} environment
|
|
389
|
+
* @param {typeof fetch} [fetchImpl]
|
|
390
|
+
* @returns {Promise.<object>} Report.
|
|
391
|
+
*/
|
|
392
|
+
function discover(variableName, businessId, environment, fetchImpl) {
|
|
393
|
+
requireOption(variableName, 'token-env');
|
|
394
|
+
const token = environment[variableName];
|
|
395
|
+
if (!token) {
|
|
396
|
+
throw new UsageError(
|
|
397
|
+
`The environment variable ${variableName} is not set (or not in .env).`,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
return discoverWabas({ token, businessId, fetchImpl });
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Reads `--expect <file>`: the `--json` report of a `deploy --dry-run` (or just its `targets`).
|
|
405
|
+
*
|
|
406
|
+
* @param {string} file
|
|
407
|
+
* @returns {{wabaId: string, actions: {name: string, language: string, action: string}[]}[]}
|
|
408
|
+
*/
|
|
409
|
+
function readExpectedActions(file) {
|
|
410
|
+
let parsed;
|
|
411
|
+
try {
|
|
412
|
+
parsed = parseJson(readFileSync(file, 'utf8'));
|
|
413
|
+
} catch (ex) {
|
|
414
|
+
throw new UsageError(`--expect: cannot read ${file}: ${ex.message}`);
|
|
415
|
+
}
|
|
416
|
+
const targets = Array.isArray(parsed) ? parsed : parsed?.targets;
|
|
417
|
+
const isValid =
|
|
418
|
+
Array.isArray(targets) &&
|
|
419
|
+
targets.every(
|
|
420
|
+
(target) =>
|
|
421
|
+
typeof target?.wabaId === 'string' &&
|
|
422
|
+
Array.isArray(target.actions) &&
|
|
423
|
+
target.actions.every(
|
|
424
|
+
(action) =>
|
|
425
|
+
typeof action?.name === 'string' &&
|
|
426
|
+
typeof action.language === 'string' &&
|
|
427
|
+
typeof action.action === 'string',
|
|
428
|
+
),
|
|
429
|
+
);
|
|
430
|
+
if (!isValid) {
|
|
431
|
+
throw new UsageError(
|
|
432
|
+
`--expect: ${file} is not the --json output of "deploy --dry-run" (targets[].wabaId, targets[].actions[].name/language/action).`,
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
if (parsed?.dryRun === false) {
|
|
436
|
+
throw new UsageError(`--expect: ${file} is from a real deploy, not from --dry-run.`);
|
|
437
|
+
}
|
|
438
|
+
return targets;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* True when a deploy report has deferred templates and nothing else went wrong, so `--wait` may go on.
|
|
443
|
+
*
|
|
444
|
+
* @param {object} report - Deploy report.
|
|
445
|
+
* @returns {boolean}
|
|
446
|
+
*/
|
|
447
|
+
function isOnlyDeferred(report) {
|
|
448
|
+
return (
|
|
449
|
+
nextRetry(report) !== undefined &&
|
|
450
|
+
report.rowErrors.length === 0 &&
|
|
451
|
+
report.targets.every(
|
|
452
|
+
(target) =>
|
|
453
|
+
!target.error &&
|
|
454
|
+
target.actions.every(
|
|
455
|
+
(action) => action.result !== 'failed' && action.action !== 'blocked',
|
|
456
|
+
),
|
|
457
|
+
)
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Sleeps until `retryAfter` (plus one second), saying every 10 minutes how long is left.
|
|
463
|
+
*
|
|
464
|
+
* @param {string} retryAfter - ISO time.
|
|
465
|
+
* @param {{sleep: (ms: number) => Promise.<void>, say: (text: string) => void}} io
|
|
466
|
+
* @returns {Promise.<void>}
|
|
467
|
+
*/
|
|
468
|
+
async function waitUntil(retryAfter, { sleep, say }) {
|
|
469
|
+
const until = Date.parse(retryAfter) + 1000;
|
|
470
|
+
say(
|
|
471
|
+
`Waiting for the next batch at ${retryAfter} (Ctrl+C is safe; run the same command later to continue).`,
|
|
472
|
+
);
|
|
473
|
+
let left = until - Date.now();
|
|
474
|
+
while (left > 0) {
|
|
475
|
+
say(` ${Math.ceil(left / 60_000)} min left`);
|
|
476
|
+
const step = Math.min(left, 10 * 60_000);
|
|
477
|
+
await sleep(step);
|
|
478
|
+
left -= step;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* @param {string} value - Option value.
|
|
484
|
+
* @param {string} name - Option name for the message.
|
|
485
|
+
* @returns {number} Whole number of 1 or more.
|
|
486
|
+
*/
|
|
487
|
+
function parseCount(value, name) {
|
|
488
|
+
if (!/^\d+$/.test(value) || Number(value) < 1) {
|
|
489
|
+
throw new UsageError(`--${name} needs a whole number of 1 or more, got "${value}".`);
|
|
490
|
+
}
|
|
491
|
+
return Number(value);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* @param {string|undefined} value - Parsed option value.
|
|
496
|
+
* @param {string} name - Option name for the message.
|
|
497
|
+
* @returns {void}
|
|
498
|
+
*/
|
|
499
|
+
function requireOption(value, name) {
|
|
500
|
+
if (!value) {
|
|
501
|
+
throw new UsageError(`Missing --${name}.`);
|
|
502
|
+
}
|
|
503
|
+
}
|