From 96b6f9c104e6312851e039dbf08908f4c6083663 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Sat, 9 Dec 2023 01:25:46 +0200 Subject: [PATCH 1/6] feat(dev-env): add quiet mode for "import sql" --- src/bin/vip-dev-env-import-sql.js | 1 + src/commands/dev-env-import-sql.js | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/bin/vip-dev-env-import-sql.js b/src/bin/vip-dev-env-import-sql.js index 52f11ae0a..1a56183f9 100755 --- a/src/bin/vip-dev-env-import-sql.js +++ b/src/bin/vip-dev-env-import-sql.js @@ -38,6 +38,7 @@ command( { .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( 'quiet', 'Suppress prompts.' ) .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..8c974137c 100644 --- a/src/commands/dev-env-import-sql.js +++ b/src/commands/dev-env-import-sql.js @@ -25,9 +25,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 +38,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 }` ); @@ -83,7 +90,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 { @@ -99,10 +106,13 @@ export class DevEnvImportSQLCommand { 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 - ); + const doIndex = + this.options.quiet || ! process.stdin.isTTY + ? true + : await promptForBoolean( + 'Do you want to index data in Elasticsearch (used by Enterprise Search)?', + true + ); if ( doIndex ) { await exec( lando, this.slug, [ 'wp', From fa804b67143c524d2141ed9961c8545d545d0eeb Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Thu, 21 Dec 2023 02:24:22 +0200 Subject: [PATCH 2/6] feat(dev-env): add `--skip-reindex` option for `import sql` --- src/bin/vip-dev-env-import-sql.js | 5 +-- src/commands/dev-env-import-sql.js | 51 ++++++++++++++++++------------ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/bin/vip-dev-env-import-sql.js b/src/bin/vip-dev-env-import-sql.js index 1a56183f9..52379dc12 100755 --- a/src/bin/vip-dev-env-import-sql.js +++ b/src/bin/vip-dev-env-import-sql.js @@ -37,8 +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( 'quiet', 'Suppress prompts.' ) + .option( 'skip-validate', 'Do not perform file validation' ) + .option( [ 'k', 'skip-reindex' ], 'Do not reindex data in Elasticsearch after import' ) + .option( 'quiet', 'Suppress prompts' ) .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 8c974137c..c14c8cad4 100644 --- a/src/commands/dev-env-import-sql.js +++ b/src/commands/dev-env-import-sql.js @@ -3,7 +3,11 @@ 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, + promptForBoolean, + validateDependencies, +} from '../lib/dev-environment/dev-environment-cli'; import { getEnvironmentPath, resolveImportPath, @@ -104,27 +108,32 @@ export class DevEnvImportSQLCommand { const cacheArg = [ 'wp', 'cache', 'flush' ]; await exec( lando, this.slug, cacheArg ); - try { - await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] ); - const doIndex = - this.options.quiet || ! process.stdin.isTTY - ? true - : await promptForBoolean( - 'Do you want to index data in Elasticsearch (used by Enterprise Search)?', - true - ); - if ( doIndex ) { - await exec( lando, this.slug, [ - 'wp', - 'vip-search', - 'index', - '--setup', - '--network-wide', - '--skip-confirm', - ] ); + if ( + undefined === this.options.skipReindex || + ! processBooleanOption( this.options.skipReindex ) + ) { + try { + await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] ); + const doIndex = + this.options.quiet || undefined !== this.options.skipReindex || ! process.stdin.isTTY + ? true + : await promptForBoolean( + 'Do you want to index data in Elasticsearch (used by Enterprise Search)?', + true + ); + if ( doIndex ) { + await exec( lando, this.slug, [ + 'wp', + 'vip-search', + 'index', + '--setup', + '--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' ]; From 62723604bbb2327e5bbdfff654dd3d7de64da8cc Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Wed, 27 Dec 2023 13:05:34 +0200 Subject: [PATCH 3/6] fix: update help for `--quiet` --- src/bin/vip-dev-env-import-sql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/vip-dev-env-import-sql.js b/src/bin/vip-dev-env-import-sql.js index 52379dc12..5efdc86c6 100755 --- a/src/bin/vip-dev-env-import-sql.js +++ b/src/bin/vip-dev-env-import-sql.js @@ -39,7 +39,7 @@ command( { .option( 'in-place', 'Search and Replace explicitly on the given input file' ) .option( 'skip-validate', 'Do not perform file validation' ) .option( [ 'k', 'skip-reindex' ], 'Do not reindex data in Elasticsearch after import' ) - .option( 'quiet', 'Suppress prompts' ) + .option( 'quiet', 'Suppress prompts and informational messages' ) .examples( examples ) .argv( process.argv, async ( unmatchedArgs, opt ) => { const [ fileName ] = unmatchedArgs; From 8d2ce9d8b559cb3b5eee108e411392718506cd9f Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Tue, 9 Jan 2024 00:15:09 +0200 Subject: [PATCH 4/6] fix: remove interactivity --- src/commands/dev-env-import-sql.js | 39 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/commands/dev-env-import-sql.js b/src/commands/dev-env-import-sql.js index c14c8cad4..7359a1c96 100644 --- a/src/commands/dev-env-import-sql.js +++ b/src/commands/dev-env-import-sql.js @@ -5,7 +5,6 @@ import * as exit from '../lib/cli/exit'; import { getFileMeta, unzipFile } from '../lib/client-file-uploader'; import { processBooleanOption, - promptForBoolean, validateDependencies, } from '../lib/dev-environment/dev-environment-cli'; import { @@ -80,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 { @@ -105,7 +106,7 @@ 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 ); if ( @@ -114,29 +115,25 @@ export class DevEnvImportSQLCommand { ) { try { await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] ); - const doIndex = - this.options.quiet || undefined !== this.options.skipReindex || ! process.stdin.isTTY - ? true - : await promptForBoolean( - 'Do you want to index data in Elasticsearch (used by Enterprise Search)?', - true - ); - if ( doIndex ) { - await exec( lando, this.slug, [ - 'wp', - 'vip-search', - 'index', - '--setup', - '--network-wide', - '--skip-confirm', - ] ); - } + await exec( lando, this.slug, [ + 'wp', + 'vip-search', + 'index', + '--setup', + '--network-wide', + '--skip-confirm', + ] ); } catch { // 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 ); } } From ce9d3618d49417c2535e1960dc62c1bb73556c84 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Thu, 11 Jan 2024 04:03:40 +0200 Subject: [PATCH 5/6] fix: command invocation from dev-env-sync-sql --- src/commands/dev-env-sync-sql.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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(); } /** From 3e073ce884645b4aec9b452f88c93f3aca3ff347 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Thu, 11 Jan 2024 04:14:32 +0200 Subject: [PATCH 6/6] test: fix tests --- __tests__/commands/dev-env-sync-sql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); } ); } );