-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-subproject.js
More file actions
93 lines (80 loc) · 2.86 KB
/
Copy pathcreate-subproject.js
File metadata and controls
93 lines (80 loc) · 2.86 KB
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
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const ncp = require("ncp").ncp;
const inquirer = require("inquirer");
const questions = [
{
type: "input",
name: "projectName",
message: "Enter the name of the new project:",
},
{
type: "list",
name: "projectType",
message: "Select the type of project:",
choices: ["vanilla-js", "react"],
},
];
inquirer
.prompt(questions)
.then((answers) => {
const { projectName, projectType } = answers;
const projectPath = path.resolve(process.cwd(), `src/${projectName}`);
const templatePath = path.resolve(__dirname, "templates", projectType);
console.log(
`Creating a new ${projectType} project named ${projectName}...`
);
// Function to create vanilla JS project
function createVanillaJsProject(name) {
execSync(`mkdir -p ${projectPath} && cd ${projectPath} && npm init -y`, {
stdio: "inherit",
});
// Copy template files
ncp(templatePath, projectPath, function (err) {
if (err) {
return console.error(err);
}
console.log("Template files copied successfully.");
});
}
// Function to create React project
function createReactProject(name) {
execSync(`mkdir -p ${projectPath}`, { stdio: "inherit" });
process.chdir(projectPath);
// Copy template files
ncp(templatePath, projectPath, function (err) {
if (err) {
return console.error(err);
}
console.log("Template files copied successfully.");
// Add development script to package.json
const packageJsonPath = path.join(projectPath, "package.json");
// Read and modify 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");
}
// Modify scripts in package.json
const packageJson = require(packageJsonPath);
packageJson.scripts = {
...packageJson.scripts,
start: "webpack serve --mode development --open",
};
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log("Development script added to package.json.");
});
}
if (projectType === "vanilla-js") {
createVanillaJsProject(projectName);
} else if (projectType === "react") {
createReactProject(projectName);
}
console.log(`Successfully created ${projectName}.`);
})
.catch((error) => {
console.error("Error during project creation:", error);
});