This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (96 loc) · 3.54 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
const inquirer = require('inquirer');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
function fill(count, character) {
let str = '';
for (let i = 0; i < count; i++) {
str += (character || ' ');
}
return str;
}
function printSuccess(projectName) {
console.log('✅ ' + chalk.green('Successfully generated project: ') + projectName);
console.log(` _____${fill(projectName.length, '_')}__`);
console.log(`| cd ${projectName} |`);
console.log(`| npm install${fill(projectName.length - 8)} |`);
console.log(`| npm run dev${fill(projectName.length - 8)} |`);
console.log(`|_____${fill(projectName.length, '_')}__|`);
}
const templates = fs.readdirSync(`${__dirname}/templates`);
const QUESTIONS = [
{
name: 'project-type',
type: 'list',
message: 'What project template would you like to generate?',
choices: templates
},
{
name: 'project-name',
type: 'input',
default: 'my-middle-connector',
message: 'What\'s the project\'s 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-author',
type: 'input',
message: 'Who is the project\'s author?'
}
];
inquirer
.prompt(QUESTIONS)
.then(answers => {
const projectName = answers['project-name'];
const projectType = answers['project-type'];
const projectAuthor = answers['project-author'];
generate({
type: projectType, name: projectName, author: projectAuthor
});
printSuccess(projectName);
});
function generate(config) {
const { type, name, author } = config;
const currentDir = process.cwd();
const templatePath = path.join(__dirname, 'templates', type);
if (fs.existsSync(path.join(currentDir, name))) {
console.log(chalk.red(`The directory ${name} already exists.`));
process.exit(1);
}
fs.mkdirSync(path.join(currentDir, name));
createDirectoryContents(templatePath, name);
const packageJsonPath = path.join(currentDir, name, 'package.json');
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);
packageJson.name = name;
packageJson.author = author || '';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
const envFilePath = path.join(currentDir, name, 'sample.env');
if (fs.existsSync(envFilePath)) {
fs.renameSync(envFilePath, path.join(currentDir, name, '.env'));
}
}
function createDirectoryContents(templatePath, newProjectPath) {
const filesToCreate = fs.readdirSync(templatePath);
const currentDir = process.cwd();
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
const contents = fs.readFileSync(origFilePath, 'utf8');
fs.writeFileSync(path.join(currentDir, newProjectPath, file), contents, 'utf8');
} else {
if (stats.isDirectory()) {
fs.mkdirSync(path.join(currentDir, newProjectPath, file));
createDirectoryContents(path.join(templatePath, file), path.join(newProjectPath, file));
}
}
});
}