Setting the file. One moment. Verify Stores · Rp Target Wix · wix/skills · Skills Docsscripts/verify-stores.js
scripts/verify-stores.js
JavaScript·107 lines·4 KB
{
9 createContractLedgerProposalFromStoresVerification,
10 writeProposal,
11} = require('../lib/contract-ledger');
12
13function usage() {
14 console.error(`Usage:
15 node scripts/verify-stores.js stores subscription-create --artifact <file> [--proposal-artifact <file>] [--marker <id>] [--no-cleanup]
16 node scripts/verify-stores.js stores product-count --artifact <file>
17 node scripts/verify-stores.js stores product-by-source-marker --marker-path <path> --marker-value <value> --artifact <file>
18 node scripts/verify-stores.js stores delete-probe --product-id <id> --artifact <file>
19
20Credentials:
21 WIX_AUTH_TOKEN and WIX_SITE_ID must be set in the environment.`);
22}
23
24function parseArgs(argv) {
25 const args = [...argv];
26 if (args[0] === 'verify') args.shift();
27 if (args[0] !== 'stores') {
28 usage();
29 process.exit(2);
30 }
31 args.shift();
32 const command = args.shift();
33 const options = {};
34 for (let i = 0; i < args.length; i++) {
35 const arg = args[i];
36 if (arg === '--no-cleanup') {
37 options.cleanup = false;
38 } else if (arg.startsWith('--')) {
39 options[arg.slice(2).replace(/-([a-z])/g, (_, c) => c.toUpperCase())] = args[++i];
40 } else {
41 throw new Error(`unexpected argument: ${arg}`);
42 }
43 }
44 return { command, options };
45}
46
47function loadEnvFile(file) {
48 if (!file || !fs.existsSync(file)) return;
49 const text = fs.readFileSync(file, 'utf8');
50 for (const line of text.split(/\r?\n/)) {
51 const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)=(.*)\s*$/);
52 if (!match || process.env[match[1]]) continue;
53 process.env[match[1]] = match[2].replace(/^['"]|['"]$/g, '');
54 }
55}
56
57(async () => {
58 const { command, options } = parseArgs(process.argv.slice(2));
59 loadEnvFile(options.env || path.join(process.cwd(), 'config/wix.env'));
60 const authToken = process.env.WIX_AUTH_TOKEN;
61 const siteId = process.env.WIX_SITE_ID;
62 if (!authToken || !siteId) throw new Error('WIX_AUTH_TOKEN and WIX_SITE_ID are required');
63 const wix = w.createWixClient({ authToken, siteId });
64 const common = { wix, siteId, artifactPath: options.artifact };
65
66 let result;
67 if (command === 'subscription-create') {
68 result = await verify.verifyStoresSubscriptionCreate({
69 ...common,
70 marker: options.marker,
71 cleanup: options.cleanup !== false,
72 });
73 if (options.proposalArtifact) {
74 result.artifactPath = options.artifact ? path.resolve(options.artifact) : null;
75 const proposal = createContractLedgerProposalFromStoresVerification(result);
76 proposal.sourceVerification.artifactPath = result.artifactPath;
77 writeProposal(options.proposalArtifact, proposal);
78 result.contractLedgerProposal = {
79 path: path.resolve(options.proposalArtifact),
80 status: proposal.status,
81 proposalId: proposal.proposalId,
82 };
83 if (options.artifact) {
84 fs.writeFileSync(options.artifact, `${JSON.stringify(result, null, 2)}\n`);
85 }
86 }
87 } else if (command === 'product-count') {
88 result = await verify.verifyStoresProductCount(common);
89 } else if (command === 'product-by-source-marker') {
90 result = await verify.verifyStoresProductBySourceMarker({
91 ...common,
92 markerPath: options.markerPath,
93 markerValue: options.markerValue,
94 });
95 } else if (command === 'delete-probe') {
96 result = await verify.verifyStoresDeleteProbe({ ...common, productId: options.productId });
97 } else {
98 usage();
99 process.exit(2);
100 }
101
102 console.log(JSON.stringify(result, null, 2));
103 process.exit(result.status === 'passed' ? 0 : 1);
104})().catch((error) => {
105 console.error(error && error.message ? error.message : error);
106 process.exit(1);
107});