npm package: @httpanimations/sji
Description: SJI (Simple JavaScript Imports) is a lightweight utility for dynamically importing single JavaScript modules or all modules from a directory in Node.js. It simplifies the process of managing and loading your application’s code.
Author: HttpAnimations
Latest version: 1.0.9

# SJI Documentation

SJI (Simple JavaScript Imports) is a lightweight utility designed to dynamically import JavaScript modules into your Node.js project. It helps slim down your main codebase by loading modules on-demand, which can be particularly useful for managing routes, database configurations, or other modularized components.

## Installation

You can install SJI using npm:

```bash
npm install @httpanimations/sji
```

## Features

- **Import a single JavaScript module**: Dynamically import and execute a single module by its file path to streamline your application.
- **Import all modules from a directory**: Automatically scan a directory and import all `.js` modules, which helps in managing routes, database configurations, or other parts of your app that can be modularized.
- **Error handling**: Provides error messages when a file or directory cannot be found.
- **Default export handling**: Automatically calls the default function export of the module, if available, immediately upon import.

## Usage

### Importing a Single Module

To import a single JavaScript module, use the `import` method by providing the module's file path. This is especially useful for importing routes, controllers, or database configuration files.

```javascript
const SJI = require('@httpanimations/sji');

async function loadModule() {
  const module = await SJI.import('./path/to/your/module.js');
  console.log(module);
}
```

#### Parameters:
- `modulePath` (string): The relative or absolute path to the module you want to import.

#### Returns:
- A promise that resolves to the module if the import is successful.

#### Example:

```javascript
// Importing a module and calling its default export if it's a function
const module = await SJI.import('./exampleModule.js');
```

### Importing All Modules from a Directory

To import all `.js` modules from a directory, use the `importAll` method with the directory path. This is useful for importing all route handlers, database connections, or other grouped files.

```javascript
const SJI = require('@httpanimations/sji');

async function loadModules() {
  await SJI.importAll('./path/to/directory');
}
```

#### Parameters:
- `dirPath` (string): The path to the directory containing the JavaScript modules.

#### Returns:
- A promise that resolves after all modules in the directory have been imported.

#### Example:

```javascript
// Importing all modules from a directory (e.g., routes or database configurations)
await SJI.importAll('./routes');
```

## Error Handling

If an error occurs while importing a module or a directory (e.g., if the path does not exist), the utility will catch the error and log it to the console.

### Error Logging

- If the module or directory does not exist, an error message will be logged indicating the problem.
- If there is an issue during the import process, the error message will be displayed with details.

Example of error log:
```
[SJI] Error importing module: ./path/to/invalid/module.js
Module not found: ./path/to/invalid/module.js
```

## Methods

### `import(modulePath)`

- **Description**: Imports a single JavaScript module dynamically by the given file path. This is particularly useful for importing individual components like routes or configuration files.
- **Arguments**: 
  - `modulePath` (string): Path to the module file (relative or absolute).
- **Returns**: A promise that resolves to the imported module, or logs an error if the module is not found.

### `importAll(dirPath)`

- **Description**: Imports all `.js` modules from a specified directory. This is ideal for modular applications where multiple related files (like routes, controllers, or database modules) need to be imported dynamically.
- **Arguments**:
  - `dirPath` (string): Path to the directory containing `.js` files.
- **Returns**: A promise that resolves after all modules have been imported, or logs an error if the directory is not found.

## Project EXP
Project that makes a database table.

```js
// index.js
const SJI = require('@httpanimations/sji'); // Imports SJI
const db = require('./backend/database'); // Imports the js file

app.set('views', __dirname + '/views');
app.use(express.static('public'));

// SQLite Database Setup
async function database() {
    await SJI.import('./backend/database.js');
}

database();
```

```js
//backend/database.js
const sqlite3 = require('sqlite3').verbose();

// SQLite Database Setup
const db = new sqlite3.Database('./reflective.db', (err) => {
  if (err) console.error('Failed to open database:', err.message);
  else console.log('Connected to SQLite database');
});

// Create table for users if it doesn't exist
db.run(`CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    discord_id TEXT UNIQUE,
    username TEXT,
    discriminator TEXT,
    avatar TEXT,
    email TEXT,
    login_count INTEGER DEFAULT 0,
    admin TEXT DEFAULT false,
    banned TEXT DEFAULT false
  )`);

module.exports = db;  // Export the db object
```

## License

SJI is open-source and licensed under the MIT License. Feel free to use, modify, and distribute it in your projects.