-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
74 lines (64 loc) · 2.19 KB
/
generator.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
#!/usr/bin/env node
const inquirer = require('inquirer');
const fs = require('fs');
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const CURR_DIR = process.cwd();
function createDirectoryContents(templatePath, projectName, newProjectPath) {
const cleanedProjectName = projectName.replace('-', '_');
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
const stats = fs.lstatSync(origFilePath);
if (stats.isFile()) {
let contents = fs.readFileSync(origFilePath, 'utf8');
const replaceContent = contents.replace(/PROJECT_NAME/g, cleanedProjectName);
if (file === '.npmignore') file = '.gitignore';
if(file === 'PROJECT_NAME.csproj') file = `${cleanedProjectName}.csproj`;
const writePath = `${CURR_DIR}/${newProjectPath}/${file}`;
fs.writeFileSync(writePath, replaceContent, 'utf8');
} else if (stats.isDirectory()) {
fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`);
createDirectoryContents(`${templatePath}/${file}`, projectName, `${newProjectPath}/${file}`);
}
});
}
module.exports.generate = () => {
clear();
console.log(
chalk.yellow(
figlet.textSync('Onion generator', {
horizontalLayout: 'full'
})
)
);
const PROJECT_TYPES = [
'nodejs',
'angular2',
'graphql'
];
const QUESTIONS = [{
name: 'project-choice',
type: 'list',
message: 'What project template would you like to generate?',
choices: PROJECT_TYPES
},
{
name: 'project-name',
type: 'input',
message: 'Project name',
validate: (input) => {
if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
else return 'Project name may only include letters, numbers, underscores and hashes.';
}
}
];
inquirer.prompt(QUESTIONS).then(answers => {
const projectChoice = answers['project-choice'];
const projectName = answers['project-name'];
const templatePath = `${__dirname}/templates/${projectChoice}`;
fs.mkdirSync(`${CURR_DIR}/${projectName}`);
createDirectoryContents(templatePath, projectName, projectName);
});
};