Setting the file. One moment.
Cms Item Quota · Wix Replatform · wix/skills · Skills Docs
ContentsBack to the top of the page scripts/cms-item-quota.js
scripts/ cms-item-quota.js
JavaScript · 67 lines · 4 KB
14 // and its cutover action, if any. `skip` records the operator's explicit decision that the
15 // waiting data of the named archive collections is not needed (who and why; later archive items are journaled
16 // skipped_out_of_scope, never silently dropped); `unskip` undoes it. None writes to the site.
17
18 const fs = require ( 'node:fs' );
19 const path = require ( 'node:path' );
20 const quota = require ( '../resources/rp-target-wix/lib/cms-item-quota.js' );
21
22 const PLAN_FILE = 'execution/cms-item-quota-plan.json' ;
23
24 function readEnv ( projectDir ) {
25 const raw = fs. readFileSync (path. join (projectDir, 'config' , 'wix.env' ), 'utf8' );
26 return Object. fromEntries (raw. split ( / \r ? \n / ). map (( line ) => line. match ( / ^ ( [A-Z0-9_] + )=( . * ) $ / )). filter (Boolean). map (([, key , value ]) => [key, value. trim ()]));
27 }
28
29 async function plan ( projectDir , expectedRows , { wix = null } = {}) {
30 let client = wix;
31 if ( ! client) {
32 const env = readEnv (projectDir);
33 const wixAuth = require ( '../lib/wix-auth.js' );
34 const writers = require ( '../resources/rp-target-wix/lib/wix-writers.js' );
35 client = writers. createWixClient ({ siteId: env. WIX_SITE_ID , tokenSource: wixAuth. createWixTokenSource ({ projectDir, siteId: env. WIX_SITE_ID }) });
36 }
37 const line = quota. quotaPlanLine ({ expectedRows, quota: await quota. readCmsItemQuota (client) });
38 const file = path. join (projectDir, PLAN_FILE );
39 fs. mkdirSync (path. dirname (file), { recursive: true });
40 fs. writeFileSync (file, `${ JSON . stringify ({ schemaVersion: 1 , ... line }, null , 2 ) } \n ` );
41 return line;
42 }
43
44 function parseArgs ( argv ) {
45 const args = { command: argv[ 0 ] };
46 for ( let i = 1 ; i < argv. length ; i += 1 ) {
47 if (argv[i] === '--project-dir' ) args.projectDir = path. resolve (argv[ ++ i]);
48 else if (argv[i] === '--expected-rows' ) args.expectedRows = Number (argv[ ++ i]);
49 else if (argv[i] === '--collection' ) (args.collectionIds = args.collectionIds || []). push (argv[ ++ i]);
50 else if (argv[i] === '--decided-by' ) args.decidedBy = argv[ ++ i];
51 else if (argv[i] === '--reason' ) args.reason = argv[ ++ i];
52 else throw new Error ( `unknown argument ${ argv [ i ] }` );
53 }
54 return args;
55 }
56
57 async function main () {
58 const args = parseArgs (process.argv. slice ( 2 ));
59 if ( ! args.projectDir || ! [ 'plan' , 'status' , 'skip' , 'unskip' ]. includes (args.command)) throw new Error ( 'usage: cms-item-quota.js plan|status|skip|unskip --project-dir <dir> [--expected-rows <n>] [--collection <id> --decided-by <name> --reason <why>]' );
60 if (args.command === 'plan' ) console. log ( JSON . stringify ( await plan (args.projectDir, args.expectedRows), null , 2 ));
61 else if (args.command === 'skip' ) console. log ( JSON . stringify (quota. recordArchiveSkip (args.projectDir, { decidedBy: args.decidedBy, reason: args.reason, collectionIds: args.collectionIds }), null , 2 ));
62 else if (args.command === 'unskip' ) console. log ( JSON . stringify (quota. clearArchiveSkip (args.projectDir), null , 2 ));
63 else console. log ( JSON . stringify ({ hold: quota. readHold (args.projectDir), cutoverAction: quota. cutoverAction (args.projectDir) }, null , 2 ));
64 }
65
66 if (require.main === module ) main (). catch (( error ) => { console. error (error.message || error); process. exit ( 1 ); });
67 module . exports = { plan, PLAN_FILE };