Skip to content

Commit

Permalink
Feat cli (#16)
Browse files Browse the repository at this point in the history
* fix: import packages as default import

* fix: import packages as default import

* fix: change main execution file

* feat: add basics for create components

* chore(app): upgrade nodemon from ^3.1.0 to ^3.1.4

* style: remove unecess ts config files

* chore(ci): update docker ignore file

* refactor(app): put auth functions together

* feat: ignore `.DS_Store` from dynamic component import

* chore: make framework to use AMD and cli to use commonjs

* refactor: show welcome message on root

* refactor: show welcome message on root

* feat: add create package manager

* feat: add create package manager
  • Loading branch information
acellam authored Aug 18, 2024
1 parent 59880c2 commit d8c224e
Show file tree
Hide file tree
Showing 151 changed files with 107,167 additions and 3,215 deletions.
2 changes: 0 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ build/
/build
.dockerignore
Dockerfile
Dockerfile.prod
Dockerfile.dev
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#PRODUCTION
RDBMS_DATABASE_URI="mysql://DATBASE_USER:DATABASE_PASSWORD@DATABASE_HOST:DATABASE_PORT/DATABASE_DB"
NOSQL_DATABASE_URI="mongodb://DATABASE_HOST:DATABASE_PORT/DATABASE_DB"
NOSQL_DATABASE_ADAPTER="mongodb"
API_BASE='/v1/'
JWT_SECRET='fractal-dev'
DEVELOPMENT_JWT_SECRET='fractal-dev'
ENVIRONMENT='development'
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
packages
12 changes: 0 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,5 @@ module.exports = function(grunt) {
["checkDependencies", "clean:build", "webpack:development"]
);

grunt.registerTask(
"sandbox",
"Compiles all the assets and copies the files to the dist directory. Minified without source mapping",
["checkDependencies", "clean:build", "webpack:sandbox"]
);

grunt.registerTask(
"staging",
"Compiles all the assets and copies the files to the dist directory. Minified without source mapping",
["checkDependencies", "clean:build", "webpack:staging"]
);

grunt.registerTask("build", ["clean build"]);
};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ A component based framework for NodeJs Applications
We are creating a structure for developing large scalable, maitanable Nodejs applications. We mash up ideas from component based architecture, MVC, Entity Framework and Repository Pattern. We want different teams to focus on creating their domain apps in the components with ease.

