-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (77 loc) · 2.64 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
#!/usr/bin/env node
import inquirer from 'inquirer';
import * as fs from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
// template dir
const __dirname = dirname(fileURLToPath(import.meta.url));
// curr dir
const CURR_DIR = process.cwd();
const CHOICES = fs.readdirSync(`${__dirname}/templates`);
const QUESTIONS = [
{
name: 'project-choice',
type: 'list',
message: 'What project template would you like to generate?',
choices: CHOICES,
},
{
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.';
},
},
];
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}`);
createDirContent(templatePath, projectName);
console.log('Installing dependencies...');
const npmInstall = runCommand(`cd ${projectName} && npm i`);
if (!npmInstall) process.exit(1);
console.log('Setting up local git...');
const setupGit = runCommand(
`cd ${projectName} && git init && git add . && git commit -m 'initial d'`
);
if (!setupGit) process.exit(1);
console.log(`----------------------`);
console.log(`ALL DONE!! ୧༼ಠ益ಠ༽୨`);
console.log(`"cd ${projectName}" and "npm run dev" to get started!`);
});
// -r function for copy all content
function createDirContent(templatePath, newProjectPath) {
const filesToCreate = fs.readdirSync(templatePath);
filesToCreate.forEach((file) => {
const origFilePath = `${templatePath}/${file}`;
const fileType = fs.statSync(origFilePath);
if (fileType.isFile()) {
const content = fs.readFileSync(origFilePath, 'utf8');
// .gitignore issue
if (file === 'gitignore.temp') file = '.gitignore';
const writePath = `${CURR_DIR}/${newProjectPath}/${file}`;
fs.writeFileSync(writePath, content, 'utf8');
console.log(`Creating ${writePath}`);
} else if (fileType.isDirectory()) {
fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`);
// recursive call
createDirContent(`${templatePath}/${file}`, `${newProjectPath}/${file}`);
}
});
}
// run command in cli
const runCommand = (command) => {
try {
execSync(`${command}`, { stdio: 'inherit' });
} catch (err) {
console.error(`Failed to execute ${command}`, err);
return false;
}
return true;
};