1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs-extra');
|
5 | const chalk = require('chalk');
|
6 | const pathExists = require('path-exists');
|
7 | const npm = require('./npm');
|
8 | const router = require('./router');
|
9 |
|
10 | module.exports = (projectName, templateType, testDirPath) => {
|
11 | const project = path.resolve(projectName);
|
12 |
|
13 | if (!pathExists.sync(project)) {
|
14 | const setting = router(templateType);
|
15 | const currentPath = process.cwd();
|
16 | const projectPath = testDirPath ? testDirPath : path.resolve(currentPath, project);
|
17 |
|
18 | fs.mkdirsSync(projectPath);
|
19 | process.chdir(projectPath);
|
20 |
|
21 | console.log(chalk.cyan('Installing packages.'));
|
22 |
|
23 | if (process.env.NODE_ENV !== 'test') npm(projectName, project, setting.id);
|
24 |
|
25 | console.log(chalk.cyan('Installed from npmjs.'));
|
26 | console.log(chalk.cyan('Making the stage.'));
|
27 |
|
28 | if (process.env.NODE_ENV === 'test') debugMode(setting.templatePath, testDirPath);
|
29 | else if (process.env.NODE_ENV === 'development') debugMode(setting.templatePath);
|
30 | else productionMode(setting.templatePath);
|
31 |
|
32 | console.log();
|
33 | console.log(`$ cd ${projectName}`);
|
34 | console.log('$ npm start');
|
35 | console.log();
|
36 | console.log(chalk.green('Have a nice time :)'));
|
37 | }
|
38 | else {
|
39 | console.error(chalk.red(`Error: ${projectName} already exists ;(`));
|
40 | }
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function debugMode(templatePath, testDirPath) {
|
47 | const base = testDirPath ? testDirPath : '.';
|
48 |
|
49 | fs.copySync(path.join(__dirname, '..', 'templates', 'common'), base);
|
50 | fs.copySync(path.join(__dirname, '..', 'templates', templatePath), base);
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function productionMode(templatePath) {
|
57 | fs.copySync(path.resolve('node_modules', 'my-dish', 'templates', 'common'), '.');
|
58 | fs.copySync(path.resolve('node_modules', 'my-dish', 'templates', templatePath), '.');
|
59 | }
|