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