UNPKG

2.11 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 // rename .npmignore to .gitignore
33 if (process.env.NODE_ENV !== 'test') {
34 fs.renameSync(`${process.cwd()}/.npmignore`, `${process.cwd()}/.gitignore`);
35 }
36
37 console.log();
38 console.log(`$ cd ${projectName}`);
39 console.log('$ npm start');
40 console.log();
41 console.log(chalk.green('Have a nice time :)'));
42 }
43 else {
44 console.error(chalk.red(`Error: ${projectName} already exists ;(`));
45 }
46};
47
48/**
49 * @description copy the template for test or development
50 */
51function debugMode(templatePath, testDirPath) {
52 const base = testDirPath ? testDirPath : '.';
53
54 fs.copySync(path.join(__dirname, '..', 'templates', 'common'), base);
55 fs.copySync(path.join(__dirname, '..', 'templates', templatePath), base);
56}
57
58/**
59 * @description copy the template for production
60 */
61function productionMode(templatePath) {
62 fs.copySync(path.resolve('node_modules', 'my-dish', 'templates', 'common'), '.');
63 fs.copySync(path.resolve('node_modules', 'my-dish', 'templates', templatePath), '.');
64}