UNPKG

3.93 kBJavaScriptView Raw
1const inquirer = require( 'inquirer' );
2const fs = require( 'fs' );
3const sh = require( 'shelljs' );
4const chalk = require( 'chalk' );
5const opn = require( 'opn' );
6const nconf = require( 'nconf' );
7const prompt = require( '../lib/prompt' );
8const config = require( '../lib/config' );
9const util = require( '../lib/util' );
10const docker = require( '../lib/docker' );
11const git = require( '../lib/git' );
12
13/**
14 * The main command.
15 * @param {Object} program Program options.
16 * @param {Object} dataProjects Remote project data.
17 * @param {Array} dataProjectTypes The types of projects available to create.
18 */
19async function clone( program, dataProjects, dataProjectTypes ) {
20 const optionProject = program.args[0].project;
21 const optionDirectory = program.args[0].directory;
22 const optionEnvironment = program.args[0].environment;
23
24 let envObj;
25 let cloneAgainObj;
26
27 // Get the available ports to use for this project.
28 const ports = await util.generatePorts();
29
30 // Get project.
31 let projectResponse;
32 if ( optionProject ) {
33 projectResponse = optionProject;
34 } else {
35 const projObj = await inquirer.prompt( prompt.getProjPrompt( dataProjects ) );
36 projectResponse = projObj.answer;
37 }
38 const project = await util.getProjectInfo( dataProjects, projectResponse );
39 const { name, type } = project;
40 let { slug } = project;
41 if ( util.localProjectExists( slug ) ) {
42 const newSlug = util.getNextSlugIncrement( slug );
43 cloneAgainObj = await inquirer.prompt( prompt.getAlreadyExistsPrompt( slug, newSlug ) );
44 if ( !cloneAgainObj.answer ) {
45 process.exit();
46 } else {
47 slug = newSlug;
48 }
49 }
50
51 // Get Url.
52 const url = `http://${slug}.slab:${ports[0]}`;
53
54 // Get directory.
55 let directoryResponse;
56 if ( optionDirectory ) {
57 if ( optionDirectory === 'default' ) {
58 directoryResponse = nconf.get( 'default_clone_path' );
59 } else {
60 directoryResponse = optionDirectory;
61 }
62 } else {
63 const dirObj = await inquirer.prompt( prompt.getDirPrompt() );
64 directoryResponse = dirObj.answer;
65 }
66 const path = `${directoryResponse}/${slug}`;
67
68 // Get type.
69 const typeInfoDirty = util.getProjectTypeInfo( type, dataProjectTypes );
70 const typeInfo = util.replacePlaceholders( typeInfoDirty, slug, path, ports, url );
71 const projectRepo = project.repo.dev;
72
73 const cmdBeforeContainersInit = typeInfo.cmd_before_containers_init;
74 const cmdAfterContainersInit = typeInfo.cmd_after_containers_init;
75 const { files } = typeInfo;
76
77 // Get environment.
78 let environmentResponse;
79 if ( optionEnvironment ) {
80 environmentResponse = optionEnvironment;
81 } else if ( util.projectHasEnvs( dataProjects, projectResponse ) ) {
82 envObj = await inquirer.prompt( prompt.getEnvPrompt( dataProjects, projectResponse ) );
83 environmentResponse = envObj.answer;
84 }
85
86 // Create directory to filesystem if it doesn't exist.
87 sh.mkdir( '-p', path );
88
89 // If project directory was able to be created.
90 if ( fs.existsSync( path ) ) {
91 // Clone project to filesystem.
92 await git.clone( projectRepo, path );
93
94 // Save project data to local slab config.
95 config.updateName( slug, name );
96 config.updateType( slug, type );
97 config.updatePath( slug, path );
98 config.updateUrl( slug, url );
99 config.updateLastUpdatedTime( slug );
100 config.updatePorts( slug, ports );
101
102 // Shell command to run before docker container is created.
103 if ( cmdBeforeContainersInit ) {
104 util.execFrom( path, cmdBeforeContainersInit );
105 }
106
107 // Setup docker container for selected project.
108 await docker.setup( slug, url, typeInfo );
109
110 // Create files.
111 if ( files ) {
112 files.forEach( ( file ) => {
113 fs.writeFileSync( `${file.location}/${file.filename}`, file.contents );
114 });
115 }
116
117 // Shell command to run after docker container is created.
118 if ( cmdAfterContainersInit ) {
119 await util.execFrom( path, cmdAfterContainersInit );
120 }
121
122 // Open site in default browser.
123 opn( url, { wait: false });
124 }
125
126 console.log( 'done.' );
127}
128
129module.exports = clone;