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