From 1b299be4f59ccb5e45321793a23268016243f735 Mon Sep 17 00:00:00 2001 From: hardope Date: Sun, 20 Oct 2024 00:33:08 +0100 Subject: [PATCH] feat: Initial Framework - auto logging --- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++--- bin/turbo.js | 35 ++++++++++---------- package-lock.json | 2 +- src/index.js | 27 ++++++++++++---- 4 files changed, 117 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index e39adfd..28670ce 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,84 @@ # turboXpress +`turboXpress` is a Framework & CLI tool designed to help you quickly create and scaffold Express-based projects. It is built on top of ExpressJs and includes built-in logging, static file serving, and JSON request handling by default, so you can focus on building your application. + +## Installation + +Install `turboXpress` globally using npm: + +```bash +npm install -g turbo-xpress +``` +## Usage +1. Initialize a New Express Project + +To create a new folder with your project name, install the required dependencies, and scaffold a basic turboXpress app: + ```bash -npm -g install turbo-xpress -# usage - command will create a folder with your project name install Express and nodemon -# and write a simple expressJs app turbo-xpress init +``` + +This command will: + + Create a directory with the specified . + Install turbo-xpress and nodemon as dependencies. + Automatically create all the necessary files and set up a basic turboXpress application with predefined routes, logging, static server setup, and JSON parsing. + +2. Setup a Project in the Current Folder + +To set up a new project in the current folder (without creating a new one), use this command: + +```bash -# Setup project in-folder (do not create new folder) turbo-xpress init . -``` \ No newline at end of file +``` + +This will: + + Install turbo-xpress and nodemon in the current directory. + Set up the basic structure of a turboXpress application inside the current folder. + +## Using Your Generated Project + +Once initialized, you can immediately start using the project as you would with a regular Express app. Since turbo-xpress is already imported, the project is ready to run: + +```javascript + +// app.js (auto-generated) + +const app = require('turbo-xpress')(); + +app.get('/', (req, res) => { + res.send('Hello, turboXpress!'); +}); + +const port = 3000; +app.listen(port, () => { + console.log(`Server running on http://localhost:${port}`); +}); +``` +Key Features + + Default Logger: Automatically logs request methods, URLs, status codes, and response times. + Static File Serving: Serves files from the /static directory by default. + JSON Handling: Automatically parses JSON payloads for incoming requests. + +## Project Structure + +After initialization, the project will have the following structure: + +``` + +project-name/ +├── node_modules/ +├── package.json +├── app.js +└── static/ + └── index.html (or other static files) +``` +## Features + + Fast Setup: Quickly scaffold your project with one command. + Pre-configured: Built-in logging, static file serving, and JSON parsing. + Flexible: Can be initialized in a new directory or the current working directory. \ No newline at end of file diff --git a/bin/turbo.js b/bin/turbo.js index 61fdce4..fbd8b78 100755 --- a/bin/turbo.js +++ b/bin/turbo.js @@ -29,14 +29,14 @@ async function initProject(projectName, currentDir, isCurrentFolder) { } } - // Create an index.js file + // Create an index.js file using turxpress const indexContent = ` -const express = require('express'); -const app = express(); +const turbo_xpress = require('turbo-xpress'); +const app = turbo_xpress(); const PORT = process.env.PORT || 3000; app.get('/', (req, res) => { - res.send('Hello, TurboXpress!'); + res.send('Hello, turbo-xpress!'); }); app.listen(PORT, () => { @@ -54,11 +54,11 @@ app.listen(PORT, () => { process.chdir(projectPath); // Change the current working directory to the new project folder } - console.log(colors.yellow('Initializing npm and installing Express...')); + console.log(colors.yellow('Initializing npm and installing turboxpress...')); - // Initialize npm and install Express + // Initialize npm and install turbo-xpress await initNpm(); - await installExpress(); + await installturbo_xpress(); // Modify package.json to add custom start scripts await updatePackageJson(); @@ -72,21 +72,22 @@ app.listen(PORT, () => { 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(); + if (error) { + console.error(colors.red(`Error initializing npm: ${stderr}`)); + reject(error); + return; + } + resolve(); }); }); } -function installExpress() { +// Function to install turboxpress using npm link and install nodemon +function installturbo_xpress() { return new Promise((resolve, reject) => { - exec('npm install express nodemon', (error, stdout, stderr) => { + exec('npm install turbo-xpress nodemon', (error, _stdout, stderr) => { if (error) { - console.error(colors.red(`Error installing Express: ${stderr}`)); + console.error(colors.red(`Error linking turbxpress or installing nodemon: ${stderr}`)); reject(error); return; } @@ -126,5 +127,5 @@ if (command === 'init' && projectName) { // Pass the currentDir if using "." initProject(isCurrentFolder ? currentDir : projectName, currentDir, isCurrentFolder); } else { - console.log(colors.red('Invalid command. Use "turbo init " or "turbo init ."')); + console.log(colors.red('Invalid command. Use "turb-xpresss init " or "turbo-xpress init ."')); } diff --git a/package-lock.json b/package-lock.json index 745135c..8d92a0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "fs-extra": "^11.2.0" }, "bin": { - "turboxpress": "bin/turbo.js" + "turbo-xpress": "bin/turbo.js" }, "engines": { "node": ">=18.0.0" diff --git a/src/index.js b/src/index.js index 7d7405b..f5a84f7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,24 @@ -function initial() { - return 'TURBO!!!!' -} +const express = require('express'); + +// The main function exported by turboXpress +const turbo_xpress = () => { + const app = express(); + + app.use((req, res, next) => { + const start = Date.now(); + res.on('finish', () => { + const duration = Date.now() - start; + const timestamp = new Date().toISOString(); + console.log(`[${timestamp}] -- ${req.method} -- ${req.url} -- ${res.statusCode} -- ${duration}ms`); + }); + next(); + }); + + app.use(express.json()); + + app.use('/static', express.static('static')); -const turbo = { - initial + return app; // Return the express apjp object } -module.exports = turbo \ No newline at end of file +module.exports = turbo_xpress; \ No newline at end of file