-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·243 lines (228 loc) · 7.14 KB
/
index.js
File metadata and controls
executable file
·243 lines (228 loc) · 7.14 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env node
import minimist from "minimist";
import { createProjectDirectory, generateBoilerplateCode } from "./commands.js";
import { createRepository } from "./github.js";
import inquirer from "inquirer";
import { loadConfig, saveConfig } from "./config.js";
import path from "path";
import { spawn } from "child_process";
import { fileURLToPath } from "url";
// utility function: executes a given `command` in the terminal using `spawn`
export function execCommand(command) {
// returns a new Promise that will resolve with the command's output if it succeeds or reject with an error if it fails
return new Promise((resolve, reject) => {
// inside the Promise, it uses the `spawn` function from the `child_process` module to create a new child process and execute the command
const child = spawn(command, { shell: true });
let output = "";
// it attaches event listeners to the child process:
// this listens for data on the standard --output-- stream and appends it to the `output` variable
child.stdout.on("data", (data) => {
output += data.toString();
});
// this listens for data on the standard --error-- stream and appends it to the `output` variable
child.stderr.on("data", (data) => {
output += data.toString();
});
// this listens for any errors that occur during the execution of the command and rejects the Promise with the error.
child.on("error", (error) => {
reject(error);
});
// this listen for the child process to close and checks the exit code
child.on("close", (code) => {
// code 0 indicates success, so any other code should reject the Promise with an error message that includes the exit code and the captured output
if (code !== 0) {
reject(new Error(`Command failed with exit code ${code}: ${output}`));
} else {
// the code was 0 therefore indicating success, resolves the Promise with the trimmed output
resolve(output.trim());
}
});
});
}
function runCLI(command, args) {
const config = loadConfig();
if (command === "new") {
inquirer
.prompt([
{
type: "input",
name: "projectName",
message: "Enter the project name:",
validate: (input) => {
if (input.trim() === "") {
return "Please enter a valid project name.";
}
return true;
},
},
{
type: "confirm",
name: "useTypeScript",
message: "Do you want to use TypeScript?",
default: true,
},
{
type: "confirm",
name: "useTailwind",
message: "Do you want to use Tailwind CSS for styling?",
default: true,
},
{
type: "confirm",
name: "useFramerMotion",
message: "Do you want to use Framer Motion for animations?",
default: true,
},
{
type: "confirm",
name: "useReactRouter",
message: "Do you want to use React Router for routing?",
default: true,
},
])
.then(async (answers) => {
const {
projectName,
useTypeScript,
useTailwind,
useFramerMotion,
useReactRouter,
} = answers;
const options = {
useTypeScript,
useTailwind,
useFramerMotion,
useReactRouter,
};
await createProjectDirectory(projectName, "react", options);
})
.catch((error) => {
console.error("Error:", error);
process.exit(1);
});
} else if (command === "generate") {
inquirer
.prompt([
{
type: "list",
name: "fileType",
message: "Select what type of file you want to generate:",
choices: ["component", "service"],
},
{
type: "input",
name: "fileName",
message: "Enter the file name:",
validate: (input) => {
if (input.trim() === "") {
return "Please enter a valid file name.";
}
return true;
},
},
])
.then((answers) => {
const { fileType, fileName } = answers;
generateBoilerplateCode(fileType, fileName);
})
.catch((error) => {
console.error(`Error:, ${error}`);
process.exit(1);
});
} else if (command === "repo") {
inquirer
.prompt([
{
type: "input",
name: "repoName",
message: "Enter the repository name:",
validate: (input) => {
if (input.trim() === "") {
return "Please enter a valid repository name.";
}
return true;
},
},
{
type: "input",
name: "repoDescription",
message: "Enter the repository description (optional):",
},
{
type: "confirm",
name: "isPrivate",
message: "Do you want to make the repository private?",
default: false,
},
{
type: "input",
name: "authToken",
message:
"Enter your GitHub personal access token (optional if you've already configured it):",
default: config.authToken,
validate: (input) => {
if (input.trim() === "") {
return "Please enter a valid GitHub personal access token.";
}
return true;
},
},
])
.then(async (answers) => {
const { repoName, repoDescription, isPrivate, authToken } = answers;
try {
const cloneUrl = await createRepository(
repoName,
repoDescription,
isPrivate,
authToken
);
const repoPath = path.join(process.cwd(), repoName);
await execCommand(`git clone ${cloneUrl} ${repoPath}`);
console.log(`Cloned repository to ${repoPath}`);
console.log(`Repository initialized with basic files`);
} catch (error) {
console.error(`Error creating repository: ${error}`);
process.exit(1);
}
});
} else if (command === "config") {
inquirer
.prompt([
{
type: "list",
name: "projectType",
message: "Select a default project type:",
choices: ["react"],
default: config.defaultProjectType,
},
{
type: "input",
name: "authToken",
message: "Enter your Github personal access token (optional):",
},
])
.then((answers) => {
const { defaultProjectType, authToken } = answers;
if (defaultProjectType) {
config.defaultProjectType = defaultProjectType;
}
if (authToken) {
config.authToken = authToken;
}
saveConfig(config);
console.log(`Configuration saved successfully.`);
});
} else {
console.log(`Unknown command`);
process.exit(1);
}
}
const args = minimist(process.argv.slice(2));
const command = args._[0];
if (command) {
runCLI(command, args);
} else {
console.log("No command provided.");
process.exit(1);
}