Skip to content

Commit 7db8e0a

Browse files
terminal
1 parent 487a7d9 commit 7db8e0a

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

server/services/CommandExecutor.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const shell = require('shelljs');
22

33
const runCommand = (cmd) => {
44
return new Promise((resolve, reject) => {
5-
shell.exec(cmd, (code, stdout, stderr) => {
5+
shell.exec(cmd, { async: true }, (code, stdout, stderr) => {
66
if (code !== 0) {
77
reject(new Error(stderr));
88
} else {
@@ -12,5 +12,38 @@ const runCommand = (cmd) => {
1212
});
1313
};
1414

15+
const startProject = async (templateName) => {
16+
try {
17+
const template = commandsTemplates.find(t => t.template === templateName);
18+
if (!template) {
19+
throw new Error(`Template ${templateName} not found`);
20+
}
1521

16-
module.exports = runCommand;
22+
for (const command of template.commands) {
23+
console.log(`Running command: ${command}`);
24+
await runCommand(command);
25+
}
26+
27+
// Run the server and client commands in separate terminals for MERN
28+
if (templateName === 'mern') {
29+
console.log(`Starting server: ${template.run[0]}`);
30+
runCommand(template.run[0]);
31+
32+
console.log(`Starting client: ${template.run[1]}`);
33+
runCommand(template.run[1]);
34+
} else {
35+
for (const command of template.run) {
36+
console.log(`Running command: ${command}`);
37+
await runCommand(command);
38+
}
39+
}
40+
41+
} catch (error) {
42+
console.error(`Error starting project: ${error.message}`);
43+
}
44+
};
45+
46+
module.exports = {
47+
runCommand,
48+
startProject
49+
};

server/services/TemplateService.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ const commandsTemplates = [
99
"touch index.html && echo '<!DOCTYPE html>\\n<html lang=\"en\">\\n<head>\\n<meta charset=\"UTF-8\">\\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\\n<title>Document</title>\\n<link rel=\"stylesheet\" href=\"style.css\">\\n</head>\\n<body>\\n<script src=\"script.js\"></script>\\n</body>\\n</html>' > index.html",
1010
"touch script.js && echo 'document.addEventListener(\"DOMContentLoaded\", () => {\\n console.log(\"Hello, World!\");\\n});' > script.js",
1111
"touch style.css && echo 'body {\\n font-family: Arial, sans-serif;\\n margin: 0;\\n padding: 0;\\n}' > style.css"
12-
]
12+
],
13+
run: []
1314
},
1415
{
1516
template: "react",
1617
commands: [
1718
"npx create-react-app app"
19+
],
20+
run: [
21+
"cd app && npm start"
1822
]
1923
},
2024
{
2125
template: "node",
2226
commands: [
2327
"npm init -y",
2428
"touch index.js && echo 'const express = require(\"express\");\\nconst app = express();\\nconst PORT = process.env.PORT || 3000;\\napp.get(\"/\", (req, res) => {\\n res.send(\"Hello, World!\");\\n});\\napp.listen(PORT, () => {\\n console.log(`Server is running on port ${PORT}`);\\n});' > index.js"
29+
],
30+
run: [
31+
"npm i express && node index.js"
2532
]
2633
},
2734
{
@@ -30,11 +37,16 @@ const commandsTemplates = [
3037
"npx create-react-app client",
3138
"mkdir server",
3239
"sh -c 'cd server && npm init -y && touch index.js && echo \"const express = require(\\\"express\\\");\\nconst app = express();\\nconst PORT = process.env.PORT || 5000;\\napp.get(\\\"/\\\", (req, res) => {\\n res.send(\\\"Hello from the server!\\\");\\n});\\napp.listen(PORT, () => {\\n console.log(`Server is running on port ${PORT}`);\\n});\" > index.js'"
40+
],
41+
run: [
42+
"cd server && npm i express && npm start",
43+
"cd client && npm start"
3344
]
3445
}
3546
];
3647

3748

49+
3850
const createTemplate =async (templateName, projectPath) => {
3951
const template = commandsTemplates.find(t => t.template === templateName);
4052

0 commit comments

Comments
 (0)