Setting the file. One moment.
Plugin Knowledge · Rp Source Wordpress · wix/skills · Skills Docs
ContentsBack to the top of the page scripts/plugin-knowledge.js
scripts/ plugin-knowledge.js
JavaScript · 103 lines · 4 KB
12 listPlugins ,
13 readProfile ,
14 resolveRoute ,
15 resolveFromIndexKey ,
16 listCapabilities ,
17 } = require ( '../lib/plugin-knowledge.js' );
18 const { detectPlugins , summarizeDetection } = require ( '../lib/wp-plugin-detect.js' );
19
20 function usage () {
21 return [
22 'Usage:' ,
23 ' node scripts/plugin-knowledge.js list-plugins' ,
24 ' node scripts/plugin-knowledge.js read-plugin --slug woocommerce-subscriptions' ,
25 ' node scripts/plugin-knowledge.js resolve-route --route /wc/v3/subscriptions' ,
26 ' node scripts/plugin-knowledge.js resolve-namespace --namespace wc-bookings/v1' ,
27 ' node scripts/plugin-knowledge.js resolve-rest-base --rest-base tribe_events' ,
28 ' node scripts/plugin-knowledge.js resolve-property --property bundled_items' ,
29 ' node scripts/plugin-knowledge.js list-capabilities' ,
30 ' node scripts/plugin-knowledge.js detect --inventory <plugin-inventory.json>' ,
31 '' ,
32 'Add --pretty for human-readable output.' ,
33 ]. join ( ' \n ' );
34 }
35
36 function parseArgs ( argv ) {
37 const args = { _: [] };
38 for ( let i = 0 ; i < argv. length ; i += 1 ) {
39 const arg = argv[i];
40 if (arg. startsWith ( '--' )) {
41 const key = arg. slice ( 2 ). replace ( /-( [a-z] )/ g , ( _ , char ) => char. toUpperCase ());
42 const next = argv[i + 1 ];
43 if ( ! next || next. startsWith ( '--' )) {
44 args[key] = true ;
45 } else {
46 args[key] = next;
47 i += 1 ;
48 }
49 } else {
50 args._. push (arg);
51 }
52 }
53 return args;
54 }
55
56 function main () {
57 const args = parseArgs (process.argv. slice ( 2 ));
58 const command = args._[ 0 ];
59 const pluginsDir = pluginsRoot (path. resolve (__dirname, '..' ));
60
61 if ( ! command || args.help) {
62 process.stderr. write ( `${ usage () } \n ` );
63 process. exit (command ? 0 : 1 );
64 }
65
66 let output;
67 if (command === 'list-plugins' ) {
68 output = { plugins: listPlugins (pluginsDir) };
69 } else if (command === 'read-plugin' ) {
70 if ( ! args.slug) throw new Error ( '--slug is required' );
71 output = readProfile (pluginsDir, args.slug);
72 } else if (command === 'resolve-route' ) {
73 if ( ! args.route) throw new Error ( '--route is required' );
74 output = { route: args.route, matches: resolveRoute (pluginsDir, args.route) };
75 } else if (command === 'resolve-namespace' ) {
76 if ( ! args.namespace) throw new Error ( '--namespace is required' );
77 output = { namespace: args.namespace, plugins: resolveFromIndexKey (pluginsDir, 'namespaceIndex' , args.namespace) };
78 } else if (command === 'resolve-rest-base' ) {
79 if ( ! args.restBase) throw new Error ( '--rest-base is required' );
80 output = { restBase: args.restBase, plugins: resolveFromIndexKey (pluginsDir, 'restBaseIndex' , args.restBase) };
81 } else if (command === 'resolve-property' ) {
82 if ( ! args.property) throw new Error ( '--property is required' );
83 output = { property: args.property, plugins: resolveFromIndexKey (pluginsDir, 'recordPropertyIndex' , args.property) };
84 } else if (command === 'list-capabilities' ) {
85 output = { capabilities: listCapabilities (pluginsDir) };
86 } else if (command === 'detect' ) {
87 if ( ! args.inventory) throw new Error ( '--inventory is required' );
88 const inventory = JSON . parse (fs. readFileSync (args.inventory, 'utf8' ));
89 output = summarizeDetection (inventory);
90 } else {
91 throw new Error ( `Unknown command: ${ command } \n ${ usage () }` );
92 }
93 process.stdout. write ( `${ JSON . stringify ( output , null , args . pretty ? 2 : 0 ) } \n ` );
94 }
95
96 try {
97 main ();
98 } catch (error) {
99 process.stderr. write ( `${ error . message } \n ` );
100 process. exit ( 1 );
101 }
102
103 module . exports = { detectPlugins };