Skip to content

Commit

Permalink
feat: added cli tooling, improved configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Mar 21, 2021
1 parent eb53c0e commit ade4d86
Show file tree
Hide file tree
Showing 33 changed files with 4,299 additions and 3,497 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/node_modules
/.cert
/.log
/logs
/lib
/storage
/tmp
/coverage
/tests/helpers/unreadable.txt
268 changes: 116 additions & 152 deletions README.md

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require('source-map-support').install();
const jsBeautify = require('js-beautify');

const RServer = require('./lib/main');
const app = RServer.create();
const { App } = require('./lib');
const app = new App();

// process form upload from our examples index.html file
app.post('/', function(req, res) {
app.post('/', function (req, res) {
const result = JSON.stringify(Object.assign({}, req.body, req.files));
return res.json(jsBeautify.js(result));
});
Expand All @@ -15,4 +15,5 @@ app.get('/download', (req, res) => {
return res.download('media/image.jpg');
});

app.listen();
module.exports = app;
// app.listen();
30 changes: 30 additions & 0 deletions bin/serve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node
const args = require('args');
const { App } = require('../lib');

const serveScriptOptions = (module.exports.serveScriptOptions = [
{
name: 'port',
description: 'the port on which the server will be running',
},
{
name: 'env',
description:
'The environment the server will be running on. either prod or dev. defaults to dev',
defaultValue: 'dev',
},
]);

args.options(serveScriptOptions);

const run = () => {
const { port, ...config } = args.parse(process.argv);

const server = new App({
config,
});

return server.listen(port);
};

run();
40 changes: 40 additions & 0 deletions bin/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node
const { getEntryPath } = require('@teclone/node-utils');
const args = require('args');
const { App } = require('../lib');
const { resolve } = require('path');
const { statSync } = require('fs');

const startScriptOptions = (module.exports.startScriptOptions = [
{
name: 'port',
description: 'the port on which the server will be running',
},
{
name: 'entry',
description:
'relative path to the entry file containing your app/server export, defaults to app.js',
defaultValue: 'app.js',
},
]);

args.options(startScriptOptions);

const run = () => {
const { port, entry, ...config } = args.parse(process.argv);
const entryPath = getEntryPath();

const entryFilePath = resolve(entryPath, entry);

try {
const stats = statSync(entryFilePath);
if (stats.isFile()) {
const app = require(entryFilePath);
app.listen(port);
}
} catch (ex) {
console.log(ex);
}
};

run();
45 changes: 28 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
"publishConfig": {
"access": "public"
},
"main": "lib/main",
"typings": "lib/main",
"main": "lib",
"typings": "lib",
"bin": {
"start": "./bin/start.js",
"serve": "./bin/serve.js"
},
"scripts": {
"commit": "git-cz",
"test": "BABEL_ENV=test jest --runInBand",
"watch-test": "BABEL_ENV=test jest --runInBand --watch",
"test": "cross-env BABEL_ENV=test jest --runInBand",
"watch-test": "cross-env BABEL_ENV=test jest --runInBand --watch",
"typings": "tsc --p ./tsconfig.build.json",
"build": "rimraf lib && yarn typings --declarationDir ./lib && rollup-all",
"lint": "eslint src/**/*.ts --fix",
"compile": "tsc --noEmit",
"start": "npm run build && node app.js",
"postinstall": "bash ./scripts/gen-ssl-cert.sh && node ./scripts/create-unreadable.js",
"start": "npm run build && node ./bin/start",
"serve": "npm run build && node ./bin/serve",
"postinstall": "bash ./scripts/gen-ssl-cert.sh",
"report-coverage": "jest --coverage --coverageReporters=text-lcov | coveralls",
"semantic-release": "semantic-release"
},
Expand All @@ -42,34 +47,37 @@
},
"homepage": "https://github.com/teclone/r-server#readme",
"devDependencies": {
"@babel/core": "7.4.0",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "7.4.0",
"@babel/preset-typescript": "7.3.3",
"@babel/runtime": "^7.12.5",
"@babel/core": "^7.13.10",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-object-rest-spread": "^7.13.8",
"@babel/plugin-transform-runtime": "^7.13.10",
"@babel/preset-typescript": "^7.13.0",
"@babel/runtime": "^7.13.10",
"@teclone/node-utils": "^1.1.1",
"@teclone/regex": "^1.0.3",
"@teclone/rollup-all": "1.10.8",
"@teclone/rollup-all": "^1.14.3",
"@teclone/utils": "^2.22.2",
"@types/jest": "24.0.11",
"@types/request-promise": "^4.1.47",
"@typescript-eslint/eslint-plugin": "2.0.0",
"@typescript-eslint/parser": "1.6.0",
"babel-jest": "24.3.1",
"babel-jest": "^26.6.3",
"commitizen": "4.0.3",
"coveralls": "3.0.3",
"cross-env": "^7.0.3",
"cz-conventional-changelog": "2.1.0",
"dotenv": "^8.2.0",
"jest": "24.3.1",
"jest": "^26.6.3",
"js-beautify": "1.7.5",
"mime-types": "^2.1.28",
"request": "2.88.0",
"request-promise": "^4.2.6",
"request-promise-native": "^1.0.9",
"rimraf": "2.6.3",
"semantic-release": "^17.0.4",
"semantic-release-cli": "5.2.0",
"source-map-support": "0.5.12",
"typescript": "3.3.3333",
"typescript": "^4.2.3",
"uuid": "^8.3.2"
},
"config": {
Expand All @@ -86,5 +94,8 @@
"dotenv": "^8.2.0",
"mime-types": "2.1.24",
"uuid": "3.3.3"
},
"dependencies": {
"@types/node": "^14.14.33"
}
}
15 changes: 6 additions & 9 deletions src/.server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { RServerConfig } from './@types';
import * as path from 'path';

