From 1149eaa84faeceb2d3e1ac7bf46cfc2610cc6c04 Mon Sep 17 00:00:00 2001 From: Taranveer Virk Date: Mon, 2 Oct 2017 23:31:52 -0400 Subject: [PATCH] Apply feedback --- .vscode/tasks.json | 10 ++---- package.json | 4 +-- src/controllers/README.md | 3 ++ src/index.ts | 2 +- src/logger-mixin/index.ts | 6 ---- src/{logger-mixin => }/mixins/README.md | 21 ++---------- .../logger.mixin.ts} | 16 +++------ test/mocha.opts | 2 +- test/unit/logger-mixin.test.ts | 15 +------- tsconfig.json | 2 +- tslint.full.json | 34 ++++++++----------- 11 files changed, 33 insertions(+), 82 deletions(-) create mode 100644 src/controllers/README.md delete mode 100644 src/logger-mixin/index.ts rename src/{logger-mixin => }/mixins/README.md (74%) rename src/{logger-mixin/mixins/logger-mixin.ts => mixins/logger.mixin.ts} (84%) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f957137..4db6a20 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,8 +5,8 @@ "tasks": [ { "taskName": "Watch and compile TypeScript", - "command": "tsc", - "args": ["--watch"], + "command": "npm", + "args": ["--silent", "run", "build:watch"], "type": "process", "isBackground": true, "problemMatcher": "$tsc-watch", @@ -24,11 +24,7 @@ { "taskName": "Test and lint", "command": "npm", - "args": [ - "--silent", - "run", - "vscode-test" - ], + "args": ["--silent", "run", "test:dev"], "type": "process", "group": { "kind": "test", diff --git a/package.json b/package.json index e6c011a..9c7c9aa 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "build": "npm run build:lib && npm run build:lib6", "build:lib": "tsc --target es2017 --outDir dist", "build:lib6": "tsc --target es2015 --outDir dist6", + "build:watch": "tsc --watch", "clean": "rm -rf dist dist6", "lint": "tslint -c tslint.full.json --project tsconfig.json --type-check", "lint:fix": "npm run lint -- --fix", @@ -17,7 +18,7 @@ "pretest": "npm run clean && npm run build", "test": "mocha", "posttest": "npm run lint", - "vscode-test": "mocha && npm run lint" + "test:dev": "mocha && npm run lint" }, "repository": { "type": "git", @@ -41,7 +42,6 @@ "@loopback/testlab": "^4.0.0-alpha.7", "@types/mocha": "^2.2.43", "mocha": "^3.5.3", - "ts-node": "^3.3.0", "tslint": "^5.7.0", "typescript": "^2.5.2" } diff --git a/src/controllers/README.md b/src/controllers/README.md new file mode 100644 index 0000000..b1352d6 --- /dev/null +++ b/src/controllers/README.md @@ -0,0 +1,3 @@ +# Controllers + +This directory contains source files for the controllers exported by this extension. \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index bcc6538..bb75df8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,4 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -export * from './logger-mixin'; +export * from './mixins/logger.mixin'; diff --git a/src/logger-mixin/index.ts b/src/logger-mixin/index.ts deleted file mode 100644 index f27ed39..0000000 --- a/src/logger-mixin/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2017. All Rights Reserved. -// Node module: loopback-next-extension-starter -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './mixins/logger-mixin'; diff --git a/src/logger-mixin/mixins/README.md b/src/mixins/README.md similarity index 74% rename from src/logger-mixin/mixins/README.md rename to src/mixins/README.md index ec23699..972d2b4 100644 --- a/src/logger-mixin/mixins/README.md +++ b/src/mixins/README.md @@ -22,7 +22,7 @@ cosnt newClass = MixinClass1(MixinClass2(BaseClass)) ### LoggerMixin -LoggerMixin adds capabilities to a LoopBack Next Application by adding a `.logger()` function that allows used to bind a Logger class to `Context` automatically. The binding key will be `loggers.${Class.name}` where `Class.name` is the name of the Logger class being bound. The Mixin also overrides existing `.component()` function so that components are also capable of providing Logger's to be bound automatically. +LoggerMixin adds capabilities to a LoopBack Next Application by adding a `.logger()` function that is used to bind a Logger class to `Context` automatically. The binding key will be `loggers.${Class.name}` where `Class.name` is the name of the Logger class being bound. Components are also able to provide their own Logger implementation which will be bound via `.component()` automatically when using this Mixin. **Example** ``` @@ -57,7 +57,7 @@ class ColorLogger { Once a Logger has been bound, you can retrieve it by using [Dependency Inject](http://loopback.io/doc/en/lb4/Dependency-injection.html) -**More Examples for binding a Logger** +#### More Examples for binding a Logger** ``` // Using the app's .logger() function. class LoggingApplication extends LoggerMixin(Application) { @@ -80,20 +80,3 @@ const app = new LoggingApplication({ components: [MyComponent] // Logger from MyComponent will be bound to loggers.ColorLogger }); ``` - -## Contributions - -- [Guidelines](http://loopback.io/doc/en/contrib/index.html) -- [Join the team](https://github.com/strongloop/loopback-next/issues/110) - -## Tests - -Run `npm test` from the root folder. - -## Contributors - -See [all contributors](https://github.com/strongloop/loopback-next-extension-starter/graphs/contributors). - -## License - -MIT diff --git a/src/logger-mixin/mixins/logger-mixin.ts b/src/mixins/logger.mixin.ts similarity index 84% rename from src/logger-mixin/mixins/logger-mixin.ts rename to src/mixins/logger.mixin.ts index 89de078..f38b4cd 100644 --- a/src/logger-mixin/mixins/logger-mixin.ts +++ b/src/mixins/logger.mixin.ts @@ -29,13 +29,6 @@ export function LoggerMixin>(superClass: T) { this.logger(logger); } } - - if (this.options.components) { - // Super would have already mounted the component - for (const component of this.options.components) { - this.component(component); - } - } } /** @@ -61,7 +54,7 @@ export function LoggerMixin>(superClass: T) { /** * Add a component to this application. Also mounts - * all the components repositories. + * all the components Loggers. * * @param component The component to add. * @@ -69,10 +62,9 @@ export function LoggerMixin>(superClass: T) { * * export class ProductComponent { * controllers = [ProductController]; - * loggers = [Logger]; + * loggers = [ProductLogger]; * providers = { - * [AUTHENTICATION_STRATEGY]: AuthStrategy, - * [AUTHORIZATION_ROLE]: Role, + * [PRODUCT_PROVIDER]: ProductProvider, * }; * }; * @@ -89,7 +81,7 @@ export function LoggerMixin>(superClass: T) { * loggers. This function is intended to be used internally * by component() * - * @param component The component to mount repositories of + * @param component The component to mount Logger's of */ mountComponentLogger(component: Constructor) { const componentKey = `components.${component.name}`; diff --git a/test/mocha.opts b/test/mocha.opts index 66342aa..3b2b30f 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,3 +1,3 @@ ---compilers ts:ts-node/register --recursive --reporter dot +dist/test diff --git a/test/unit/logger-mixin.test.ts b/test/unit/logger-mixin.test.ts index a697125..7e49f9c 100644 --- a/test/unit/logger-mixin.test.ts +++ b/test/unit/logger-mixin.test.ts @@ -30,16 +30,6 @@ describe('LoggerMixin', () => { expectLoggerToBeBound(myApp); }); - it('binds user defined component without Logger', () => { - class EmptyTestComponent {} - - const myApp = new AppWithLogger({ - components: [EmptyTestComponent], - }); - - expectComponentToBeBound(myApp, EmptyTestComponent); - }); - it('binds user defined component with Logger in constructor', () => { const myApp = new AppWithLogger({ loggers: [MyLogger], @@ -66,10 +56,7 @@ describe('LoggerMixin', () => { }); const logger = myApp.getSync('loggers.MyLogger'); - expect(typeof logger.log).to.be.eql('function'); - expect(typeof logger.info).to.be.eql('function'); - expect(typeof logger.warn).to.be.eql('function'); - expect(typeof logger.error).to.be.eql('function'); + expect(logger).to.be.instanceOf(MyLogger); }); class AppWithLogger extends LoggerMixin(Application) {} diff --git a/tsconfig.json b/tsconfig.json index fff059d..cdbb46b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "lib": ["es2017", "dom"], "module": "commonjs", "moduleResolution": "node", - "target": "es6", + "target": "es2017", "outDir": "dist", "sourceMap": true, "declaration": true diff --git a/tslint.full.json b/tslint.full.json index 8af98a7..11aa5c8 100644 --- a/tslint.full.json +++ b/tslint.full.json @@ -1,21 +1,17 @@ { - "$schema": "http://json.schemastore.org/tslint", - "extends": [ - "./tslint.json" - ], - // This configuration files enabled rules which require type checking - // and therefore cannot be run by Visual Studio Code TSLint extension - // See https://github.com/Microsoft/vscode-tslint/issues/70 - "rules": { - // These rules find errors related to TypeScript features. - - - // These rules catch common errors in JS programming or otherwise - // confusing constructs that are prone to producing bugs. - - "await-promise": true, - "no-floating-promises": true, - "no-void-expression": [true, "ignore-arrow-function-shorthand"] - } + "$schema": "http://json.schemastore.org/tslint", + "extends": ["./tslint.json"], + // This configuration files enabled rules which require type checking + // and therefore cannot be run by Visual Studio Code TSLint extension + // See https://github.com/Microsoft/vscode-tslint/issues/70 + "rules": { + // These rules find errors related to TypeScript features. + + // These rules catch common errors in JS programming or otherwise + // confusing constructs that are prone to producing bugs. + + "await-promise": true, + "no-floating-promises": true, + "no-void-expression": [true, "ignore-arrow-function-shorthand"] } - \ No newline at end of file +}