UNPKG

1.98 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' );
8
9/**
10 * The main command.
11 * @param {Object} program Program options.
12 * @param {Object}
13 */
14async function remove( program ) {
15 const optionProject = program.args[0].project;
16 const optionForce = program.args[0].force;
17
18 if ( !util.localProjectsExist() ) {
19 console.error( chalk`{red.bold No projects available.}` );
20 process.exit( 1 );
21 }
22
23 // Get project.
24 let projectResponse;
25 if ( optionProject ) {
26 projectResponse = optionProject;
27 } else {
28 const projObj = await inquirer.prompt( prompt.getAvailableProjectsPrompt() );
29 projectResponse = projObj.answer;
30 console.log( chalk`{red.bold You are about to remove this project from your machine.}` );
31 }
32 const slug = projectResponse;
33
34 let confirmResponse;
35 if ( optionForce ) {
36 confirmResponse = optionForce;
37 } else {
38 const confirmObj = await inquirer.prompt( prompt.getConfirmPrompt() );
39 confirmResponse = confirmObj.answer;
40 }
41
42 if ( confirmResponse ) {
43 const projects = nconf.get( 'local_projects' );
44 const path = nconf.get( `local_projects:${slug}:path` );
45 const containers = nconf.get( `local_projects:${slug}:containers` );
46
47 // Delete all of this project's docker containers and associated volumes.
48 if ( containers ) {
49 containers.forEach( ( container ) => {
50 sh.exec( `docker rm --volumes --force ${container}` );
51 });
52 }
53
54 // Remove project directory from filesystem.
55 if ( path ) {
56 sh.rm( '-rf', path );
57 } else {
58 console.log( chalk`{red.bold Path not found in selected project's config.}` );
59 process.exit( 1 );
60 }
61
62 // Remove reference to project in config.
63 delete projects[slug];
64 config.updateLocalProjects( projects );
65
66 console.log( chalk`{green Removed ${slug}.}` );
67 }
68}
69
70module.exports = remove;