UNPKG

1.94 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const fs = require('fs-extra');
5const chalk = require('chalk');
6const pathExists = require('path-exists');
7const npm = require('./npm');
8const router = require('./router');
9
10module.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 * @description copy the template for test or development
45 */
46function 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 * @description copy the template for production
55 */
56function 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}