Skip to content

Commit

Permalink
Merge pull request #1 from hardope/beta
Browse files Browse the repository at this point in the history
feat: Initial Framework - auto logging
  • Loading branch information
hardope authored Oct 19, 2024
2 parents 7b784b4 + 1b299be commit f34b486
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 29 deletions.
82 changes: 77 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <project-name>
```

This command will:

Create a directory with the specified <project-name>.
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 <project-name> .
```
```

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.
35 changes: 18 additions & 17 deletions bin/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 <project-name>" or "turbo init <project-name> ."'));
console.log(colors.red('Invalid command. Use "turb-xpresss init <project-name>" or "turbo-xpress init <project-name> ."'));
}
2 changes: 1 addition & 1 deletion package-lock.json

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

27 changes: 21 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
module.exports = turbo_xpress;

0 comments on commit f34b486

Please sign in to comment.