-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·88 lines (74 loc) · 2.26 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const cli = require('cac')()
const prompts = require('prompts');
async function main() {
try {
const projectDetails = await getProjectDetails();
console.log("[details]1", projectDetails)
const { name, domain, clientId } = projectDetails;
const targetDir = path.join(process.cwd(), projectDetails.name);
fs.mkdirSync(targetDir);
const templateDir = path.join(__dirname, 'templates');
copyTemplateFiles(templateDir, targetDir, name, domain, clientId);
console.log(`Boilerplate project created successfully in ${targetDir}`);
} catch (err) {
console.error('Error creating boilerplate:', err);
}
}
function copyTemplateFiles(source, destination, projectName, domain, clientId) {
// console.log("[details]2", projectName, domain, clientId)
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true });
}
fs.readdirSync(source).forEach(file => {
const sourcePath = path.join(source, file);
const destPath = path.join(destination, file);
if (fs.statSync(sourcePath).isDirectory()) {
// Recursively handle directories
copyTemplateFiles(sourcePath, destPath, projectName, domain, clientId);
} else {
// Handle file copying and replacement
let content = fs.readFileSync(sourcePath, 'utf8');
content = content.replace(/{DOMAIN}/g, domain);
content = content.replace(/{CLIENT_ID}/g, clientId);
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: 'name',
message: 'What should be your project name?',
},
{
type: 'text',
name: 'domain',
message: 'What is your domain name?',
},
{
type: 'text',
name: 'clientId',
message: 'What is your client name?',
}]
);
return response
}
async function getProjectDetails() {
const cliArgument = getCliArguments();
if (cliArgument) {
return cliArgument;
} else {
const response = await getUserPrompts();
return response;
}
}
main();