UNPKG

3.24 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 app = require( '../lib/app' );
13
14/**
15 * The main command.
16 * @param {Array} dataProjectTypes The types of projects available to create.
17 */
18async function create( dataProjectTypes ) {
19 let cloneAgainObj;
20
21 // Get the available ports to use for this project.
22 const ports = await util.generatePorts();
23
24 // Get Project.
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 // Get Url.
39 const url = `http://${slug}.slab:${ports[0]}`;
40
41 // Get directory.
42 const dirObj = await inquirer.prompt( prompt.getDirPrompt() );
43 const path = `${dirObj.answer}/${slug}`;
44
45 // Get type.
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 // Create directory to filesystem if it doesn't exist.
56 sh.mkdir( '-p', path );
57
58 // If project directory was able to be created.
59 if ( fs.existsSync( path ) ) {
60 // Clone project to filesystem.
61 await git.clone( projectRepo, path );
62
63 // Save project data to local slab config.
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 // Shell command to run before docker container is created.
72 if ( cmdBeforeContainersInit ) {
73 console.log( `${app.name}: Executing command before containers init...\n ${cmdBeforeContainersInit}` );
74 await util.execFrom( path, cmdBeforeContainersInit );
75 }
76
77 // Setup docker container for selected project.
78 await docker.setup( slug, url, typeInfo );
79
80 // Create files.
81 if ( files ) {
82 files.forEach( ( file ) => {
83 fs.writeFileSync( `${file.location}/${file.filename}`, file.contents );
84 });
85 }
86
87 // Shell command to run after docker container is created.
88 if ( cmdAfterContainersInit ) {
89 console.log( `${app.name}: Executing command after containers init...\n ${cmdAfterContainersInit}` );
90 await util.execFrom( path, cmdAfterContainersInit );
91 }
92
93 // Open site in default browser.
94 opn( url, { wait: false });
95 }
96}
97
98module.exports = create;