Skip to content

Commit

Permalink
Add prompt to ask user if they want to download the errors from the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskataoka committed May 2, 2024
1 parent 5178c93 commit ed521fd
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions src/lib/media-import/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApolloClient, NormalizedCacheObject } from '@apollo/client';
import chalk from 'chalk';
import { prompt } from 'enquirer';
import gql from 'graphql-tag';
import { writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
Expand Down Expand Up @@ -272,8 +273,51 @@ ${ maybeExitPrompt }
void checkStatus( IMPORT_MEDIA_PROGRESS_POLL_INTERVAL );
} );

async function exportFailureDetails(
fileErrors: Maybe< AppEnvironmentMediaImportStatusFailureDetailsFileErrors >[]
) {
const formattedData = buildFileErrors( fileErrors, exportFileErrorsToJson );
const errorsFile = `media-import-${ app.name ?? '' }-${ Date.now() }${
exportFileErrorsToJson ? '.json' : '.txt'
}`;
try {
await writeFile( errorsFile, formattedData );
progressTracker.suffix += `${ chalk.yellow(
`⚠️ All errors have been exported to ${ chalk.bold( resolve( errorsFile ) ) }`
) }`;
} catch ( writeFileErr ) {
progressTracker.suffix += `${ chalk.red(
`Could not export errors to file\n${ ( writeFileErr as Error ).message }`
) }`;
}
}

async function promptFailureDetailsErrors( results: AppEnvironmentMediaImportStatus ) {
progressTracker.suffix += `${ chalk.yellow(
`⚠️ Error details can be found on ${ chalk.bold( results.failureDetailsUrl ) }. `
) }\n`;

const failureDetails = await prompt( {
type: 'confirm',
name: 'download',
message: '\nDo you want to download it now?',
} );

if ( ! failureDetails.download ) {
return;
}
progressTracker.suffix += `
=============================================================
Downloading errors details from ${ results.failureDetailsUrl }...
\n`;
const response = await fetch( results.failureDetailsUrl );

Check failure on line 313 in src/lib/media-import/status.ts

View workflow job for this annotation

GitHub Actions / Type Checker

Argument of type 'Maybe<string> | undefined' is not assignable to parameter of type 'string | URL | Request'.
const failureDetailsErrors =
( await response.json() ) as AppEnvironmentMediaImportStatusFailureDetailsFileErrors[];
await exportFailureDetails( failureDetailsErrors );
}

try {
const results = await getResults();
const results: AppEnvironmentMediaImportStatus = await getResults();
overallStatus = results.status ?? 'unknown';

progressTracker.stopPrinting();
Expand All @@ -282,36 +326,21 @@ ${ maybeExitPrompt }
progressTracker.print();

if ( results.failureDetailsUrl ) {
progressTracker.suffix += `\n\n${ chalk.yellow(
`⚠️ All errors have been exported to ${ chalk.bold(
results.failureDetailsUrl
) }. Please check the link for more details.`
) }\n\n`;
await promptFailureDetailsErrors( results );
} else {
// Falls back to exporting errors to a local file
const fileErrors = results.failureDetails?.fileErrors ?? [];
if ( fileErrors.length > 0 ) {
progressTracker.suffix += `${ chalk.yellow(
`⚠️ ${ fileErrors.length } file error(s) have been extracted`
) }`;

if ( ( results.filesTotal ?? 0 ) - ( results.filesProcessed ?? 0 ) !== fileErrors.length ) {
progressTracker.suffix += `. ${ chalk.italic.yellow(
'File-errors report size threshold reached.'
) }`;
}
const formattedData = buildFileErrors( fileErrors, exportFileErrorsToJson );
const errorsFile = `media-import-${ app.name ?? '' }-${ Date.now() }${
exportFileErrorsToJson ? '.json' : '.txt'
}`;
try {
await writeFile( errorsFile, formattedData );
progressTracker.suffix += `\n\n${ chalk.yellow(
`All errors have been exported to ${ chalk.bold( resolve( errorsFile ) ) }`
) }\n\n`;
} catch ( writeFileErr ) {
progressTracker.suffix += `\n\n${ chalk.red(
`Could not export errors to file\n${ ( writeFileErr as Error ).message }`
) }\n\n`;
}
await exportFailureDetails( fileErrors );
}
}

Expand Down

0 comments on commit ed521fd

Please sign in to comment.