UNPKG

3.32 kBJavaScriptView Raw
1const inquirer = require( 'inquirer' );
2const fs = require( 'fs' );
3const sh = require( 'shelljs' );
4const opn = require( 'opn' );
5const chalk = require( 'chalk' );
6const prompt = require( '../lib/prompt' );
7const config = require( '../lib/config' );
8const util = require( '../lib/util' );
9const sanitize = require( '../lib/sanitize' );
10const docker = require( '../lib/docker' );
11const git = require( '../lib/git' );
12const hosts = require( '../lib/hosts' );
13const app = require( '../lib/app' );
14
15/**
16 * The main command.
17 * @param {Array} dataProjectTypes The types of projects available to create.
18 */
19async function create( dataProjectTypes ) {
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 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 // Get Url.
40 const url = `http://${slug}.slab:${ports[0]}`;
41
42 // Get directory.
43 const dirObj = await inquirer.prompt( prompt.getDirPrompt() );
44 const path = `${dirObj.answer}/${slug}`;
45
46 // Get type.
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 // Create directory to filesystem if it doesn't exist.
57 sh.mkdir( '-p', path );
58
59 // If project directory was able to be created.
60 if ( fs.existsSync( path ) ) {
61 // Clone project to filesystem.
62 git.clone( projectRepo, path );
63
64 // Save project data to local slab config.
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 // Add domain group to hosts file.
73 const domainGroup = util.getProjectDomainGroup( url, slug, containers );
74 await hosts.add( domainGroup );
75 config.updateHostsDomainGroup( slug, domainGroup );
76
77 // Shell command to run before docker container is created.
78 if ( cmdBeforeContainersInit ) {
79 console.log( `${app.name}: Executing command before containers init...\n ${cmdBeforeContainersInit}` );
80 await util.execFrom( path, cmdBeforeContainersInit );
81 }
82
83 // Setup docker container for selected project.
84 await docker.setup( slug, url, typeInfo );
85
86 // Shell command to run after docker container is created.
87 if ( cmdAfterContainersInit ) {
88 console.log( `${app.name}: Executing command after containers init...\n ${cmdAfterContainersInit}` );
89 await util.execFrom( path, cmdAfterContainersInit );
90 }
91
92 // Open site in default browser.
93 opn( url, { wait: false });
94 }
95}
96
97module.exports = create;