UNPKG

3.24 kBJavaScriptView Raw
1const inquirer = require( 'inquirer' );
2const fs = require( 'fs' );
3const sh = require( 'shelljs' );
4const opn = require( 'opn' );
5const prompt = require( '../lib/prompt' );
6const config = require( '../lib/config' );
7const util = require( '../lib/util' );
8const docker = require( '../lib/docker' );
9const git = require( '../lib/git' );
10const hosts = require( '../lib/hosts' );
11const app = require( '../lib/app' );
12
13/**
14 * The main command.
15 * @param {Object} dataProjects Remote project data.
16 * @param {Array} dataProjectTypes The types of projects available to create.
17 */
18async function clone( dataProjects, dataProjectTypes ) {
19 let envObj;
20 let cloneAgainObj;
21
22 // Get the available ports to use for this project.
23 const ports = await util.generatePorts();
24
25 // Get project.
26 const projObj = await inquirer.prompt( prompt.getProjPrompt( dataProjects ) );
27 const project = await util.getProjectInfo( dataProjects, projObj.answer );
28 const { name, type } = project;
29 let { slug } = project;
30 if ( util.localProjectExists( slug ) ) {
31 const newSlug = util.getNextSlugIncrement( slug );
32 cloneAgainObj = await inquirer.prompt( prompt.getAlreadyExistsPrompt( slug, newSlug ) );
33 if ( !cloneAgainObj.answer ) {
34 process.exit();
35 } else {
36 slug = newSlug;
37 }
38 }
39
40 // Get Url.
41 const url = `http://${slug}.slab:${ports[0]}`;
42
43 // Get directory.
44 const dirObj = await inquirer.prompt( prompt.getDirPrompt() );
45 const path = `${dirObj.answer}/${slug}`;
46
47 // Get type.
48 const typeInfoDirty = util.getProjectTypeInfo( type, dataProjectTypes );
49 const typeInfo = util.replacePlaceholders( typeInfoDirty, slug, path, ports, url );
50 const projectRepo = typeInfo.project_repo;
51 const cmdBeforeContainersInit = typeInfo.cmd_before_containers_init;
52 const cmdAfterContainersInit = typeInfo.cmd_after_containers_init;
53 const { containers } = typeInfo;
54
55 // Get environment.
56 if ( util.projectHasEnvs( dataProjects, projObj.answer ) ) {
57 envObj = await inquirer.prompt( prompt.getEnvPrompt( dataProjects, projObj.answer ) );
58 }
59
60 // Create directory to filesystem if it doesn't exist.
61 sh.mkdir( '-p', path );
62
63 // If project directory was able to be created.
64 if ( fs.existsSync( path ) ) {
65 // Clone project to filesystem.
66 git.clone( projectRepo, path );
67
68 // Save project data to local slab config.
69 config.updateName( slug, name );
70 config.updateType( slug, type );
71 config.updatePath( slug, path );
72 config.updateUrl( slug, url );
73 config.updateLastUpdatedTime( slug );
74 config.updatePorts( slug, ports );
75
76 // Add domain group to hosts file.
77 const domainGroup = util.getProjectDomainGroup( url, slug, containers );
78 await hosts.add( domainGroup );
79 config.updateHostsDomainGroup( slug, domainGroup );
80
81 // Shell command to run before docker container is created.
82 if ( cmdBeforeContainersInit ) {
83 util.execFrom( path, cmdBeforeContainersInit );
84 }
85
86 // Setup docker container for selected project.
87 await docker.setup( slug, url, typeInfo );
88
89 // Shell command to run after docker container is created.
90 if ( cmdAfterContainersInit ) {
91 await util.execFrom( path, cmdAfterContainersInit );
92 }
93
94 // Open site in default browser.
95 opn( url, { wait: false });
96 }
97
98 console.log( 'done.' );
99}
100
101module.exports = clone;