Setting the file. One moment.
Plugin Full Read · Rp Source Wordpress · wix/skills · Skills Docs
ContentsBack to the top of the page 39.89
Product Video Sources
scripts/plugin-full-read.js
scripts/ plugin-full-read.js
JavaScript · 71 lines · 4 KB
// data/source-extract/<entity>.deferred.json), 1 on a usage or setup error.
15
16 const path = require ( 'node:path' );
17 const fs = require ( 'node:fs' );
18 const discovery = require ( './wp-discovery.js' );
19 const { buildHeaders , configureRateLimit } = require ( '../lib/wp-http.js' );
20 const plans = require ( '../lib/plugin-entity-read-plan.js' );
21
22 function splitArgs ( argv ) {
23 const own = { entities: [], restart: false };
24 const rest = [];
25 for ( let i = 0 ; i < argv. length ; i += 1 ) {
26 const arg = argv[i];
27 if (arg === '--project-dir' ) { own.projectDir = argv[i + 1 ]; i += 1 ; }
28 else if (arg === '--entity' ) { own.entities. push (argv[i + 1 ]); i += 1 ; }
29 else if (arg === '--restart' ) own.restart = true ;
30 else rest. push (arg);
31 }
32 return { own, rest };
33 }
34
35 async function main ( argv , { createTransport = discovery.createBridgeTransport } = {}) {
36 const { own , rest } = splitArgs (argv);
37 if ( ! own.projectDir || own.entities. some (( entity ) => ! entity)) {
38 console. error ( 'usage: plugin-full-read.js --project-dir <dir> [--env-file <file>] [--base-url <url>] [--entity <ref>]... [--restart]' );
39 return 1 ;
40 }
41 const projectDir = path. resolve (own.projectDir);
42 const args = await discovery. hydrateArgsFromEnvFile (discovery. parseArgs (rest));
43 const schemaPath = path. join (projectDir, 'source-schema.json' );
44 const schema = fs. existsSync (schemaPath) ? JSON . parse (fs. readFileSync (schemaPath, 'utf8' )) : {};
45 const baseUrl = args.baseUrl || schema.sourceUrl || schema.source?.url;
46 if ( ! baseUrl) { console. error ( 'no source URL: pass --base-url or keep source-schema.json sourceUrl' ); return 1 ; }
47
48 let entities = plans. plannedPluginEntities (projectDir);
49 if (own.entities. length ) {
50 const unknown = own.entities. filter (( ref ) => ! entities. some (( entity ) => entity.sourceEntityRef === ref));
51 if (unknown. length ) { console. error ( `no current read plan for: ${ unknown . join ( ', ' ) }` ); return 1 ; }
52 entities = entities. filter (( entity ) => own.entities. includes (entity.sourceEntityRef));
53 }
54
55 configureRateLimit ({ rateLimitRpm: args.rateLimitRpm, maxRetries: args.maxRetries });
56 const sourceReads = discovery. makeSourceReadCollector (path. join (projectDir, 'discovery' , 'wordpress' ));
57 const transport = createTransport ({
58 baseUrl: plans. sourceIdentity (baseUrl), headers: buildHeaders (args),
59 timeoutMs: args.timeoutMs, onSourceRead: sourceReads.onSourceRead,
60 });
61 const results = await plans. readPluginEntitiesForMapping ({ projectDir, transport, entities, restart: own.restart });
62 sourceReads. flush ();
63 console. log ( JSON . stringify ({ results }, null , 2 ));
64 return results. every (( result ) => [ 'current' , 'promoted' , 'components-read' ]. includes (result.status)) ? 0 : 2 ;
65 }
66
67 if (require.main === module ) {
68 main (process.argv. slice ( 2 )). then (( code ) => { process.exitCode = code; }, ( error ) => { console. error (error.message); process.exitCode = 1 ; });
69 }
70
71 module . exports = { main, splitArgs };