1 | const inquirer = require( 'inquirer' );
|
2 | const fs = require( 'fs' );
|
3 | const sh = require( 'shelljs' );
|
4 | const opn = require( 'opn' );
|
5 | const chalk = require( 'chalk' );
|
6 | const prompt = require( '../lib/prompt' );
|
7 | const config = require( '../lib/config' );
|
8 | const util = require( '../lib/util' );
|
9 | const sanitize = require( '../lib/sanitize' );
|
10 | const docker = require( '../lib/docker' );
|
11 | const git = require( '../lib/git' );
|
12 | const hosts = require( '../lib/hosts' );
|
13 | const app = require( '../lib/app' );
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | async function create( dataProjectTypes ) {
|
20 | let cloneAgainObj;
|
21 |
|
22 |
|
23 | const ports = await util.generatePorts();
|
24 |
|
25 |
|
26 | const nameObj = await inquirer.prompt( prompt.getNamePrompt() );
|
27 | const name = util.trimWhitespace( nameObj.answer );
|
28 | let slug = sanitize.projectName( name );
|
29 | if ( util.localProjectExists( slug ) ) {
|
30 | const newSlug = util.getNextSlugIncrement( slug );
|
31 | cloneAgainObj = await inquirer.prompt( prompt.getAlreadyExistsPrompt( slug, newSlug ) );
|
32 | if ( !cloneAgainObj.answer ) {
|
33 | process.exit();
|
34 | } else {
|
35 | slug = newSlug;
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 | const url = `http://${slug}.slab:${ports[0]}`;
|
41 |
|
42 |
|
43 | const dirObj = await inquirer.prompt( prompt.getDirPrompt() );
|
44 | const path = `${dirObj.answer}/${slug}`;
|
45 |
|
46 |
|
47 | const typeObj = await inquirer.prompt( prompt.getTypePrompt( dataProjectTypes ) );
|
48 | const type = typeObj.answer;
|
49 | const typeInfoDirty = util.getProjectTypeInfo( type, dataProjectTypes );
|
50 | const typeInfo = util.replacePlaceholders( typeInfoDirty, slug, path, ports, url );
|
51 | const projectRepo = typeInfo.project_repo;
|
52 | const cmdBeforeContainersInit = typeInfo.cmd_before_containers_init;
|
53 | const cmdAfterContainersInit = typeInfo.cmd_after_containers_init;
|
54 | const { containers } = typeInfo;
|
55 |
|
56 |
|
57 | sh.mkdir( '-p', path );
|
58 |
|
59 |
|
60 | if ( fs.existsSync( path ) ) {
|
61 |
|
62 | git.clone( projectRepo, path );
|
63 |
|
64 |
|
65 | config.updateName( slug, name );
|
66 | config.updateType( slug, type );
|
67 | config.updatePath( slug, path );
|
68 | config.updateUrl( slug, url );
|
69 | config.updateLastUpdatedTime( slug );
|
70 | config.updatePorts( slug, ports );
|
71 |
|
72 |
|
73 | const domainGroup = util.getProjectDomainGroup( url, slug, containers );
|
74 | await hosts.add( domainGroup );
|
75 | config.updateHostsDomainGroup( slug, domainGroup );
|
76 |
|
77 |
|
78 | if ( cmdBeforeContainersInit ) {
|
79 | console.log( `${app.name}: Executing command before containers init...\n ${cmdBeforeContainersInit}` );
|
80 | await util.execFrom( path, cmdBeforeContainersInit );
|
81 | }
|
82 |
|
83 |
|
84 | await docker.setup( slug, url, typeInfo );
|
85 |
|
86 |
|
87 | if ( cmdAfterContainersInit ) {
|
88 | console.log( `${app.name}: Executing command after containers init...\n ${cmdAfterContainersInit}` );
|
89 | await util.execFrom( path, cmdAfterContainersInit );
|
90 | }
|
91 |
|
92 |
|
93 | opn( url, { wait: false });
|
94 | }
|
95 | }
|
96 |
|
97 | module.exports = create;
|