const rServerConfig: RServerConfig = {
entryPath: path.resolve(__dirname, '../'),
export const rServerConfig: RServerConfig = {
entryPath: './',

env: 'development',

errorLog: '.log/error.log',
errorLog: 'logs/error.log',

accessLog: '.log/access.log',
accessLog: 'logs/access.log',

profileRequest: true,

tempDir: 'storage/temp',
tempDir: 'tmp/uploads',

publicPaths: ['public'],

serveHiddenFiles: false,
serveHiddenFiles: true,

cacheControl: 'no-cache, max-age=86400',

Expand Down Expand Up @@ -50,5 +49,3 @@ const rServerConfig: RServerConfig = {
},
},
};

export default rServerConfig;
29 changes: 21 additions & 8 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import Response from '../modules/Response';

export type Env = 'development' | 'production';

export interface ObjectOfAny {
[p: string]: any;
}

export interface RServerConfig {
entryPath: string;
entryPath?: string;

env: Env;

Expand All @@ -23,7 +27,7 @@ export interface RServerConfig {

cacheControl: string;

encoding: string;
encoding: BufferEncoding;

maxMemory: string | number;

Expand Down Expand Up @@ -101,7 +105,7 @@ export type Method =
| 'head'
| 'options'
| 'delete'
| 'all';
| '*';

export type Url = string;

Expand All @@ -120,7 +124,12 @@ export interface Next {
export type Callback<
Rq extends Request = Request,
Rs extends Response = Response
> = (request: Rq, response: Rs, ...parameters: Parameter[]) => Promise<boolean>;
> = (
request: Rq,
response: Rs,
params: ObjectOfAny,
options?: ObjectOfAny
) => Promise<boolean>;

export type ErrorCallback<
Rq extends Request = Request,
Expand All @@ -134,25 +143,30 @@ export type Middleware<
request: Rq,
response: Rs,
next: Next,
...parameters: Parameter[]
params: ObjectOfAny,
options?: ObjectOfAny
) => Promise<boolean> | boolean;

export type ListenerCallback = () => void;

export interface CallbackOptions {
middleware: Middleware | Middleware[];
use: Middleware | Middleware[];
options?: ObjectOfAny;
}

export interface MiddlewareOptions {
method: Method | Method[];
options?: ObjectOfAny;
}

export interface ResolvedCallbackOptions {
middleware: Middleware[];
use: Middleware[];
options?: ObjectOfAny;
}

export interface ResolvedMiddlewareOptions {
method: Method[];
options?: ObjectOfAny;
}

export type RouteInstance = [
Expand Down Expand Up @@ -262,5 +276,4 @@ export interface Routes {
post: RouteInstance[];
put: RouteInstance[];
delete: RouteInstance[];
all: RouteInstance[];
}
4 changes: 2 additions & 2 deletions src/Exceptions/EntityTooLargeException.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Exception from '.';
import { Exception } from '.';

export default class EntityTooLargeException extends Exception {
export class EntityTooLargeException extends Exception {
constructor() {
super('request entity too large', EntityTooLargeException);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* exception base class
*/
export default class Exception extends Error {
export class Exception extends Error {
constructor(message: string, domain: any = Exception) {
super(message);

Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { App } from './modules/App';

export { setErrorCallback } from './modules/Utils';

export { Router } from './modules/Router';
30 changes: 0 additions & 30 deletions src/main.ts

This file was deleted.

Loading

0 comments on commit ade4d86

Please sign in to comment.