UNPKG

1.72 kBJavaScriptView Raw
1const inquirer = require( 'inquirer' );
2const chalk = require( 'chalk' );
3const nconf = require( 'nconf' );
4const sh = require( 'shelljs' );
5const prompt = require( '../lib/prompt' );
6const config = require( '../lib/config' );
7const util = require( '../lib/util' );
8const hosts = require( '../lib/hosts' );
9
10/**
11 * The main command.
12 */
13async function remove() {
14 if ( !util.localProjectsExist() ) {
15 console.error( chalk`{red.bold No projects available.}` );
16 process.exit( 1 );
17 }
18
19 const projObj = await inquirer.prompt( prompt.getDestroyPrompt() );
20 console.log( chalk`{red.bold You are about to remove this project from your machine.}` );
21 const confirmObj = await inquirer.prompt( prompt.getConfirmPrompt() );
22
23 if ( confirmObj.answer ) {
24 const slug = projObj.answer;
25 const projects = nconf.get( 'local_projects' );
26 const path = nconf.get( `local_projects:${slug}:path` );
27 const containers = nconf.get( `local_projects:${slug}:containers` );
28 const domainGroup = nconf.get( `local_projects:${slug}:hosts_domain_group` );
29
30 // Remove project's domain group from etc/hosts.
31 if ( domainGroup ) {
32 await hosts.remove( domainGroup );
33 }
34
35 // Delete all of this project's docker containers and associated volumes.
36 if ( containers ) {
37 containers.forEach( ( container ) => {
38 sh.exec( `docker rm --volumes --force ${container}` );
39 });
40 }
41
42 // Remove project directory from filesystem.
43 if ( path ) {
44 sh.rm( '-rf', path );
45 } else {
46 console.log( chalk`{red.bold Path not found in selected project's config.}` );
47 process.exit( 1 );
48 }
49
50 // Remove reference to project in config.
51 delete projects[slug];
52 config.updateLocalProjects( projects );
53 }
54}
55
56module.exports = remove;