Setting the file. One moment. User Action · Wix Replatform · wix/skills · Skills Docsscripts/user-action.js
scripts/user-action.js
JavaScript·65 lines·3 KB
11
supersedeStaleUserAction
,
12} = require('../lib/user-actions.js');
13const { applyUserActionEffect } = require('../lib/user-action-effects.js');
14
15function parseArgs(argv) {
16 const result = { _: [] };
17 for (let index = 0; index < argv.length; index += 1) {
18 const value = argv[index];
19 if (!value.startsWith('--')) result._.push(value);
20 else {
21 const key = value.slice(2).replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
22 result[key] = key === 'acknowledge' ? true : argv[++index];
23 }
24 }
25 return result;
26}
27
28function applyEffect(projectDir, request, answer) {
29 return applyUserActionEffect(projectDir, request, answer);
30}
31
32async function main() {
33 const args = parseArgs(process.argv.slice(2));
34 const command = args._[0];
35 if (!command || !args.project) throw new Error('Usage: user-action.js create|list|show|answer|supersede|recover-omission-ack --project <path> ...');
36 const projectDir = path.resolve(args.project);
37 let result;
38 if (command === 'create') {
39 if (!args.requestFile) throw new Error('create requires --request-file <json>');
40 result = await createUserAction(projectDir, JSON.parse(await fs.readFile(path.resolve(args.requestFile), 'utf8')));
41 } else if (command === 'list') {
42 result = await listUserActions(projectDir, { status: args.status || null });
43 } else if (command === 'show') {
44 if (!args.request) throw new Error('show requires --request <id>');
45 result = await readUserAction(projectDir, args.request);
46 } else if (command === 'recover-omission-ack') {
47 if (!args.request) throw new Error('recover-omission-ack requires --request <stale-id>');
48 result = await require('../lib/read-back-approval.js').recoverStaleAcknowledgement(projectDir, args.request);
49 } else if (command === 'supersede') {
50 if (!args.request || !args.replacement) throw new Error('supersede requires --request <stale-id> --replacement <fresh-id>');
51 result = await supersedeStaleUserAction(projectDir, args.request, args.replacement);
52 } else if (command === 'answer') {
53 if (!args.request) throw new Error('answer requires --request <id>');
54 const answer = args.acknowledge ? { acknowledged: true, ...(args.note ? { note: args.note } : {}) } : args.choice !== undefined ? { choice: args.choice, ...(args.note ? { note: args.note } : {}) } : { text: args.text };
55 result = await answerUserAction(projectDir, args.request, answer, {
56 applyEffect: (request, normalized) => applyEffect(projectDir, request, normalized),
57 });
58 } else throw new Error(`unknown command: ${command}`);
59 process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
60}
61
62main().catch((error) => {
63 process.stderr.write(`${error && error.message ? error.message : error}\n`);
64 process.exit(2);
65});