diff --git a/__tests__/commands/dev-env-sync-sql.js b/__tests__/commands/dev-env-sync-sql.js index a1d6ce556..98857d616 100644 --- a/__tests__/commands/dev-env-sync-sql.js +++ b/__tests__/commands/dev-env-sync-sql.js @@ -134,7 +134,7 @@ describe( 'commands/DevEnvSyncSQLCommand', () => { const cmd = new DevEnvSyncSQLCommand( app, env, 'test-slug' ); await cmd.runImport(); - expect( mockImport ).toHaveBeenCalledWith( true ); + expect( mockImport ).toHaveBeenCalled(); } ); } ); diff --git a/src/bin/vip-dev-env-import-sql.js b/src/bin/vip-dev-env-import-sql.js index 52f11ae0a..5efdc86c6 100755 --- a/src/bin/vip-dev-env-import-sql.js +++ b/src/bin/vip-dev-env-import-sql.js @@ -37,7 +37,9 @@ command( { .option( 'slug', 'Custom name of the dev environment' ) .option( [ 'r', 'search-replace' ], 'Perform Search and Replace on the specified SQL file' ) .option( 'in-place', 'Search and Replace explicitly on the given input file' ) - .option( 'skip-validate', 'Do not perform file validation.' ) + .option( 'skip-validate', 'Do not perform file validation' ) + .option( [ 'k', 'skip-reindex' ], 'Do not reindex data in Elasticsearch after import' ) + .option( 'quiet', 'Suppress prompts and informational messages' ) .examples( examples ) .argv( process.argv, async ( unmatchedArgs, opt ) => { const [ fileName ] = unmatchedArgs; diff --git a/src/commands/dev-env-import-sql.js b/src/commands/dev-env-import-sql.js index 0dbd90347..7359a1c96 100644 --- a/src/commands/dev-env-import-sql.js +++ b/src/commands/dev-env-import-sql.js @@ -3,7 +3,10 @@ import fs from 'fs'; import * as exit from '../lib/cli/exit'; import { getFileMeta, unzipFile } from '../lib/client-file-uploader'; -import { promptForBoolean, validateDependencies } from '../lib/dev-environment/dev-environment-cli'; +import { + processBooleanOption, + validateDependencies, +} from '../lib/dev-environment/dev-environment-cli'; import { getEnvironmentPath, resolveImportPath, @@ -25,9 +28,9 @@ export class DevEnvImportSQLCommand { this.slug = slug; } - async run( silent = false ) { + async run() { const lando = await bootstrapLando(); - await validateDependencies( lando, this.slug, silent ); + await validateDependencies( lando, this.slug, this.options.quiet ); validateImportFileExtension( this.fileName ); @@ -38,9 +41,16 @@ export class DevEnvImportSQLCommand { const sqlFile = `${ tmpDir }/sql-import.sql`; try { - console.log( `Extracting the compressed file ${ this.fileName }...` ); + if ( ! this.options.quiet ) { + console.log( `Extracting the compressed file ${ this.fileName }...` ); + } + await unzipFile( this.fileName, sqlFile ); - console.log( `${ chalk.green( '✓' ) } Extracted to ${ sqlFile }` ); + + if ( ! this.options.quiet ) { + console.log( `${ chalk.green( '✓' ) } Extracted to ${ sqlFile }` ); + } + this.fileName = sqlFile; } catch ( err ) { exit.withError( `Error extracting the SQL file: ${ err.message }` ); @@ -69,7 +79,9 @@ export class DevEnvImportSQLCommand { } const fd = await fs.promises.open( resolvedPath, 'r' ); - const importArg = [ 'db', '--disable-auto-rehash' ]; + const importArg = [ 'db', '--disable-auto-rehash' ].concat( + this.options.quiet ? '--silent' : [] + ); const origIsTTY = process.stdin.isTTY; try { @@ -83,7 +95,7 @@ export class DevEnvImportSQLCommand { process.stdin.isTTY = false; await exec( lando, this.slug, importArg, { stdio: [ fd, 'pipe', 'pipe' ] } ); - if ( ! silent ) { + if ( ! this.options.quiet ) { console.log( `${ chalk.green.bold( 'Success:' ) } Database imported.` ); } } finally { @@ -94,16 +106,15 @@ export class DevEnvImportSQLCommand { fs.unlinkSync( resolvedPath ); } - const cacheArg = [ 'wp', 'cache', 'flush' ]; + const cacheArg = [ 'wp', 'cache', 'flush' ].concat( this.options.quiet ? '--quiet' : [] ); await exec( lando, this.slug, cacheArg ); - try { - await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] ); - const doIndex = await promptForBoolean( - 'Do you want to index data in Elasticsearch (used by Enterprise Search)?', - true - ); - if ( doIndex ) { + if ( + undefined === this.options.skipReindex || + ! processBooleanOption( this.options.skipReindex ) + ) { + try { + await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] ); await exec( lando, this.slug, [ 'wp', 'vip-search', @@ -112,12 +123,17 @@ export class DevEnvImportSQLCommand { '--network-wide', '--skip-confirm', ] ); + } catch { + // Exception means they don't have vip-search enabled. } - } catch ( err ) { - // Exception means they don't have vip-search enabled. } - const addUserArg = [ 'wp', 'dev-env-add-admin', '--username=vipgo', '--password=password' ]; + const addUserArg = [ + 'wp', + 'dev-env-add-admin', + '--username=vipgo', + '--password=password', + ].concat( this.options.quiet ? '--quiet' : [] ); await exec( lando, this.slug, addUserArg ); } } diff --git a/src/commands/dev-env-sync-sql.js b/src/commands/dev-env-sync-sql.js index 126628690..880c6a0b8 100644 --- a/src/commands/dev-env-sync-sql.js +++ b/src/commands/dev-env-sync-sql.js @@ -177,9 +177,10 @@ export class DevEnvSyncSQLCommand { const importOptions = { inPlace: true, skipValidate: true, + quiet: true, }; const importCommand = new DevEnvImportSQLCommand( this.sqlFile, importOptions, this.slug ); - await importCommand.run( true ); + await importCommand.run(); } /**