Skip to content

Commit

Permalink
wp-env: Better errors when Docker is not started (#30882)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen authored Apr 15, 2021

Unverified

This user has not yet uploaded their public signing key.
1 parent 2f044a0 commit d21846f
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -204,7 +204,7 @@ module.exports = {
},
},
{
files: [ 'bin/**/*.js' ],
files: [ 'bin/**/*.js', 'packages/env/**' ],
rules: {
'no-console': 'off',
},
12 changes: 11 additions & 1 deletion packages/env/lib/cli.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ const chalk = require( 'chalk' );
const ora = require( 'ora' );
const yargs = require( 'yargs' );
const terminalLink = require( 'terminal-link' );
const { execSync } = require( 'child_process' );

/**
* Internal dependencies
@@ -63,7 +64,6 @@ const withSpinner = ( command ) => ( ...args ) => {
typeof error === 'string' ? error : error.message
);
// Disable reason: Using console.error() means we get a stack trace.
// eslint-disable-next-line no-console
console.error( error );
process.exit( 1 );
} else {
@@ -75,6 +75,16 @@ const withSpinner = ( command ) => ( ...args ) => {
};

module.exports = function cli() {
// Do nothing if Docker is unavailable.
try {
execSync( 'docker info', { stdio: 'ignore' } );
} catch {
console.error(
chalk.red( 'Could not connect to Docker. Is it running?' )
);
process.exit( 1 );
}

yargs.usage( wpPrimary( '$0 <command>' ) );
yargs.option( 'debug', {
type: 'boolean',
2 changes: 0 additions & 2 deletions packages/env/lib/commands/logs.js
Original file line number Diff line number Diff line change
@@ -60,12 +60,10 @@ module.exports = async function logs( { environment, watch, spinner, debug } ) {
);

if ( result.out.length ) {
// eslint-disable-next-line no-console
console.log(
process.stdout.isTTY ? `\n\n${ result.out }\n\n` : result.out
);
} else if ( result.err.length ) {
// eslint-disable-next-line no-console
console.error(
process.stdout.isTTY ? `\n\n${ result.err }\n\n` : result.err
);
4 changes: 0 additions & 4 deletions packages/env/test/cli.js
Original file line number Diff line number Diff line change
@@ -82,7 +82,6 @@ describe( 'env cli', () => {
} );

it( 'handles failed commands with messages.', async () => {
/* eslint-disable no-console */
env.start.mockRejectedValueOnce( {
message: 'failure message',
} );
@@ -100,10 +99,8 @@ describe( 'env cli', () => {
expect( process.exit ).toHaveBeenCalledWith( 1 );
console.error = consoleError;
process.exit = processExit;
/* eslint-enable no-console */
} );
it( 'handles failed docker commands with errors.', async () => {
/* eslint-disable no-console */
env.start.mockRejectedValueOnce( {
err: 'failure error',
out: 'message',
@@ -128,6 +125,5 @@ describe( 'env cli', () => {
console.error = consoleError;
process.exit = processExit;
process.stderr.write = stderr;
/* eslint-enable no-console */
} );
} );

0 comments on commit d21846f

Please sign in to comment.