Skip to content

Commit

Permalink
Add saveErrorLog flag to import media command (#2007)
Browse files Browse the repository at this point in the history
* Add saveErrorLog flag to import media command

* Update src/bin/vip-import-media-status.js

Co-authored-by: Ahmed Sayeed Wasif <[email protected]>

* Update src/bin/vip-import-media.js

Co-authored-by: Ahmed Sayeed Wasif <[email protected]>

* Convert saveErrorLog flag to string with three possible states

* Use string value provided to saveErrorLog, default to 'prompt', add support for specifying 'true' or 'yes'

* Allow specifying `--saveErrorLog` as a boolean flag even though the command stores three possible values

---------

Co-authored-by: Ahmed Sayeed Wasif <[email protected]>
  • Loading branch information
kevinfodness and aswasif007 authored Sep 2, 2024
1 parent 4391389 commit e6e12f1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/bin/vip-import-media-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ command( {
'Export any file errors encountered to a JSON file instead of a plain text file',
false
)
.argv( process.argv, async ( arg, { app, env, exportFileErrorsToJson } ) => {
.option( 'saveErrorLog', 'Download file-error logs without prompting', 'prompt' )
.argv( process.argv, async ( arg, { app, env, exportFileErrorsToJson, saveErrorLog } ) => {
const { id: envId, appId } = env;
const track = trackEventWithEnv.bind( null, appId, envId );

Expand All @@ -53,5 +54,11 @@ command( {
Checking the Media import status for your environment...
`;

await mediaImportCheckStatus( { app, env, progressTracker, exportFileErrorsToJson } );
await mediaImportCheckStatus( {
app,
env,
progressTracker,
exportFileErrorsToJson,
saveErrorLog,
} );
} );
21 changes: 17 additions & 4 deletions src/bin/vip-import-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,25 @@ Are you sure you want to import the contents of the url?
'Export any file errors encountered to a JSON file instead of a plain text file',
false
)
.option( 'saveErrorLog', 'Download file-error logs without prompting' )
.option( 'overwriteExistingFiles', 'Overwrite any existing files', false )
.option( 'importIntermediateImages', 'Import intermediate image files', false )
.examples( examples )
.argv( process.argv, async ( args, opts ) => {
const { app, env, exportFileErrorsToJson, overwriteExistingFiles, importIntermediateImages } =
opts;
const {
app,
env,
exportFileErrorsToJson,
saveErrorLog,
overwriteExistingFiles,
importIntermediateImages,
} = opts;
const [ url ] = args;

if ( ! isSupportedUrl( url ) ) {
console.log(
chalk.red( `
Error:
Error:
Invalid URL provided: ${ url }
Please make sure that it is a publicly accessible web URL containing an archive of the media files to import` )
);
Expand Down Expand Up @@ -149,7 +156,13 @@ Importing Media into your App...
},
} );

await mediaImportCheckStatus( { app, env, progressTracker, exportFileErrorsToJson } );
await mediaImportCheckStatus( {
app,
env,
progressTracker,
exportFileErrorsToJson,
saveErrorLog,
} );
} catch ( error ) {
if ( error.graphQLErrors ) {
for ( const err of error.graphQLErrors ) {
Expand Down
18 changes: 18 additions & 0 deletions src/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ args.argv = async function ( argv, cb ) {
}
}

// Negotiate flag values
switch ( _opts.module ) {
case 'import-media':
if ( [ true, 'true', 'yes' ].includes( options.saveErrorLog ) ) {
options.saveErrorLog = 'true';
} else if ( [ false, 'false', 'no' ].includes( options.saveErrorLog ) ) {
options.saveErrorLog = 'false';
} else {
options.saveErrorLog = 'prompt';
}
break;
}

// Prompt for confirmation if necessary
if ( _opts.requireConfirm && ! options.force ) {
/** @type {Tuple[]} */
Expand Down Expand Up @@ -475,6 +488,11 @@ args.argv = async function ( argv, cb ) {
key: 'Export any file errors encountered to a JSON file instead of a plain text file.',
value: options.exportFileErrorsToJson ? '✅ Yes' : `${ chalk.red( 'x' ) } No`,
} );

info.push( {
key: 'Download file-error logs?',
value: options.saveErrorLog,
} );
break;
default:
}
Expand Down
19 changes: 13 additions & 6 deletions src/lib/media-import/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface MediaImportCheckStatusInput {
env: AppEnvironment;
progressTracker: MediaImportProgressTracker;
exportFileErrorsToJson: boolean;
saveErrorLog: string;
}

async function getStatus(
Expand Down Expand Up @@ -159,6 +160,7 @@ export async function mediaImportCheckStatus( {
env,
progressTracker,
exportFileErrorsToJson,
saveErrorLog,
}: MediaImportCheckStatusInput ) {
// Stop printing so we can pass our callback
progressTracker.stopPrinting();
Expand Down Expand Up @@ -306,12 +308,17 @@ Downloading errors details from ${ fileErrorsUrl }
}

async function promptFailureDetailsDownload( fileErrorsUrl: string ) {
const failureDetails = await prompt( {
type: 'confirm',
name: 'download',
message:
'Download import errors report now? (Report will be downloadable for up to 7 days from the completion of the import)',
} );
const failureDetails =
'prompt' === saveErrorLog
? await prompt( {
type: 'confirm',
name: 'download',
message:
'Download import errors report now? (Report will be downloadable for up to 7 days from the completion of the import)',
} )
: {
download: [ 'true', 'yes' ].includes( saveErrorLog ),
};

if ( ! failureDetails.download ) {
progressTracker.suffix += `${ chalk.yellow(
Expand Down

0 comments on commit e6e12f1

Please sign in to comment.