Skip to content

Commit

Permalink
colors and file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
hardope committed Oct 17, 2024
1 parent 9f3dffa commit fd97341
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 32 deletions.
115 changes: 90 additions & 25 deletions bin/turbo.js
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> ."'));
}
22 changes: 19 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"main": "dist/index.js",
"bin": "./bin/turbo.js",
"scripts": {
"build": "mkdir -p dist && cp -r src/* dist/",
"build": "mkdir -p dist && cp -r src/* dist/",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Opeoluwa Adeyeri",
"license": "MIT",
"dependencies": {
"fs-extra": "^11.1.0"
"colors": "^1.4.0",
"fs-extra": "^11.2.0"
},
"devDependencies": {},
"engines": {
"node": ">=18.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function initial() {
return 'TURBO!!'
return 'TURBO!!!!'
}

const turbo = {
Expand Down

0 comments on commit fd97341

Please sign in to comment.