Skip to content

Commit

Permalink
Merge pull request #1638 from Automattic/fix/sonarcloud-issues
Browse files Browse the repository at this point in the history
fix: issues found by SonarCloud
  • Loading branch information
sjinks authored Jan 9, 2024
2 parents a092033 + be4a2d2 commit 09c79ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/commands/dev-env-import-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class DevEnvImportSQLCommand {
process.stdin.isTTY = origIsTTY;
}

if ( searchReplace && searchReplace.length && ! inPlace ) {
if ( searchReplace?.length && ! inPlace ) {
fs.unlinkSync( resolvedPath );
}

Expand Down
58 changes: 25 additions & 33 deletions src/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,7 @@ args.argv = async function ( argv, cb ) {
exit.withError( `Failed to get app (${ _opts.appQuery }) details: ${ message }` );
}

if (
! res ||
! res.data ||
! res.data.apps ||
! res.data.apps.edges ||
! res.data.apps.edges.length
) {
if ( ! res.data?.apps?.edges?.length ) {
await trackEvent( 'command_appcontext_list_fetch_error', {
error: 'No apps found',
} );
Expand Down Expand Up @@ -229,7 +223,7 @@ args.argv = async function ( argv, cb ) {
// Copy all app information
appSelection.app = res.data.apps.edges.find( cur => cur.name === appSelection.app );

if ( ! appSelection || ! appSelection.app || ! appSelection.app.id ) {
if ( ! appSelection.app?.id ) {
await trackEvent( 'command_appcontext_list_select_error', {
error: 'Invalid app selected',
} );
Expand All @@ -239,7 +233,7 @@ args.argv = async function ( argv, cb ) {

await trackEvent( 'command_appcontext_list_select_success' );

options.app = Object.assign( {}, appSelection.app );
options.app = { ...appSelection.app };
} else {
let appLookup;
try {
Expand All @@ -252,7 +246,7 @@ args.argv = async function ( argv, cb ) {
exit.withError( `App ${ chalk.blueBright( options.app ) } does not exist` );
}

if ( ! appLookup || ! appLookup.id ) {
if ( ! appLookup?.id ) {
await trackEvent( 'command_appcontext_param_error', {
error: 'Invalid app specified',
} );
Expand All @@ -262,7 +256,7 @@ args.argv = async function ( argv, cb ) {

await trackEvent( 'command_appcontext_param_select' );

options.app = Object.assign( {}, appLookup );
options.app = { ...appLookup };
}

if ( _opts.childEnvContext ) {
Expand Down Expand Up @@ -297,7 +291,7 @@ args.argv = async function ( argv, cb ) {
}

options.env = env;
} else if ( ! options.app || ! options.app.environments || ! options.app.environments.length ) {
} else if ( ! options.app?.environments?.length ) {
console.log( 'To set up a new development environment, please contact VIP Support.' );

await trackEvent( 'command_childcontext_fetch_error', {
Expand Down Expand Up @@ -337,7 +331,7 @@ args.argv = async function ( argv, cb ) {
envObject => getEnvIdentifier( envObject ) === envSelection.env
);

if ( ! envSelection || ! envSelection.env || ! envSelection.env.id ) {
if ( ! envSelection.env?.id ) {
await trackEvent( 'command_childcontext_list_select_error', {
error: 'Invalid environment selected',
} );
Expand Down Expand Up @@ -375,13 +369,13 @@ args.argv = async function ( argv, cb ) {
switch ( _opts.module ) {
case 'import-sql': {
const site = options.env;
if ( site && site.primaryDomain ) {
if ( site?.primaryDomain ) {
const primaryDomainName = site.primaryDomain.name;
info.push( { key: 'Primary Domain Name', value: primaryDomainName } );
}

// Site launched details
const haveLaunchedField = Object.prototype.hasOwnProperty.call( site, 'launched' );
const haveLaunchedField = Object.hasOwn( site, 'launched' );

if ( haveLaunchedField ) {
const launched = site.launched ? '✅ Yes' : `${ chalk.red( 'x' ) } No`;
Expand All @@ -394,7 +388,7 @@ args.argv = async function ( argv, cb ) {
}

options.skipValidate =
Object.prototype.hasOwnProperty.call( options, 'skipValidate' ) &&
Object.hasOwn( options, 'skipValidate' ) &&
Boolean( options.skipValidate ) &&
! [ 'false', 'no' ].includes( options.skipValidate );

Expand Down Expand Up @@ -454,7 +448,7 @@ args.argv = async function ( argv, cb ) {
info.push( { key: 'Archive URL', value: chalk.blue.underline( this.sub ) } );

options.overwriteExistingFiles =
Object.prototype.hasOwnProperty.call( options, 'overwriteExistingFiles' ) &&
Object.hasOwn( options, 'overwriteExistingFiles' ) &&
Boolean( options.overwriteExistingFiles ) &&
! [ 'false', 'no' ].includes( options.overwriteExistingFiles );
info.push( {
Expand All @@ -463,7 +457,7 @@ args.argv = async function ( argv, cb ) {
} );

options.importIntermediateImages =
Object.prototype.hasOwnProperty.call( options, 'importIntermediateImages' ) &&
Object.hasOwn( options, 'importIntermediateImages' ) &&
Boolean( options.importIntermediateImages ) &&
! [ 'false', 'no' ].includes( options.importIntermediateImages );
info.push( {
Expand All @@ -472,7 +466,7 @@ args.argv = async function ( argv, cb ) {
} );

options.exportFileErrorsToJson =
Object.prototype.hasOwnProperty.call( options, 'exportFileErrorsToJson' ) &&
Object.hasOwn( options, 'exportFileErrorsToJson' ) &&
Boolean( options.exportFileErrorsToJson ) &&
! [ 'false', 'no' ].includes( options.exportFileErrorsToJson );
info.push( {
Expand Down Expand Up @@ -503,7 +497,7 @@ args.argv = async function ( argv, cb ) {
}

res = res.map( row => {
const out = Object.assign( {}, row );
const out = { ...row };

if ( out.__typename ) {
// Apollo injects __typename
Expand Down Expand Up @@ -557,19 +551,17 @@ function validateOpts( opts ) {
* @returns {args}
*/
export default function ( opts ) {
_opts = Object.assign(
{
appContext: false,
appQuery: 'id,name',
childEnvContext: false,
envContext: false,
format: false,
requireConfirm: false,
requiredArgs: 0,
wildcardCommand: false,
},
opts
);
_opts = {
appContext: false,
appQuery: 'id,name',
childEnvContext: false,
envContext: false,
format: false,
requireConfirm: false,
requiredArgs: 0,
wildcardCommand: false,
...opts,
};

if ( _opts.appContext || _opts.requireConfirm ) {
args.option( 'app', 'Specify the app' );
Expand Down

0 comments on commit 09c79ce

Please sign in to comment.