Skip to content

Commit

Permalink
Env: Check for legacy installs and provide the option to delete them. (
Browse files Browse the repository at this point in the history
…#20340)

* Env: Check for legacy installs and provide the option to delete them.

* Fix tests.
  • Loading branch information
epiqueras authored Feb 21, 2020
1 parent 5d41e72 commit a77bad2
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 2 deletions.
182 changes: 180 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/env/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const withSpinner = ( command ) => ( ...args ) => {
time[ 1 ] / 1e6
).toFixed( 0 ) }ms)`
);
process.exit( 0 );
},
( error ) => {
if ( error instanceof env.ValidationError ) {
Expand Down
49 changes: 49 additions & 0 deletions packages/env/lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const path = require( 'path' );
const fs = require( 'fs' ).promises;
const dockerCompose = require( 'docker-compose' );
const yaml = require( 'js-yaml' );
const inquirer = require( 'inquirer' );

/**
* Promisified dependencies
*/
const copyDir = util.promisify( require( 'copy-dir' ) );
const sleep = util.promisify( setTimeout );
const rimraf = util.promisify( require( 'rimraf' ) );

/**
* Internal dependencies
Expand Down Expand Up @@ -47,6 +49,7 @@ module.exports = {
*/
await module.exports.stop( { spinner } );

await checkForLegacyInstall( spinner );
const config = await initConfig( { spinner, debug } );

spinner.text = 'Downloading WordPress.';
Expand Down Expand Up @@ -238,6 +241,52 @@ module.exports = {
ValidationError,
};

/**
* Checks for legacy installs and provides
* the user the option to delete them.
*
* @param {Object} spinner A CLI spinner which indicates progress.
*/
async function checkForLegacyInstall( spinner ) {
const basename = path.basename( process.cwd() );
const installs = [
`../${ basename }-wordpress`,
`../${ basename }-tests-wordpress`,
];
await Promise.all(
installs.map( ( install ) =>
fs
.access( install )
.catch( () =>
installs.splice( installs.indexOf( install ), 1 )
)
)
);
if ( ! installs.length ) {
return;
}

spinner.info(
`It appears that you have used a previous version of this tool where installs were kept in ${ installs.join(
' and '
) }. Installs are now in your home folder.\n`
);
const { yesDelete } = await inquirer.prompt( [
{
type: 'confirm',
name: 'yesDelete',
message:
'Do you wish to delete these old installs to reclaim disk space?',
default: true,
},
] );
if ( yesDelete ) {
await Promise.all( installs.map( ( install ) => rimraf( install ) ) );
spinner.info( 'Old installs deleted successfully.' );
}
spinner.start();
}

/**
* Initializes the local environment so that Docker commands can be run. Reads
* ./.wp-env.json, creates ~/.wp-env, and creates ~/.wp-env/docker-compose.yml.
Expand Down
2 changes: 2 additions & 0 deletions packages/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
"chalk": "^2.4.2",
"copy-dir": "^1.2.0",
"docker-compose": "^0.22.2",
"inquirer": "^7.0.4",
"js-yaml": "^3.13.1",
"nodegit": "^0.26.2",
"ora": "^4.0.2",
"rimraf": "^3.0.2",
"terminal-link": "^2.0.0",
"yargs": "^14.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/env/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const env = require( '../lib/env' );
/**
* Mocked dependencies
*/
jest.spyOn( process, 'exit' ).mockImplementation( () => {} );
jest.mock( 'ora', () => () => ( {
start() {
return { text: '', succeed: jest.fn(), fail: jest.fn() };
Expand Down

0 comments on commit a77bad2

Please sign in to comment.