-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·87 lines (80 loc) · 2.88 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /usr/bin/env node
const inquirer = require("inquirer");
const fs = require("fs");
const CHOICES = fs.readdirSync(`${__dirname}/templates`);
const CURR_DIR = process.cwd();
const QUESTIONS = [
{
name: "project-name",
type: "input",
message: "Enter 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: "databases",
type: "checkbox",
message: "Select required databases:",
choices: ['Mongoose', 'Redis', 'MySQL'],
}
];
inquirer.prompt(QUESTIONS).then(answers => {
console.log(answers);
const projectName = answers["project-name"];
const databases = answers["databases"];
const templatePath = `${__dirname}/templates/demo-app`;
const dbTemplatePath = `${__dirname}/templates/dbconfig/`
fs.mkdirSync(`${CURR_DIR}/${projectName}`);
createDirectoryContents(templatePath, projectName);
createDatabasesConfig(dbTemplatePath, databases, `${CURR_DIR}/${projectName}/src/config/`);
});
function createDirectoryContents(templatePath, newProjectPath) {
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
// get stats about the current file
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
const contents = fs.readFileSync(origFilePath, "utf8");
// rename fallback for npm ignore.
if (file === '.npmignore') file = '.gitignore';
const writePath = `${CURR_DIR}/${newProjectPath}/${file}`;
fs.writeFileSync(writePath, contents, "utf8");
} else if (stats.isDirectory()) {
fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`);
// recursive call
createDirectoryContents(
`${templatePath}/${file}`,
`${newProjectPath}/${file}`
);
}
});
}
function createDatabasesConfig(templatePath, databases, projectDbPath) {
databases.forEach(database => {
let fileName = null;
switch (database) {
case 'Mongoose':
fileName = `mongoose.js`;
break;
case 'Redis':
fileName = `redis.js`;
break;
case 'MySQL':
fileName = `mysql.js`;
break;
default: break;
}
if (fileName) {
const filePath = `${templatePath}${fileName}`
const stats = fs.statSync(filePath);
if (stats.isFile()) {
const contents = fs.readFileSync(filePath, "utf8");
fs.writeFileSync(`${projectDbPath}${fileName}`, contents, "utf8");
}
}
});
}