|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +// Get the folder name from the command-line argument. |
| 5 | +const folderName = process.argv[2]; |
| 6 | + |
| 7 | +if (!folderName) { |
| 8 | + console.error('Please provide a folder name as a command-line argument.'); |
| 9 | + process.exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +// Function to create a unique project folder name. |
| 13 | +function getUniqueFolderName(baseFolderName) { |
| 14 | + let folderName = baseFolderName; |
| 15 | + let count = 2; |
| 16 | + |
| 17 | + while (fs.existsSync(folderName)) { |
| 18 | + folderName = `${baseFolderName}${count}`; |
| 19 | + count++; |
| 20 | + } |
| 21 | + |
| 22 | + return folderName; |
| 23 | +} |
| 24 | + |
| 25 | +// Create a unique project folder. |
| 26 | +const projectFolder = getUniqueFolderName(folderName); |
| 27 | + |
| 28 | +// Create frontend and backend folders. |
| 29 | +const frontendFolder = path.join(projectFolder, 'frontend'); |
| 30 | +const backendFolder = path.join(projectFolder, 'backend'); |
| 31 | + |
| 32 | +fs.mkdirSync(projectFolder); |
| 33 | +fs.mkdirSync(frontendFolder); |
| 34 | +fs.mkdirSync(backendFolder); |
| 35 | + |
| 36 | +// Create index.html content. |
| 37 | +const indexHtmlContent = ` |
| 38 | +<html> |
| 39 | + <head> |
| 40 | + <title>sample</title> |
| 41 | + <link rel="stylesheet" type="text/css" href="style.css"> |
| 42 | + <script src="script.js"></script> |
| 43 | + </head> |
| 44 | +
|
| 45 | + <body> |
| 46 | + <p>Your project has been set up successfully</p> |
| 47 | + </body> |
| 48 | +</html> |
| 49 | +`; |
| 50 | + |
| 51 | +// Create index.html file in the frontend folder. |
| 52 | +fs.writeFileSync(path.join(frontendFolder, 'index.html'), indexHtmlContent); |
| 53 | + |
| 54 | +// Create style.css content. |
| 55 | +const styleCssContent = `/* Your CSS styles go here */`; |
| 56 | + |
| 57 | +// Create style.css file in the frontend folder. |
| 58 | +fs.writeFileSync(path.join(frontendFolder, 'style.css'), styleCssContent); |
| 59 | + |
| 60 | +// Create script.js content. |
| 61 | +const scriptJsContent = `// Your JavaScript code goes here`; |
| 62 | + |
| 63 | +// Create script.js file in the frontend folder. |
| 64 | +fs.writeFileSync(path.join(frontendFolder, 'script.js'), scriptJsContent); |
| 65 | + |
| 66 | +// Update app.js content to serve frontend files correctly. |
| 67 | +const appJsContent = `const express = require('express'); |
| 68 | +const app = express(); |
| 69 | +const port = 3000; |
| 70 | +
|
| 71 | +app.use(express.static('frontend')); |
| 72 | +
|
| 73 | +app.listen(port, () => { |
| 74 | + console.log('The server is running at http://localhost:3000'); |
| 75 | +}); |
| 76 | +`; |
| 77 | + |
| 78 | +// Create app.js file in the backend folder. |
| 79 | +fs.writeFileSync(path.join(backendFolder, 'app.js'), appJsContent); |
| 80 | + |
| 81 | +// Log a success message. |
| 82 | +console.log(`Your sample project has been set up successfully as "${projectFolder}"`); |
0 commit comments