Skip to content

Commit

Permalink
feat(inversify): upgrade inversify to the latest version
Browse files Browse the repository at this point in the history
Also add some quality improvements with tests and mutation testing.
  • Loading branch information
Sander Koenders authored Oct 2, 2018
2 parents 76e3908 + c9c3e96 commit 29ae8eb
Show file tree
Hide file tree
Showing 24 changed files with 511 additions and 140 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 2 space indentation
[{*.ts,*.js,*.json}]
indent_style = space
indent_size = 2
26 changes: 22 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# Operating system related files
*~
.fuse_hidden*
.directory
.Trash-*
/.idea
/node_modules/
/env.json

/**/*.js
# Intellij ide files
.idea

# Ignore environment file, use env.json.example instead
config/env.json

# Ignore package manager files
node_modules/
package-lock.json

# Ignore everything related to test output
reports
.nyc_output

# Ignore javascript files, sourcemaps and type declarations
src/**/*.js
test/**/*.js
src/**/*.d.ts
test/**/*.d.ts
src/**/*.js.map
test/**/*.js.map
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"files": {
"exclude": {
".git": "",
".tscache": "",
"**/*.d.ts": true,
"**/*.js": {
"when": "$(basename).ts"
},
"**/*.map": {
"when": "$(basename)"
}
}
}
}
40 changes: 22 additions & 18 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node
"use strict";
'use strict';

const debug = require("debug")("express:server");
const http = require("http");
/* tslint:disable:no-console */
/* tslint:disable:no-var-requires */
const http = require('http');

