-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·63 lines (56 loc) · 1.97 KB
/
cli.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
#!/usr/bin/env node
const { exec } = require("shelljs");
const inquirer = require("inquirer");
const path = require("path");
const fs = require("fs");
const templatesDir = path.join(__dirname, "templates");
// Function to prompt user for project type
function promptForProjectType() {
return inquirer.prompt([
{
type: "list",
name: "template",
message: "Choose a project template:",
choices: fs.readdirSync(templatesDir),
},
{
type: "input",
name: "projectName",
message: "Enter project name:",
validate: function (input) {
// Validate if directory with the same name already exists
const projectPath = path.join(process.cwd(), input);
if (fs.existsSync(projectPath)) {
return "Directory already exists. Please choose a different project name.";
}
return true;
},
},
]);
}
// Function to copy template directory to destination
function copyTemplate(templateName, projectName) {
const templatePath = path.join(templatesDir, templateName);
const destinationPath = path.join(process.cwd(), projectName);
exec(`cp -r ${templatePath} ${destinationPath}`);
// Read and modify package.json
const packageJsonPath = path.join(destinationPath, "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJsonData = JSON.parse(packageJsonContent);
packageJsonData.name = projectName; // Set project name as package name
const modifiedPackageJson = JSON.stringify(packageJsonData, null, 2);
fs.writeFileSync(packageJsonPath, modifiedPackageJson, "utf8");
}
}
// Main function to start CLI
async function main() {
try {
const { template, projectName } = await promptForProjectType();
copyTemplate(template, projectName);
console.log(`Project '${projectName}' created successfully.`);
} catch (error) {
console.error("Error creating project:", error);
}
}
main();