Skip to content

Commit 15b1cc0

Browse files
Apply code suggestions
1 parent 8a3c690 commit 15b1cc0

File tree

3 files changed

+69
-131
lines changed

3 files changed

+69
-131
lines changed

bin/plugin/commands/get-plugin-dir.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,17 @@ function doRunGetPluginDir( settings ) {
4747
// Read the plugins.json file synchronously.
4848
const { modules, plugins } = require( pluginsFile );
4949

50-
// Validate that the modules object is not empty.
51-
if ( modules && Object.keys( modules ).length !== 0 ) {
52-
for ( const module of Object.values( modules ) ) {
53-
if ( module.version && settings.slug === module.slug ) {
54-
log( 'build' );
55-
return;
56-
}
50+
for ( const module of Object.values( modules ) ) {
51+
if ( settings.slug === module.slug ) {
52+
log( 'build' );
53+
return;
5754
}
5855
}
5956

60-
// Validate that the plugins object is not empty.
61-
if ( plugins && Object.keys( plugins ).length !== 0 ) {
62-
for ( const plugin of Object.values( plugins ) ) {
63-
if ( plugin.version && settings.slug === plugin.slug ) {
64-
log( 'plugins' );
65-
return;
66-
}
57+
for ( const plugin of Object.values( plugins ) ) {
58+
if ( settings.slug === plugin.slug ) {
59+
log( 'plugins' );
60+
return;
6761
}
6862
}
6963
} catch ( error ) {

bin/plugin/commands/get-plugin-version.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,18 @@ function doRunGetPluginVersion( settings ) {
4747
// Read the plugins.json file synchronously.
4848
const { modules, plugins } = require( pluginsFile );
4949

50-
// Validate that the modules object is not empty.
51-
if ( modules && Object.keys( modules ).length !== 0 ) {
52-
for ( const module of Object.values( modules ) ) {
53-
if ( module.version && settings.slug === module.slug ) {
54-
log( module.version );
55-
return;
56-
}
50+
for ( const module of Object.values( modules ) ) {
51+
if ( settings.slug === module.slug ) {
52+
log( module.version );
53+
return;
5754
}
5855
}
5956

6057
// Validate that the plugins object is not empty.
61-
if ( plugins && Object.keys( plugins ).length !== 0 ) {
62-
for ( const plugin of Object.values( plugins ) ) {
63-
if ( plugin.version && settings.slug === plugin.slug ) {
64-
log( plugin.version );
65-
return;
66-
}
58+
for ( const plugin of Object.values( plugins ) ) {
59+
if ( settings.slug === plugin.slug ) {
60+
log( plugin.version );
61+
return;
6762
}
6863
}
6964
} catch ( error ) {

bin/plugin/commands/test-plugins.js

Lines changed: 53 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* External dependencies
33
*/
44
const fs = require( 'fs-extra' );
5+
const path = require( 'path' );
56
const { execSync, spawnSync } = require( 'child_process' );
67

78
/**
@@ -405,120 +406,68 @@ function doRunStandalonePluginTests( settings ) {
405406
// Buffer built plugins array.
406407
let builtPlugins = [];
407408

408-
// Buffer contents of plugins JSON file.
409-
let pluginsJsonFileContent = '';
409+
// Resolve the absolute path to the plugins.json file.
410+
const pluginsFile = path.join(
411+
__dirname,
412+
'../../../' + settings.pluginsJsonFile
413+
);
410414

411415
try {
412-
pluginsJsonFileContent = fs.readFileSync(
413-
settings.pluginsJsonFile,
414-
'utf-8'
415-
);
416-
} catch ( e ) {
417-
log(
418-
formats.error(
419-
`Error reading file at "${ settings.pluginsJsonFile }". ${ e }`
420-
)
421-
);
422-
}
423-
424-
// Validate that the plugins JSON file contains content before proceeding.
425-
if ( '' === pluginsJsonFileContent || ! pluginsJsonFileContent ) {
426-
log(
427-
formats.error(
428-
`Contents of file at "${ settings.pluginsJsonFile }" could not be read, or are empty.`
429-
)
430-
);
431-
}
432-
433-
const pluginsConfig = JSON.parse( pluginsJsonFileContent );
434-
435-
// Check for valid and not empty object resulting from plugins JSON file parse.
436-
if (
437-
'object' !== typeof pluginsConfig ||
438-
0 === Object.keys( pluginsConfig ).length
439-
) {
440-
log(
441-
formats.error(
442-
`File at "settings.pluginsJsonFile" parsed, but detected empty/non valid JSON object.`
443-
)
444-
);
445-
446-
// Return with exit code 1 to trigger a failure in the test pipeline.
447-
process.exit( 1 );
448-
}
449-
450-
const standalonePlugins = pluginsConfig.modules;
451-
if ( ! standalonePlugins ) {
452-
log(
453-
formats.error(
454-
'The given module configuration is invalid, the modules are missing, or they are misspelled.'
455-
)
456-
);
457-
458-
// Return with exit code 1 to trigger a failure in the test pipeline.
459-
process.exit( 1 );
460-
}
461-
462-
// Create an array of plugins from entries in plugins JSON file.
463-
builtPlugins = Object.keys( standalonePlugins )
464-
.filter( ( item ) => {
465-
if (
466-
! fs.pathExistsSync(
467-
`${ settings.builtPluginsDir }${ standalonePlugins[ item ].slug }`
468-
)
469-
) {
470-
log(
471-
formats.error(
472-
`Built plugin path "${ settings.builtPluginsDir }${ standalonePlugins[ item ].slug }" not found, skipping and removing from plugin list`
416+
// Read the plugins.json file synchronously.
417+
const { modules, plugins } = require( pluginsFile );
418+
419+
// Create an array of plugins from entries in plugins JSON file.
420+
builtPlugins = Object.keys( modules )
421+
.filter( ( item ) => {
422+
if (
423+
! fs.pathExistsSync(
424+
`${ settings.builtPluginsDir }${ modules[ item ].slug }`
473425
)
474-
);
475-
return false;
476-
}
477-
return true;
478-
} )
479-
.map( ( item ) => standalonePlugins[ item ].slug );
480-
481-
// Append plugins into array.
482-
const plugins = pluginsConfig.plugins;
483-
if ( ! plugins ) {
484-
log(
485-
formats.error(
486-
'The given plugin configuration is invalid, the plugins are missing, or they are misspelled.'
487-
)
488-
);
489-
490-
// Return with exit code 1 to trigger a failure in the test pipeline.
491-
process.exit( 1 );
492-
}
493-
494-
// Create an array of plugins from entries in plugins JSON file.
495-
builtPlugins = builtPlugins.concat(
496-
Object.values( plugins )
497-
.filter( ( plugin ) => {
498-
try {
499-
fs.copySync(
500-
`${ settings.pluginsDir }${ plugin.slug }/`,
501-
`${ settings.builtPluginsDir }${ plugin.slug }/`,
502-
{
503-
overwrite: true,
504-
}
505-
);
506-
log(
507-
formats.success( `Copied plugin "${ plugin.slug }".\n` )
508-
);
509-
return true;
510-
} catch ( e ) {
511-
// Handle the error appropriately
426+
) {
512427
log(
513428
formats.error(
514-
`Error copying plugin "${ plugin.slug }": ${ e.message }`
429+
`Built plugin path "${ settings.builtPluginsDir }${ modules[ item ].slug }" not found, skipping and removing from plugin list`
515430
)
516431
);
517432
return false;
518433
}
434+
return true;
519435
} )
520-
.map( ( plugin ) => plugin.slug )
521-
);
436+
.map( ( item ) => modules[ item ].slug );
437+
438+
// Create an array of plugins from entries in plugins JSON file.
439+
builtPlugins = builtPlugins.concat(
440+
Object.values( plugins )
441+
.filter( ( plugin ) => {
442+
try {
443+
fs.copySync(
444+
`${ settings.pluginsDir }${ plugin.slug }/`,
445+
`${ settings.builtPluginsDir }${ plugin.slug }/`,
446+
{
447+
overwrite: true,
448+
}
449+
);
450+
log(
451+
formats.success(
452+
`Copied plugin "${ plugin.slug }".\n`
453+
)
454+
);
455+
return true;
456+
} catch ( e ) {
457+
// Handle the error appropriately
458+
log(
459+
formats.error(
460+
`Error copying plugin "${ plugin.slug }": ${ e.message }`
461+
)
462+
);
463+
return false;
464+
}
465+
} )
466+
.map( ( plugin ) => plugin.slug )
467+
);
468+
} catch ( error ) {
469+
throw Error( `Error reading file at "${ pluginsFile }": ${ error }` );
470+
}
522471

523472
// For each built plugin, copy the test assets.
524473
builtPlugins.forEach( ( plugin ) => {

0 commit comments

Comments
 (0)