-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,106 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const { exec } = require('child_process'); | ||
const colors = require('colors'); | ||
|
||
// Check if 'index.js' already exists | ||
if (fs.existsSync('index.js')) { | ||
console.log('index.js already exists! Exiting...'); | ||
process.exit(1); | ||
} | ||
async function initProject(projectName, currentDir, isCurrentFolder) { | ||
let projectPath; | ||
|
||
if (isCurrentFolder) { | ||
// Use the current directory for files and node_modules | ||
projectPath = currentDir; | ||
console.log(colors.yellow(`Setting up project in the current directory: ${currentDir}`)); | ||
} else { | ||
// Create a new directory for the project | ||
projectPath = path.join(currentDir, projectName); | ||
try { | ||
if (!fs.existsSync(projectPath)) { | ||
console.log(colors.yellow(`Creating project directory: ${projectName}`)); | ||
await fs.mkdir(projectPath); | ||
} else { | ||
console.log(colors.red(`Directory ${projectName} already exists. Please choose a different name.`)); | ||
return; | ||
} | ||
} catch (error) { | ||
console.error(colors.red('Error creating project directory:', error.message)); | ||
return; | ||
} | ||
} | ||
|
||
// Basic Express starter code | ||
const expressStarterCode = ` | ||
// Create an index.js file | ||
const indexContent = ` | ||
const express = require('express'); | ||
const app = express(); | ||
const port = 3000; | ||
const PORT = process.env.PORT || 3000; | ||
app.get('/', (req, res) => { | ||
res.send('Hello, World!'); | ||
res.send('Hello, TurboXpress!'); | ||
}); | ||
app.listen(port, () => { | ||
console.log(\`Server running at http://localhost:\${port}/\`); | ||
app.listen(PORT, () => { | ||
console.log('Server is running on port ' + PORT); | ||
}); | ||
`; | ||
|
||
// Create index.js file | ||
fs.writeFile('index.js', expressStarterCode, (err) => { | ||
if (err) throw err; | ||
console.log('index.js has been created with basic Express code.'); | ||
|
||
// Install Express.js | ||
console.log('Installing Express...'); | ||
exec('npm install express', (err, stdout, stderr) => { | ||
if (err) { | ||
console.error(`Error installing Express: ${stderr}`); | ||
process.exit(1); | ||
try { | ||
// Write index.js to the appropriate folder | ||
await fs.writeFile(path.join(projectPath, 'index.js'), indexContent); | ||
console.log(colors.green(`index.js has been created successfully in ${projectPath}!`)); | ||
|
||
// Change to the project directory (or stay in the current directory) | ||
if (!isCurrentFolder) { | ||
process.chdir(projectPath); // Change the current working directory to the new project folder | ||
} | ||
|
||
console.log(colors.yellow('Initializing npm and installing Express...')); | ||
|
||
// Initialize npm and install Express | ||
await initNpm(); | ||
await installExpress(); | ||
|
||
console.log(colors.green('Project setup complete!')); | ||
} catch (error) { | ||
console.error(colors.red('Error setting up project:', error.message)); | ||
} | ||
console.log('Express has been installed successfully.'); | ||
}); | ||
}); | ||
} | ||
|
||
function initNpm() { | ||
return new Promise((resolve, reject) => { | ||
exec('npm init -y', (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(colors.red(`Error initializing npm: ${stderr}`)); | ||
reject(error); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
function installExpress() { | ||
return new Promise((resolve, reject) => { | ||
exec('npm install express', (error, stdout, stderr) => { | ||
if (error) { | ||
console.error(colors.red(`Error installing Express: ${stderr}`)); | ||
reject(error); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
const [,, command, projectName, currentFolderFlag] = process.argv; | ||
const currentDir = process.cwd(); | ||
|
||
// If the command is "init" and projectName is provided | ||
if (command === 'init' && projectName) { | ||
const isCurrentFolder = currentFolderFlag === '.'; | ||
|
||
// Pass the currentDir if using "." | ||
initProject(isCurrentFolder ? currentDir : projectName, currentDir, isCurrentFolder); | ||
} else { | ||
console.log(colors.red('Invalid command. Use "turbo init <project-name>" or "turbo init <project-name> ."')); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
function initial() { | ||
return 'TURBO!!' | ||
return 'TURBO!!!!' | ||
} | ||
|
||
const turbo = { | ||
|