-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
62 lines (57 loc) · 1.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
const inquirer = require('inquirer');
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const { join } = require('path');
const currentDir = process.cwd();
const chalk = require('chalk');
const CHOICES = fs.readdirSync(`${__dirname}/boilerplates`);
const QUESTIONS = [
{
name: 'project-name',
type: 'input',
message: 'Project name:',
validate: function (input) {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else
return 'Project name may only include letters, numbers, underscores and hashes.';
},
},
{
name: 'project-type',
type: 'list',
message: 'Project Type:',
choices: CHOICES,
},
];
inquirer.prompt(QUESTIONS).then((answers) => {
const projectName = answers['project-name'];
const projectType = answers['project-type'];
let boilerplatePath = `${__dirname}/boilerplates/`;
if (projectType == 'javascript') {
boilerplatePath = `${__dirname}/boilerplates/javascript`;
} else if (projectType == 'typescript') {
boilerplatePath = `${__dirname}/boilerplates/typescript`;
}
fs.mkdirSync(`${currentDir}/${projectName}`);
fse.copySync(boilerplatePath, projectName);
createContent(projectName);
});
let createContent = (projectName) => {
let projectPath = path.join(currentDir, projectName);
let packageJson = path.join(projectPath, 'package.json');
let packageObj = fse.readJsonSync(packageJson);
packageObj.name = projectName;
fs.writeFileSync(packageJson, JSON.stringify(packageObj));
welcomeMsg(projectName);
};
let welcomeMsg = (projectName) => {
console.log(
chalk.yellow(
`Yay! we have created all the files to get started for your ${projectName}! 🚀`
)
);
console.log(chalk.yellow(`cd ${projectName}`));
console.log(chalk.blue(`Happy Coding!! ❤️`));
};