1 | const inquirer = require( 'inquirer' );
|
2 | const fs = require( 'fs' );
|
3 | const sh = require( 'shelljs' );
|
4 | const chalk = require( 'chalk' );
|
5 | const opn = require( 'opn' );
|
6 | const nconf = require( 'nconf' );
|
7 | const prompt = require( '../lib/prompt' );
|
8 | const config = require( '../lib/config' );
|
9 | const util = require( '../lib/util' );
|
10 | const docker = require( '../lib/docker' );
|
11 | const git = require( '../lib/git' );
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | async 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 |
|
28 | const ports = await util.generatePorts();
|
29 |
|
30 |
|
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 |
|
52 | const url = `http://${slug}.slab:${ports[0]}`;
|
53 |
|
54 |
|
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 |
|
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 |
|
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 |
|
87 | sh.mkdir( '-p', path );
|
88 |
|
89 |
|
90 | if ( fs.existsSync( path ) ) {
|
91 |
|
92 | await git.clone( projectRepo, path );
|
93 |
|
94 |
|
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 |
|
103 | if ( cmdBeforeContainersInit ) {
|
104 | util.execFrom( path, cmdBeforeContainersInit );
|
105 | }
|
106 |
|
107 |
|
108 | await docker.setup( slug, url, typeInfo );
|
109 |
|
110 |
|
111 | if ( files ) {
|
112 | files.forEach( ( file ) => {
|
113 | fs.writeFileSync( `${file.location}/${file.filename}`, file.contents );
|
114 | });
|
115 | }
|
116 |
|
117 |
|
118 | if ( cmdAfterContainersInit ) {
|
119 | await util.execFrom( path, cmdAfterContainersInit );
|
120 | }
|
121 |
|
122 |
|
123 | opn( url, { wait: false });
|
124 | }
|
125 |
|
126 | console.log( 'done.' );
|
127 | }
|
128 |
|
129 | module.exports = clone;
|