Skip to content

Commit

Permalink
feat: app-autoload add config module
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Feb 25, 2021
1 parent efe7329 commit 24df2e3
Show file tree
Hide file tree
Showing 20 changed files with 523 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
'@ecomfe/eslint-config/typescript',
],
rules: {
'no-console': 'off'
'no-console': 'off',
'spaced-comment': ["error", "always", { "markers": ["/"] }]
},
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

# mac
.DS_Store
5 changes: 5 additions & 0 deletions commmitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
extends: [
"@commitlint/config-conventional"
]
};
2 changes: 1 addition & 1 deletion example/hoth-quickstart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "0.1.0",
"description": "Fastify Resty quickstart API application",
"author": "Demidovich Daniil <demidovich.daniil@gmail.com>",
"author": "cxtom <cxtom2008@gmail.com>",
"license": "MIT",
"main": "src/app.ts",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion example/hoth-quickstart/src/config/default.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default {};
export = {
test: 1,
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {Controller, ALL} from 'fastify-decorators';
import {Controller, ALL, getFastifyInstanceByAppName} from '@hoth/decorators';
import {FastifyReply} from 'fastify';
import '@hoth/app-autoload';

@Controller('/post')
export default class PostController {

private readonly service = getFastifyInstanceByAppName('hoth');

@ALL('/')
addApp(req, reply: FastifyReply) {
console.log(this.service.appConfig.get('test'));
reply.send('ok');
}
}
4 changes: 3 additions & 1 deletion example/hoth-quickstart/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
"outDir": "dist",
"declaration": false,
"composite": false
}
}
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
}
},
"npmClient": "yarn",
"version": "independent"
"version": "independent",
"useWorkspaces": true
}
6 changes: 3 additions & 3 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"watch": ["src"],
"ignore": ["src/**/*.test.ts"],
"ext": "ts,mjs,js,json,graphql",
"watch": ["packages/*/src", "example/*/src"],
"ignore": ["*.test.ts"],
"ext": "ts,js",
"exec": "yarn run build && yarn run hoth start --app-dir='example/hoth-quickstart/dist'",
"legacyWatch": true
}
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"engines": {
"node": ">=12",
"yarn": "^1.0"
"yarn": "^1.22.0"
},
"workspaces": [
"packages/*",
Expand All @@ -25,14 +25,17 @@
"build": "lerna run build",
"dev": "nodemon"
},
"pre-commit": [
"lint-staged"
],
"lint-staged": {
"**/*.ts": [
"eslint"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"dependencies": {
"lint-staged": "^10.5.4",
"lodash": "^4.17.20",
Expand All @@ -41,6 +44,8 @@
},
"devDependencies": {
"@babel/eslint-plugin": "^7.12.13",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@ecomfe/eslint-config": "^7.0.0",
"@types/node": "^14.14.22",
"@typescript-eslint/eslint-plugin": "^4.14.2",
Expand All @@ -49,9 +54,9 @@
"esbuild-node-tsc": "^1.2.0",
"eslint": "^7.19.0",
"fastify": "^3.11.0",
"husky": "^4.3.8",
"lerna": "^3.22.1",
"nodemon": "^2.0.7",
"pre-commit": "^1.2.2",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
Expand Down
1 change: 1 addition & 0 deletions packages/app-autoload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@hoth/decorators": "^1.0.0",
"@hoth/utils": "^1.0.0",
"config": "^3.3.3",
"fastify-autoload": "^3.4.2",
"fastify-plugin": "^3.0.0",
"tslib": "^2.1.0"
Expand Down
37 changes: 31 additions & 6 deletions packages/app-autoload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* @author cxtom
*/

process.env.SUPPRESS_NO_CONFIG_WARNING = 'y';

import Config from 'config';
import {resolve, join, isAbsolute} from 'path';
import autoload from 'fastify-autoload';
import {existsSync, readdirSync} from 'fs';
Expand All @@ -22,6 +25,7 @@ interface AppConfig {
dir: string;
prefix: string;
name: string;
rootPath: string;
}

interface PluginAppConfig extends AppConfig {
Expand All @@ -34,11 +38,14 @@ interface PluginAppConfig extends AppConfig {

declare module 'fastify' {
interface FastifyInstance {
readonly appConfig: PluginAppConfig;
readonly appConfig: {
get: (property: string) => any;
has: (property: string) => boolean;
};
}
}

async function loadSingleApp(appConfig: AppConfig, childInstance: FastifyInstance) {
async function load(appConfig: AppConfig, childInstance: FastifyInstance) {

const pluginAppConfig: PluginAppConfig = {
...appConfig,
Expand All @@ -48,22 +55,38 @@ async function loadSingleApp(appConfig: AppConfig, childInstance: FastifyInstanc
entryPath: join(appConfig.dir, 'app.js'),
};

childInstance.decorate('appConfig', appConfig);

// load config
if (!existsSync(pluginAppConfig.configPath)) {
exit(`Did not find \`config\` dir in ${appConfig.dir}`);
return;
}

const config = Config.util.loadFileConfigs(pluginAppConfig.configPath);
Config.util.setModuleDefaults(appConfig.name, {
...config,
...appConfig,
});

childInstance.decorate('appConfig', {
get(property: string | string[]) {
const props = Array.isArray(property)
? [appConfig.name, ...property]
: `${appConfig.name}.${property}`;
if (Config.has(props)) {
return Config.get(props);
}
},
});

// load controllers
if (!existsSync(pluginAppConfig.controllerPath)) {
exit(`Did not find \`controller\` dir in ${appConfig.dir}`);
return;
}
await childInstance.register(bootstrap, {
directory: pluginAppConfig.controllerPath,
mask: /\.controller\./,
mask: /\.controller\.js$/,
appName: appConfig.name,
});

// register app plugins
Expand Down Expand Up @@ -110,6 +133,7 @@ export default fp(async function (instance: FastifyInstance, opts: AppAutoload)
dir: appRoot,
prefix,
name: name || (prefix === '/' ? 'root' : prefix.slice(1)),
rootPath,
}];
}
else {
Expand All @@ -121,6 +145,7 @@ export default fp(async function (instance: FastifyInstance, opts: AppAutoload)
dir: dirPath,
prefix: `${prefix}${prefix === '/' ? '' : '/'}${dir}`,
name: dir.name,
rootPath,
});
}
}
Expand All @@ -132,7 +157,7 @@ export default fp(async function (instance: FastifyInstance, opts: AppAutoload)
}

for await (const appConfig of apps) {
await instance.register(loadSingleApp.bind(null, appConfig), {
await instance.register(load.bind(null, appConfig), {
prefix: appConfig.prefix,
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/app-autoload/src/module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'config';
2 changes: 1 addition & 1 deletion packages/decorators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"homepage": "https://github.com/cxtom/hoth#readme",
"dependencies": {
"fastify-decorators": "^3.8.0",
"fastify-decorators": "~3.8.0",
"tslib": "^2.1.0"
},
"files": [
Expand Down
26 changes: 22 additions & 4 deletions packages/decorators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */

import type {FastifyInstance} from 'fastify';
import type {AutoLoadConfig} from 'fastify-decorators/interfaces/bootstrap-config';

import {bootstrap as bootstrapInner} from 'fastify-decorators';

export {
bootstrap,
BootstrapConfig,
Controller,
ControllerType,
ControllerConfig,
Expand All @@ -13,7 +16,6 @@ export {
Destructor,
Hook,
ErrorHandler,
getInstanceByToken,
ALL,
DELETE,
GET,
Expand All @@ -22,5 +24,21 @@ export {
PATCH,
POST,
PUT,
FastifyInstanceToken,
getInstanceByToken,
} from 'fastify-decorators';

export interface BootstrapConfig extends AutoLoadConfig {
appName: string;
}

export const appFastifyInstanceTokenMap = new Map<string, FastifyInstance>();

export function getFastifyInstanceByAppName(name: string) {
return appFastifyInstanceTokenMap.get(name);
}

export async function bootstrap(fastify: FastifyInstance, config: BootstrapConfig) {
const appName = config.appName;
appFastifyInstanceTokenMap.set(appName, fastify);
return await bootstrapInner(fastify, config);
}
3 changes: 3 additions & 0 deletions packages/logger/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @hoth/logger

logger for hoth framework
29 changes: 29 additions & 0 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@hoth/logger",
"version": "1.0.0",
"description": "logger plugin for hoth framework",
"main": "dist/index.js",
"scripts": {
"build": "tsc --build tsconfig.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cxtom/hoth.git"
},
"keywords": [
"logger"
],
"author": "cxtom (cxtom2008@gmail.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/cxtom/hoth/issues"
},
"homepage": "https://github.com/cxtom/hoth#readme",
"dependencies": {
"resolve-from": "^5.0.0",
"tslib": "^2.1.0"
},
"files": [
"dist"
]
}
4 changes: 4 additions & 0 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @file logger
* @author cxtom
*/
7 changes: 7 additions & 0 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
}
}
Loading

0 comments on commit 24df2e3

Please sign in to comment.