-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·63 lines (51 loc) · 1.55 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const cli = require('cac')()
const prompts = require('prompts');
async function main() {
try {
const projectName = await getProjectName();
const targetDir = path.join(process.cwd(), projectName);
fs.mkdirSync(targetDir);
const templateDir = path.join(__dirname, 'templates');
copyTemplateFiles(templateDir, targetDir, projectName);
console.log(`Boilerplate project created successfully in ${targetDir}`);
} catch (err) {
console.error('Error creating boilerplate:', err);
}
}
function copyTemplateFiles(source, destination, projectName) {
fs.readdirSync(source).forEach(file => {
const sourcePath = path.join(source, file);
const destPath = path.join(destination, file);
let content = fs.readFileSync(sourcePath, 'utf8');
content = content.replace(/{{ projectName }}/g, projectName);
fs.writeFileSync(destPath, content, 'utf8');
});
}
function getCliArguments() {
cli.option('--name <name>', 'Provide your name')
const parsed = cli.parse()
const projectName = parsed.args[0]
return projectName;
}
async function getUserPrompts() {
const response = await prompts({
type: 'text',
name: 'value',
message: 'What should be your project name?',
});
return response
}
async function getProjectName() {
const cliArgument = getCliArguments();
if (cliArgument) {
return cliArgument;
} else {
const response = await getUserPrompts();
return response.value;;
}
}
main();