Skip to content

Commit a6feb83

Browse files
feat(debug): Add debug feature for extra output (#41)
* feat(debug): Add debug feature for extra output * build: Fix build with rollup json
1 parent 72d62aa commit a6feb83

19 files changed

+1374
-308
lines changed

package-lock.json

+1,253-250
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"body-parser": "^1.18.2",
77
"commander": "^2.12.2",
88
"compression": "^1.7.1",
9+
"debug": "^3.1.0",
910
"del": "^3.0.0",
1011
"express": "^4.16.2",
1112
"express-winston": "git+https://github.com/ChrisAlderson/express-winston.git#3.0.0",
@@ -14,6 +15,7 @@
1415
"mongoose": "^4.13.7",
1516
"p-map": "^1.2.0",
1617
"response-time": "^2.3.2",
18+
"rollup-plugin-json": "^2.3.0",
1719
"winston": "^3.0.0-rc1"
1820
},
1921
"devDependencies": {
@@ -30,27 +32,27 @@
3032
"babel-register": "^6.26.0",
3133
"chai": "^4.1.2",
3234
"coveralls": "^3.0.0",
33-
"cross-env": "^5.1.1",
35+
"cross-env": "^5.1.3",
3436
"esdoc": "^1.0.4",
3537
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
3638
"esdoc-flow-type-plugin": "^1.0.1",
3739
"esdoc-standard-plugin": "^1.0.0",
38-
"eslint": "^4.13.1",
39-
"eslint-config-vixo": "^2.0.1",
40+
"eslint": "^4.14.0",
41+
"eslint-config-vixo": "^2.0.2",
4042
"flow": "^0.2.3",
4143
"flow-bin": "^0.61.0",
4244
"flow-typed": "^2.2.3",
4345
"husky": "^0.14.3",
4446
"mocha": "^4.0.1",
45-
"nyc": "^11.4.0",
46-
"rollup": "^0.52.2",
47+
"nyc": "^11.4.1",
48+
"rollup": "^0.53.0",
4749
"rollup-plugin-babel": "^3.0.3",
4850
"rollup-plugin-node-resolve": "^3.0.0",
4951
"rollup-plugin-uglify": "^2.0.1",
5052
"sinon": "^4.1.3",
5153
"standard-version": "^4.2.0",
5254
"supertest": "^3.0.0",
53-
"uglify-es": "^3.2.2"
55+
"uglify-es": "^3.3.2"
5456
},
5557
"license": "MIT",
5658
"scripts": {

rollup.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import babel from 'rollup-plugin-babel'
2+
import json from 'rollup-plugin-json'
23
import resolve from 'rollup-plugin-node-resolve'
34
import uglify from 'rollup-plugin-uglify'
45
import { minify } from 'uglify-es'
@@ -22,6 +23,7 @@ export default {
2223
],
2324
plugins: [
2425
resolve(),
26+
json(),
2527
babel(),
2628
uglify({}, minify)
2729
],

src/PopApi.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Import the necessary modules.
22
// @flow
3+
import debug from 'debug'
34
/**
45
* Fast, unopinionated, minimalist web framework for node.
56
* @external {Express} https://github.com/expressjs/express
@@ -16,6 +17,7 @@ import {
1617
Routes
1718
} from './middleware'
1819
import * as utils from './utils'
20+
import { name } from '../package.json'
1921

2022
/**
2123
* The default log directory.
@@ -51,6 +53,12 @@ export default class PopApi {
5153
*/
5254
static app: $Application = express()
5355

56+
/**
57+
* The debugger for extra output.
58+
* @type {Function}
59+
*/
60+
static debug: Function = debug(name)
61+
5462
/**
5563
* A map of the installed plugins.
5664
* @type {Map<any>}

src/controllers/BaseContentController.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export default class BaseContentController extends IContentController {
5555
* Default method to register the routes.
5656
* @override
5757
* @param {!Object} router - The router to register the routes to.
58-
* @param {?PopApi} [PopApi] - The PopApi instance.
58+
* @param {!PopApi} PopApi - The PopApi instance.
5959
* @returns {undefined}
6060
*/
61-
registerRoutes(router: Object, PopApi?: any): void {
61+
registerRoutes(router: Object, PopApi: any): void {
6262
const t = this.basePath
6363

6464
router.get(`/${t}s`, this.getContents.bind(this))

src/controllers/IController.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default class IController {
1111
* Default method to register the routes.
1212
* @abstract
1313
* @param {!Object} router - The router to register the routes to.
14-
* @param {?PopApi} [PopApi] - The PopApi instance.
14+
* @param {!PopApi} PopApi - The PopApi instance.
1515
* @throws {Error} - Using default method: 'registerRoutes'
1616
* @returns {undefined}
1717
*/
18-
registerRoutes(router: Object, PopApi?: any): void {
18+
registerRoutes(router: Object, PopApi: any): void {
1919
throw new Error('Using default method: \'registerRoutes\'')
2020
}
2121

src/middleware/Cli.js

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ export default class Cli {
4343
* middleware!
4444
*/
4545
constructor(PopApi: any, {argv, name, version}: Object): void {
46+
const { name: debugName } = this.constructor
47+
PopApi.debug(`Registering ${debugName} middleware with options: %o`, {
48+
argv,
49+
name,
50+
version
51+
})
52+
4653
if (!name || !version) {
4754
throw new TypeError('\'name\' and \'version\' are required options for the Cli middleware!')
4855
}

src/middleware/Database.js

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ export default class Database {
7070
username,
7171
password
7272
}: Object): void {
73+
const { name: debugName } = this.constructor
74+
PopApi.debug(`Registering ${debugName} middleware with options: %o`, {
75+
database,
76+
hosts,
77+
dbPort,
78+
username,
79+
password
80+
})
81+
7382
if (!database) {
7483
throw new TypeError('\'database\' is a required option for the Database middleware!')
7584
}

src/middleware/HttpServer.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ export default class HttpServer {
4646
*/
4747
constructor(PopApi: any, {
4848
app,
49-
serverPort = process.env.PORT,
49+
serverPort = process.env.PORT || 5000,
5050
workers = 2
5151
}: Object): void {
52+
const { name: debugName } = this.constructor
53+
PopApi.debug(`Registering ${debugName} middleware with options: %o`, {
54+
serverPort,
55+
workers
56+
})
57+
5258
if (!app) {
5359
throw new TypeError('\'app\' is a required option for the HttpServer middleware!')
5460
}
@@ -62,7 +68,7 @@ export default class HttpServer {
6268
* The port on which the API will run on. Default is `5000`.
6369
* @type {number}
6470
*/
65-
this.serverPort = serverPort || 5000
71+
this.serverPort = serverPort
6672
/**
6773
* The amount of workers on the cluster.
6874
* @type {number}

src/middleware/Logger.js

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ export default class Logger {
6565
* Logger middleware!
6666
*/
6767
constructor(PopApi: any, {name, logDir, pretty, quiet}: Object): void {
68+
const { name: debugName } = this.constructor
69+
PopApi.debug(`Registering ${debugName} middleware with options: %o`, {
70+
name,
71+
logDir,
72+
pretty,
73+
quiet
74+
})
75+
6876
if (!name || !logDir) {
6977
throw new TypeError('\'name\' and \'logDir\' are required options for the Logger middleware!')
7078
}

src/middleware/Routes.js

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export default class Routes {
3434
* middleware!
3535
*/
3636
constructor(PopApi: any, {app, controllers}: Object): void {
37+
const { name: debugName } = this.constructor
38+
PopApi.debug(`Registering ${debugName} middleware with options: %o`, {
39+
controllers
40+
})
41+
3742
if (!app) {
3843
throw new TypeError('\'app\' is a required option for the Routes middleware!')
3944
}
@@ -58,6 +63,7 @@ export default class Routes {
5863
const { Controller, args } = c
5964
const controller = new Controller(args)
6065

66+
PopApi.debug(`Registering ${Controller.name} route controller`)
6167
controller.registerRoutes(app, PopApi)
6268
})
6369
}

test/PopApi.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ describe('PopApi', () => {
2323
expect(PopApi.app).to.exist
2424
})
2525

26+
/** @test {PopApi.debug} */
27+
it('should have a debug function for extra output', () => {
28+
expect(PopApi.debug).to.exist
29+
expect(PopApi.debug).to.be.a('function')
30+
})
31+
2632
/** @test {PopApi._installedPlugins} */
2733
it('should have a static map for the installed plugins', () => {
2834
expect(PopApi._installedPlugins).to.exist

test/controllers/BaseContentController.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import supertest from 'supertest'
1010
import {
1111
BaseContentController,
1212
ContentService,
13-
Database
13+
Database,
14+
PopApi
1415
} from '../../src'
1516
import {
1617
ExampleModel,
@@ -87,7 +88,7 @@ describe('BaseContentController', () => {
8788
baseContentController.registerRoutes(app)
8889
request = supertest(app)
8990

90-
database = new Database({}, {
91+
database = new Database(PopApi, {
9192
database: name
9293
})
9394
database.connect()

test/controllers/ContentService.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { expect } from 'chai'
55

66
import {
77
ContentService,
8-
Database
8+
Database,
9+
PopApi
910
} from '../../src'
1011
import {
1112
ExampleModel,
@@ -41,7 +42,7 @@ describe('ContentService', () => {
4142
query: {}
4243
})
4344

44-
database = new Database({}, {
45+
database = new Database(PopApi, {
4546
database: name
4647
})
4748
database.connect()

test/middleware/Cli.spec.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import sinon from 'sinon'
55
import { expect } from 'chai'
66

7-
import { Cli } from '../../src'
7+
import {
8+
Cli,
9+
PopApi
10+
} from '../../src'
811
import {
912
name,
1013
version
@@ -45,7 +48,7 @@ describe('Cli', () => {
4548
error = sinon.stub(console, 'error')
4649
info = sinon.stub(console, 'info')
4750

48-
cli = new Cli({}, {
51+
cli = new Cli(PopApi, {
4952
argv: ['', '', '-m', 'pretty'],
5053
name,
5154
version
@@ -54,14 +57,14 @@ describe('Cli', () => {
5457

5558
/** @test {Cli#constructor} */
5659
it('should create a new Cli instance without arguments to parse', () => {
57-
const cli = new Cli({}, {
60+
const cli = new Cli(PopApi, {
5861
name,
5962
version
6063
})
6164
expect(cli).to.be.an('object')
6265

6366
try {
64-
new Cli({}, {}) // eslint-disable-line no-new
67+
new Cli(PopApi, {}) // eslint-disable-line no-new
6568
expect(true).to.be.false
6669
} catch (err) {
6770
expect(err).to.be.an('Error')

test/middleware/Database.spec.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import sinon from 'sinon'
88
import { expect } from 'chai'
99
import { join } from 'path'
1010

11-
import { Database } from '../../src'
11+
import {
12+
Database,
13+
PopApi
14+
} from '../../src'
1215
import { name } from '../../package'
1316

1417
/** @test {Database} */
@@ -56,7 +59,7 @@ describe('Database', () => {
5659
])
5760
mkdirp.sync(logDir)
5861

59-
database = new Database({}, {
62+
database = new Database(PopApi, {
6063
database: name,
6164
username: '',
6265
password: ''
@@ -67,18 +70,18 @@ describe('Database', () => {
6770
it('should test the use the environment variables to connect', () => {
6871
const stub = process.env.MONGO_PORT_27017_TCP_ADDR
6972
process.env.MONGO_PORT_27017_TCP_ADDR = 'localhost'
70-
new Database({}, { // eslint-disable-line no-new
73+
new Database(PopApi, { // eslint-disable-line no-new
7174
database: name
7275
})
7376

7477
process.env.MONGO_PORT_27017_TCP_ADDR = null
75-
new Database({}, { // eslint-disable-line no-new
78+
new Database(PopApi, { // eslint-disable-line no-new
7679
database: name
7780
})
7881
process.env.MONGO_PORT_27017_TCP_ADDR = stub
7982

8083
try {
81-
new Database({}, {}) // eslint-disable-line no-new
84+
new Database(PopApi, {}) // eslint-disable-line no-new
8285
expect(true).to.be.false
8386
} catch (err) {
8487
expect(err).to.be.an('Error')
@@ -113,7 +116,7 @@ describe('Database', () => {
113116
expect(mongoose.connection.readyState).to.equal(0)
114117

115118
try {
116-
new Database({}, { // eslint-disable-line no-new
119+
new Database(PopApi, { // eslint-disable-line no-new
117120
database: name
118121
})
119122
process.env.NODE_ENV = 'test'
@@ -165,7 +168,7 @@ describe('Database', () => {
165168
expect(mongoose.connection.readyState).to.be.a('number')
166169
expect(mongoose.connection.readyState).to.equal(0)
167170

168-
const database = new Database({}, {
171+
const database = new Database(PopApi, {
169172
database: name,
170173
username: 'fault',
171174
password: 'faulty'

0 commit comments

Comments
 (0)