try {
const env = require("../env.json");
const env = require('../config/env.json');

startServer(env);
} catch (err) {
Expand All @@ -15,20 +16,23 @@ try {
function startServer(env) {
env.port = normalizePort(env.port);

const main = new (require("../src/main"))(env);

main.App.set("port", env.port);

const main = new (require('../src/main'))(env);
const server = http.createServer(main.App);

server.listen(env.port);
main.initialize().then(() => {
main.App.set('port', env.port);

server.listen(env.port);

server.on("error", onError);
server.on("listening", main.onListening.bind(main));
server.on('error', onError);
server.on('listening', main.onListening.bind(main));
}).catch((err) => {
console.log(err);
});
}

function normalizePort(val) {
var port = parseInt(val, 10);
const port = parseInt(val, 10);

if (isNaN(port)) {
return val;
Expand All @@ -42,20 +46,20 @@ function normalizePort(val) {
}

function onError(error) {
if (error.syscall !== "listen") {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
Expand Down
File renamed without changes.
33 changes: 26 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"description": "An example for NodeJS in combination with Inversify",
"main": "bin/www",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"tslint": "tslint --project tsconfig.json",
"build": "npm run tslint && tsc",
"pretest": "npm run build",
"test": "nyc --reporter=html --report-dir=reports/coverage --check-coverage --lines 80 --functions 80 --branches 75 mocha \"test/**/*Spec.js\" --exit",
"posttest": "stryker run"
},
"keywords": [
"NodeJS",
Expand All @@ -14,13 +18,28 @@
"author": "Sander Koenders",
"license": "Apache-2.0",
"devDependencies": {
"@types/express": "^4.0.35",
"inversify": "^3.1.0",
"inversify-binding-decorators": "^3.0.0",
"reflect-metadata": "^0.1.10",
"typescript": "^2.2.1"
"@types/chai": "^4.1.4",
"@types/express": "^4.16.0",
"@types/mocha": "^5.2.5",
"@types/sinon": "^5.0.2",
"chai": "^4.1.2",
"mocha": "^5.2.0",
"nyc": "^13.0.1",
"sinon": "^6.2.0",
"source-map-support": "^0.5.9",
"stryker": "^0.29.4",
"stryker-api": "^0.21.0",
"stryker-html-reporter": "^0.16.1",
"stryker-mocha-framework": "^0.12.1",
"stryker-mocha-runner": "^0.14.1",
"stryker-typescript": "^0.13.2",
"tslint": "^5.11.0",
"typescript": "^3.0.3"
},
"dependencies": {
"express": "^4.15.2"
"express": "^4.15.2",
"reflect-metadata": "^0.1.12",
"inversify": "^4.13.0",
"inversify-binding-decorators": "^4.0.0"
}
}
8 changes: 4 additions & 4 deletions src/constants/environment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface Environment {
appName: string,
port: Number,
loadPaths: Array<string>
}
appName: string;
port: number;
loadPaths: string[];
}
7 changes: 4 additions & 3 deletions src/constants/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const TYPES = {
Environment: Symbol("Environment"),
Logger: Symbol("Logger")
Environment: Symbol('Environment'),
Logger: Symbol('Logger'),
Console: Symbol('Console')
};

export default TYPES;
export default TYPES;
59 changes: 53 additions & 6 deletions src/ioc/ioc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
import "reflect-metadata";
import { Container, inject } from 'inversify';
import { autoProvide, makeFluentProvideDecorator } from 'inversify-binding-decorators';
import { Container, ContainerModule } from 'inversify';
import * as path from 'path';
import * as fs from 'fs';
import 'reflect-metadata';

let container = new Container();
export default class IocContext {
private static METADATA_PROVIDE_KEY = 'inversify-binding-decorators:provide';
private static ALLOWED_EXTENSIONS = ['.js'];

let provide = makeFluentProvideDecorator(container);
public constructor(
private projectRoot: string,
/* istanbul ignore next */
private loader: NodeRequire = require,
/* istanbul ignore next */
private container: Container = new Container(),
/* istanbul ignore next */
private reflect: any = Reflect
) { }

export { container, autoProvide, provide, inject };
public componentScan(contextPaths: string[]) {
contextPaths.forEach(contextPath => this.readDir(path.join(this.projectRoot, contextPath)));

this.container.load(this.getAnnotatedDependencies());
}

public getContainer() {
return this.container;
}

private readDir(dir: string) {
fs.readdirSync(dir).forEach((entity) => {
this.processEntity(path.join(dir, entity));
});
}

private processEntity(entity: string) {
if (fs.statSync(entity).isDirectory()) {
this.readDir(entity);
} else {
this.loadFile(entity);
}
}

private loadFile(filePath: string) {
if (IocContext.ALLOWED_EXTENSIONS.indexOf(path.extname(filePath)) !== -1) {
this.loader(filePath);
}
}

private getAnnotatedDependencies() {
return new ContainerModule(bind => {
const provideMetadata: any[] = this.reflect.getMetadata(IocContext.METADATA_PROVIDE_KEY, this.reflect) || [];
provideMetadata.map(metadata => metadata.constraint(bind, metadata.implementationType));
});
}
}
8 changes: 8 additions & 0 deletions src/ioc/iocUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { fluentProvide, provide } from 'inversify-binding-decorators';
import { interfaces, inject } from 'inversify';

const provideSingleton = (identifier: interfaces.ServiceIdentifier<any>) => {
return fluentProvide(identifier).inSingletonScope().done();
};

export { provideSingleton, fluentProvide, inject, provide };
32 changes: 0 additions & 32 deletions src/ioc/loader.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/libraries/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface Logger {
log(message: string);
log(message: string): void;
}

export default Logger;
export default Logger;
29 changes: 16 additions & 13 deletions src/libraries/logger/loggerImpl.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import TYPES from "../../constants/types";
import Logger from "./logger";
import { provide, inject } from "../../ioc/ioc";
import TYPES from '../../constants/types';
import Logger from './logger';
import { inject, provideSingleton } from '../../ioc/iocUtils';
import 'reflect-metadata';

@provide(TYPES.Logger).done()
/* tslint:disable:no-console */
@provideSingleton(TYPES.Logger)
class LoggerImpl implements Logger {
private _appName: string;
private appName: string;
private consoleRef: any;

public constructor(@inject(TYPES.Environment) environment: any)
{
this._appName = environment.appName;
}
public constructor(@inject(TYPES.Environment) environment: any, @inject(TYPES.Console) consoleRef: any) {
this.consoleRef = consoleRef || console;
this.appName = environment.appName;
}

public log(message: string) {
console.log(this._appName + " says: " + message);
}
public log(message: string) {
this.consoleRef.log(this.appName + ' says: ' + message);
}
}

export default LoggerImpl;
export default LoggerImpl;
Loading

0 comments on commit 29ae8eb

Please sign in to comment.