## ⚙️ How to run the project
Prerequisite: Install git, node package manager, webpack CLI, grunt CLI. This framework also uses fractalerp [Active Record Js](https://github.com/fractalerp/fractal-js) for defining models.
Prerequisite: Install git, node package manager, webpack CLI, grunt CLI. This framework also uses fractalerp [Active Record Js](https://github.com/fractalerp/active-record-js) for defining models.

1. Create the following environment variables in your node project.
```env
Expand Down
46 changes: 24 additions & 22 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import * as http from "http";
import * as fs from "fs";
import * as express from "express";
import * as bodyParser from "body-parser";
import * as dotenv from "dotenv";
import * as morgan from "morgan";
import http from "http";
import fs from "fs";
import express from "express";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import morgan from "morgan";
import { createClient } from "redis";
import RedisStore from "connect-redis";
import * as session from "express-session";
import * as cors from "cors";
import * as passport from "passport";
import * as appRoot from "app-root-path";
import session from "express-session";
import cors from "cors";
import passport from "passport";
import appRoot from "app-root-path";
import helmet from "helmet";
import * as useragent from "useragent";
import useragent from "useragent";
import { StatusCodes } from "http-status-codes";
import { ExtractJwt, Strategy, StrategyOptionsWithRequest } from "passport-jwt";
import fractaLog, { fractalLogger } from "./config/logger";
import { csrfHandler } from "./middleware/csrf.middleware";
import { Environments } from "./utils/constants";
import { getJWT } from "./utils/helpers";
import { getJWT } from "./utils/auth";
import { FractalRouter } from "./routes/fractal_router";

export class FractalJs {
Expand Down Expand Up @@ -184,19 +184,21 @@ export class FractalJs {

private loadComponents = async () => {
// Define the directory containing classes
const excludeDirs = [".DS_Store"];
const classesDir = `${appRoot}/components`;

// Read all files in the classes directory
for (const component of fs.readdirSync(classesDir)) {

import(`./components/${component}`).then(module => {
// Assuming each file exports a single class
const className = Object.keys(module)[0];
const importedClass = module[className];
new importedClass(this);
}).catch(err => {
fractalLogger.error(`Error importing component ${component}: ${err}`);
});
for (const componentName of fs.readdirSync(classesDir)) {
if (!excludeDirs.includes(componentName)) {
import(`./components/${componentName}`).then(module => {
// Assuming each file exports a single class
const className = Object.keys(module)[0];
const importedClass = module[className];
new importedClass(this);
}).catch((err: Error) => {
fractalLogger.error(`Error importing component ${componentName}: ${err.message}`);
});
}
}
};

Expand Down
29 changes: 29 additions & 0 deletions cli/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint no-console: 0 */
import fs from "fs";
import path from "path";

export class App {
private name: string;

constructor(name: string) {
this.name = name;
}

public create = () => {
console.log("Creating fractal-js application...");

const basComponentPath = path.resolve(`${this.name}`);
// create component directories
if (!fs.existsSync(basComponentPath)) {
this.createDirs(basComponentPath);
// create component source files

} else {
console.log("The folder already exists");
}
};

private createDirs = async (basComponentPath: string) => {
await fs.mkdirSync(`${basComponentPath}`);
};
}
45 changes: 45 additions & 0 deletions cli/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint no-console: 0 */
import fs from "fs";
import path from "path";

export class Component {
private name: string;

constructor(name: string) {
this.name = name;
}

public create = () => {
console.log("Creating component...");

const basComponentPath = path.resolve(`components/${this.name}`);
// create component directories
if (!fs.existsSync(basComponentPath)) {
this.createDirs(basComponentPath);
// create component source files

} else {
console.log("The component already exists");
}
};

private createDirs = async (basComponentPath: string) => {
await fs.mkdirSync(`${basComponentPath}`);
await fs.mkdirSync(`${basComponentPath}/controllers`);
await fs.mkdirSync(`${basComponentPath}/adapters`);
await fs.mkdirSync(`${basComponentPath}/jobs`);
await fs.mkdirSync(`${basComponentPath}/logic`);
await fs.mkdirSync(`${basComponentPath}/middleware`);
await fs.mkdirSync(`${basComponentPath}/models`);
await fs.mkdirSync(`${basComponentPath}/notifications`);
await fs.mkdirSync(`${basComponentPath}/policies`);
await fs.mkdirSync(`${basComponentPath}/public`);
await fs.mkdirSync(`${basComponentPath}/public/dtos`);
await fs.mkdirSync(`${basComponentPath}/public/entities`);
await fs.mkdirSync(`${basComponentPath}/public/mappers`);
await fs.mkdirSync(`${basComponentPath}/public/repositories`);
await fs.mkdirSync(`${basComponentPath}/routes`);
await fs.mkdirSync(`${basComponentPath}/tests`);
await fs.mkdirSync(`${basComponentPath}/views`);
};
}
34 changes: 34 additions & 0 deletions cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint no-console: 0 */
import { Command } from "commander";
import * as figlet from "figlet";
import { version, description } from "../package.json";
import { Component } from "./component";
import { App } from "./app";
// eslint-disable
console.log(figlet.textSync("Fractal JS"));

// add program
const program = new Command();

program
.version(version)
.description(description)
.option("-a, --app <value>", "Create a fractal js app")
.option("-c, --c <value>", "Create a component")
.parse(process.argv);

const options = program.opts();

if (options.c) {
const component = new Component(options.c);
component.create();
}
if (options.a) {
new App(options.a).create();
}

if (!process.argv.slice(2).length) {
program.outputHelp();
}

console.log(options.c);
1 change: 0 additions & 1 deletion components/task/routes/tasks_home_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class TasksHomeRoute {
fractalJs.express.route(authEndpoint)
// GET endpoint
.get((_req: Request, res: Response) => {
// Get all contacts
res.status(200).send({
message: "Welcome to Fractal Task API.",
version: "1.0.0"
Expand Down
6 changes: 6 additions & 0 deletions examples/hello-world/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
build/
/node_modules
/build
.dockerignore
Dockerfile
24 changes: 24 additions & 0 deletions examples/hello-world/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[*]
charset=utf-8
end_of_line=lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style=space
indent_size=4

[*.json]
indent_style=space
indent_size=2

[*.sass]
indent_style=space
indent_size=4

[*.md]
trim_trailing_whitespace = false

[*.yaml]
insert_final_newline = false

[*.yml]
insert_final_newline = false
7 changes: 7 additions & 0 deletions examples/hello-world/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RDBMS_DATABASE_URI="mysql://DATBASE_USER:DATABASE_PASSWORD@DATABASE_HOST:DATABASE_PORT/DATABASE_DB"
NOSQL_DATABASE_URI="mongodb://DATABASE_HOST:DATABASE_PORT/DATABASE_DB"
NOSQL_DATABASE_ADAPTER="mongodb"
API_BASE='/v1/'
JWT_SECRET='fractal-hello-world-dev'
DEVELOPMENT_JWT_SECRET='fractal-hello-world-dev'
ENVIRONMENT='development'
1 change: 1 addition & 0 deletions examples/hello-world/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Loading

0 comments on commit d8c224e

Please sign in to comment.