Setting the file. One moment. Execution State Prepare · Wix Replatform · wix/skills · Skills Docsscripts/execution-state-prepare.js
JavaScript·128 lines·4 KB
require
(
'../lib/progress-log.js'
);
8
9let progress;
10
11function usage() {
12 console.error(`Usage:
13 node scripts/execution-state-prepare.js <projectDir> [--manifest <path>] [--cms-mirror-file <taskId:path>] [--progress-log <path>]
14
15CMS mirror files may be NDJSON or JSON arrays of crosswalk rows. Use one --cms-mirror-file
16per import task that has cmsMirror.mode download/download-and-upload.
17`);
18}
19
20function parseArgs(argv) {
21 const args = { cmsMirrorFiles: [] };
22 for (let i = 0; i < argv.length; i += 1) {
23 const token = argv[i];
24 if (token === '--manifest') {
25 args.manifest = argv[i + 1];
26 i += 1;
27 } else if (token === '--cms-mirror-file') {
28 args.cmsMirrorFiles.push(argv[i + 1]);
29 i += 1;
30 } else if (!args.projectDir) {
31 args.projectDir = token;
32 } else {
33 throw new Error(`unknown argument: ${token}`);
34 }
35 }
36 return args;
37}
38
39async function readRows(filePath) {
40 const raw = await fs.readFile(filePath, 'utf8');
41 const trimmed = raw.trim();
42 if (!trimmed) {
43 return [];
44 }
45 if (trimmed.startsWith('[')) {
46 const parsed = JSON.parse(trimmed);
47 if (!Array.isArray(parsed)) {
48 throw new Error(`${filePath} must contain a JSON array when using JSON format`);
49 }
50 return parsed;
51 }
52 return trimmed.split(/\r?\n/).filter(Boolean).map((line, index) => {
53 try {
54 return JSON.parse(line);
55 } catch (error) {
56 throw new Error(`${filePath}:${index + 1} invalid NDJSON: ${error.message}`);
57 }
58 });
59}
60
61async function readMirrorRows(projectDir, specs) {
62 const rowsByTaskId = {};
63 for (const spec of specs) {
64 const separator = spec.indexOf(':');
65 if (separator <= 0) {
66 throw new Error(`--cms-mirror-file must be <taskId:path>, got: ${spec}`);
67 }
68 const taskId = spec.slice(0, separator);
69 const filePath = path.resolve(projectDir, spec.slice(separator + 1));
70 rowsByTaskId[taskId] = await readRows(filePath);
71 }
72 return rowsByTaskId;
73}
74
75async function main() {
76 const parsed = parseProgressArgs(process.argv.slice(2));
77 progress = createProgressLogger({
78 script: 'skills/wix-replatform/scripts/execution-state-prepare.js',
79 ...parsed.progress,
80 });
81 progress.start('Execution state preparation started', { phase: 'execution', step: 'prepare-state' });
82
83 const args = parseArgs(parsed.args);
84 if (!args.projectDir) {
85 usage();
86 progress.error('Missing project directory', { phase: 'execution', step: 'prepare-state' });
87 process.exit(1);
88 }
89
90 const projectDir = path.resolve(args.projectDir);
91 const manifestPath = args.manifest
92 ? path.resolve(projectDir, args.manifest)
93 : path.join(projectDir, 'execution', 'execution-manifest.json');
94 const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
95 const cmsMirrorRowsByTaskId = await readMirrorRows(projectDir, args.cmsMirrorFiles);
96
97 const result = await prepareExecutionState(projectDir, manifest, { cmsMirrorRowsByTaskId });
98 console.log(JSON.stringify(result, null, 2));
99 if (result.ok) {
100 progress.complete('Execution state preparation completed', {
101 phase: 'execution',
102 step: 'prepare-state',
103 artifact: projectDir,
104 count: result.actions.length,
105 unit: 'actions',
106 });
107 } else {
108 progress.error('Execution state preparation blocked', {
109 phase: 'execution',
110 step: 'prepare-state',
111 artifact: projectDir,
112 count: result.errors.length,
113 unit: 'errors',
114 });
115 }
116 process.exit(result.ok ? 0 : 1);
117}
118
119main().catch((error) => {
120 if (progress) {
121 progress.error(error && error.message ? error.message : 'Execution state preparation failed', {
122 phase: 'execution',
123 step: 'prepare-state',
124 });
125 }
126 console.error(error && error.message ? error.message : error);
127 process.exit(1);
128});