Skip to content

Commit

Permalink
Merge pull request #28 from remojansen/master
Browse files Browse the repository at this point in the history
  • Loading branch information
remojansen authored Sep 10, 2016
2 parents e5580b0 + df4caa1 commit f3cdd21
Show file tree
Hide file tree
Showing 28 changed files with 129 additions and 196 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ test/**/*.js
type_definitions/**/*.js
type_definitions/*.js

.typingsrc
.typingsrc
dts
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ node_js:
- 5.1.1
- 4.4.6
before_install:
- npm install -g typings
- npm install -g codeclimate-test-reporter
- typings install
after_success:
- codeclimate-test-reporter < coverage/lcov.info
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test/**/*.js": true,
"**/es": true,
"**/lib": true,
"**/dts": true,
"**/coverage": true,
"**/dist": true,
"**/docs": true,
Expand Down
5 changes: 1 addition & 4 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Bug fix / Docs (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

Expand All @@ -29,8 +28,6 @@
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] My change requires a change to the type definitions.
- [ ] I have updated the type definitions accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ An utility that allows developers to declare [InversifyJS](http://inversify.io/)
You can install `inversify-binding-decorators` using npm:

```
$ npm install --save inversify inversify-binding-decorators reflect-metadata
$ npm install --save-dev inversify-dts
$ npm install [email protected] inversify-binding-decorators reflect-metadata --save
```

If you are workiong with TypeScript you will need the following `.d.ts` files:

```
/// <reference path="node_modules/reflect-metadata/reflect-metadata.d.ts" />
/// <reference path="node_modules/inversify-dts/inversify-binding-decorators/inversify-binding-decorators.d.ts" />
```
The `inversify-binding-decorators` type definitions are included in the npm module and require TypeScript 2.0.
Please refer to the [InversifyJS documentation](https://github.com/inversify/InversifyJS#installation) to learn more about the installation process.

### The basics
The [InversifyJS](http://inversify.io/) API allows us to delcare bindings using a fluent API:
Expand Down Expand Up @@ -84,9 +79,6 @@ class Shuriken implements ThrowableWeapon {

```

A basic example can be found at the
[inversify-code-samples](https://github.com/inversify/inversify-code-samples/tree/master/inversify-binding-decorators) repository.

### Using classes, string literals & symbols as identifiers
When you invoke `@provide` using classes:

Expand Down
114 changes: 25 additions & 89 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
//* DEPENDENCIES
//******************************************************************************
var gulp = require("gulp"),
browserify = require("browserify"),
tsify = require("tsify"),
source = require("vinyl-source-stream"),
buffer = require("vinyl-buffer"),
tslint = require("gulp-tslint"),
tsc = require("gulp-typescript"),
sourcemaps = require("gulp-sourcemaps"),
uglify = require("gulp-uglify"),
rename = require("gulp-rename"),
runSequence = require("run-sequence"),
header = require("gulp-header"),
mocha = require("gulp-mocha"),
istanbul = require("gulp-istanbul");

Expand All @@ -36,108 +28,57 @@ gulp.task("lint", function() {
//******************************************************************************
//* SOURCE
//******************************************************************************
var banner = ["/**",
" * <%= pkg.name %> v.<%= pkg.version %> - <%= pkg.description %>",
" * Copyright (c) 2015 <%= pkg.author %>",
" * <%= pkg.license %> inversify.io/LICENSE",
" * <%= pkg.homepage %>",
" */",
""].join("\n");
var pkg = require("./package.json");

gulp.task("build-bundle-src", function() {

var mainTsFilePath = "src/index.ts";
var outputFolder = "dist/";
var outputFileName = "index.js";

var bundler = browserify({
debug: true,
standalone : "inversifyBindingDecorators"
});

// TS compiler options are in tsconfig.json file
return bundler.add(mainTsFilePath)
.plugin(tsify)
.bundle()
.pipe(source(outputFileName))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(header(banner, { pkg : pkg } ))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(outputFolder));
});

gulp.task("build-bundle-compress-src", function() {

var mainTsFilePath = "src/index.ts";
var outputFolder = "dist/";
var outputFileName = "index.min.js";

var bundler = browserify({
debug: true,
standalone : "inversifyBindingDecorators"
});

// TS compiler options are in tsconfig.json file
return bundler.add(mainTsFilePath)
.plugin(tsify)
.bundle()
.pipe(source(outputFileName))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(header(banner, { pkg : pkg } ))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(outputFolder));
});

var tsLibProject = tsc.createProject("tsconfig.json", { module : "commonjs" });
var tsLibProject = tsc.createProject("tsconfig.json", { module : "commonjs", typescript: require("typescript") });

gulp.task("build-lib", function() {
return gulp.src([
"node_modules/inversify-dts/inversify/inversify.d.ts",
"node_modules/reflect-metadata/reflect-metadata.d.ts",
"typings/index.d.ts",
"src/**/*.ts"
])
.pipe(tsc(tsLibProject ))
.on("error", function (err) {
process.exit(1);
})
.js
.pipe(header(banner, { pkg : pkg } ))
.pipe(gulp.dest("lib/"));
.js.pipe(gulp.dest("lib/"));
});

var tsEsProject = tsc.createProject("tsconfig.json", { module : "es2015" });
var tsEsProject = tsc.createProject("tsconfig.json", { module : "es2015", typescript: require("typescript") });

gulp.task("build-es", function() {
return gulp.src([
"node_modules/inversify-dts/inversify/inversify.d.ts",
"node_modules/reflect-metadata/reflect-metadata.d.ts",
"typings/index.d.ts",
"src/**/*.ts"
])
.pipe(tsc(tsEsProject))
.on("error", function (err) {
process.exit(1);
})
.js
.pipe(header(banner, { pkg : pkg } ))
.pipe(gulp.dest("es/"));
.js.pipe(gulp.dest("es/"));
});

var tsDtsProject = tsc.createProject("tsconfig.json", {
declaration: true,
noExternalResolve: false,
typescript: require("typescript")
});

gulp.task("build-dts", function() {
return gulp.src([
"src/**/*.ts"
])
.pipe(tsc(tsDtsProject))
.on("error", function (err) {
process.exit(1);
})
.dts.pipe(gulp.dest("dts"));

});

//******************************************************************************
//* TESTS
//******************************************************************************
var tstProject = tsc.createProject("tsconfig.json");
var tstProject = tsc.createProject("tsconfig.json", { typescript: require("typescript") });

gulp.task("build-src", function() {
return gulp.src([
"node_modules/inversify-dts/inversify/inversify.d.ts",
"node_modules/reflect-metadata/reflect-metadata.d.ts",
"typings/index.d.ts",
"src/**/*.ts"
])
.pipe(tsc(tstProject))
Expand All @@ -147,13 +88,10 @@ gulp.task("build-src", function() {
.js.pipe(gulp.dest("src/"));
});

var tsTestProject = tsc.createProject("tsconfig.json");
var tsTestProject = tsc.createProject("tsconfig.json", { typescript: require("typescript") });

gulp.task("build-test", function() {
return gulp.src([
"node_modules/inversify-dts/inversify/inversify.d.ts",
"node_modules/reflect-metadata/reflect-metadata.d.ts",
"typings/index.d.ts",
"test/**/*.ts"
])
.pipe(tsc(tsTestProject))
Expand Down Expand Up @@ -187,9 +125,7 @@ gulp.task("test", function(cb) {
gulp.task("build", function(cb) {
runSequence(
"lint",
"build-bundle-src", // for nodejs
"build-bundle-compress-src", // for browsers
["build-src", "build-es", "build-lib"], // tests + build es and lib
["build-src", "build-es", "build-lib", "build-dts"], // tests + build es and lib
"build-test",
cb);
});
Expand Down
21 changes: 8 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "inversify-binding-decorators",
"version": "1.0.0",
"version": "2.0.0",
"description": "An utility that allows developers to declare InversifyJS bindings using ES2016 decorators",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
"typings": "./dts/inversify.d.ts",
"scripts": {
"test": "gulp",
"prepublish": "publish-please guard",
Expand All @@ -24,28 +25,22 @@
"homepage": "https://github.com/inversify/inversify-binding-decorators#readme",
"dependencies": {},
"devDependencies": {
"inversify": "^2.0.0-rc.12",
"inversify-dts": "^3.0.0",
"browserify": "^13.0.1",
"@types/chai": "^3.4.32",
"@types/mocha": "^2.2.31",
"@types/sinon": "^1.16.29",
"chai": "^3.5.0",
"gulp": "^3.9.1",
"gulp-header": "1.8.8",
"gulp-istanbul": "^1.0.0",
"gulp-mocha": "^3.0.0",
"gulp-rename": "^1.2.2",
"gulp-sourcemaps": "^1.6.0",
"gulp-tslint": "^6.0.2",
"gulp-typescript": "^2.13.4",
"gulp-uglify": "^2.0.0",
"harmonize": "^1.4.4",
"publish-please": "^2.1.4",
"inversify": "^2.0.0-rc.14",
"mocha": "^3.0.0",
"publish-please": "^2.1.4",
"reflect-metadata": "^0.1.3",
"run-sequence": "^1.2.0",
"sinon": "^1.17.4",
"tsify": "^1.0.2",
"tslint": "^3.12.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
"typescript": "^2.0.2"
}
}
8 changes: 5 additions & 3 deletions src/decorator/fluent_provide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import ProvideWhenSyntax from "../syntax/provide_when_syntax";
import ProvideOnSyntax from "../syntax/provide_on_syntax";
import ProvideInSyntax from "../syntax/provide_in_syntax";
import ProvideDoneSyntax from "../syntax/provide_done_syntax";
import interfaces from "../interfaces/interfaces";
import { interfaces as inversifyInterfaces } from "inversify";

function fluentProvide(kernel: inversify.interfaces.Kernel) {
function fluentProvide(kernel: inversifyInterfaces.Kernel) {

// function is named for testing
return function _fluentProvide(serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>)) {
return function _fluentProvide(serviceIdentifier: inversifyInterfaces.ServiceIdentifier<any>) {

let bindingWhenOnSyntax = kernel.bind<any>(serviceIdentifier).to(null);
let binding = (<any>bindingWhenOnSyntax)._binding;
let provideDoneSyntax = new ProvideDoneSyntax(binding);

let provideInWhenOnSyntax = new ProvideInWhenOnSyntax<any>(
let provideInWhenOnSyntax: interfaces.ProvideInWhenOnSyntax<any> = new ProvideInWhenOnSyntax<any>(
new ProvideInSyntax<any>(bindingWhenOnSyntax, provideDoneSyntax),
new ProvideWhenSyntax<any>(bindingWhenOnSyntax, provideDoneSyntax),
new ProvideOnSyntax<any>(bindingWhenOnSyntax, provideDoneSyntax)
Expand Down
7 changes: 4 additions & 3 deletions src/decorator/provide.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { decorate, injectable } from "inversify";
import { interfaces } from "inversify";

function provide(kernel: inversify.interfaces.Kernel) {
function provide(kernel: interfaces.Kernel) {

// function is named for testing
return function _provide(serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>)) {
return function _provide(serviceIdentifier: interfaces.ServiceIdentifier<any>) {
let bindingWhenOnSyntax = kernel.bind<any>(serviceIdentifier).to(null);
return function (target: any) {
decorate(injectable(), target);
let binding: inversify.interfaces.Binding<any> = (<any>bindingWhenOnSyntax)._binding;
let binding: interfaces.Binding<any> = (<any>bindingWhenOnSyntax)._binding;
binding.implementationType = target;
return target;
};
Expand Down
8 changes: 6 additions & 2 deletions src/factory/fluent_provide_decorator_factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fluentProvide from "../decorator/fluent_provide";
import interfaces from "../interfaces/interfaces";
import { interfaces as inversifyInterfaces } from "inversify";

function makeFluentProvideDecorator(kernel: inversify.interfaces.Kernel) {
return fluentProvide(kernel);
function makeFluentProvideDecorator(kernel: inversifyInterfaces.Kernel) {
let result: (serviceIdentifier: inversifyInterfaces.ServiceIdentifier<any>) => interfaces.ProvideInWhenOnSyntax<any> = null;
result = fluentProvide(kernel);
return result;
}

export default makeFluentProvideDecorator;
3 changes: 2 additions & 1 deletion src/factory/provide_decorator_factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import provide from "../decorator/provide";
import { interfaces } from "inversify";

function makeProvideDecorator(kernel: inversify.interfaces.Kernel) {
function makeProvideDecorator(kernel: interfaces.Kernel) {
return provide(kernel);
}

Expand Down
11 changes: 7 additions & 4 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { interfaces as inversifyInterfaces } from "inversify";

namespace interfaces {

export interface ProvideInSyntax<T> extends ProvideDoneSyntax<T> {
inSingletonScope(): ProvideWhenOnSyntax<T>;
inTransientScope(): ProvideWhenOnSyntax<T>;
}

export interface ProvideDoneSyntax<T> {
Expand All @@ -11,11 +14,11 @@ namespace interfaces {
export interface ProvideInWhenOnSyntax<T> extends ProvideInSyntax<T>, ProvideWhenSyntax<T>, ProvideOnSyntax<T> {}

export interface ProvideOnSyntax<T> extends ProvideDoneSyntax<T> {
onActivation(fn: (context: inversify.interfaces.Context, injectable: T) => T): ProvideWhenSyntax<T>;
onActivation(fn: (context: inversifyInterfaces.Context, injectable: T) => T): ProvideWhenSyntax<T>;
}

export interface ProvideWhenSyntax<T> extends ProvideDoneSyntax<T> {
when(constraint: (request: inversify.interfaces.Request) => boolean): ProvideOnSyntax<T>;
when(constraint: (request: inversifyInterfaces.Request) => boolean): ProvideOnSyntax<T>;
whenTargetNamed(name: string): ProvideOnSyntax<T>;
whenTargetTagged(tag: string, value: any): ProvideOnSyntax<T>;
whenInjectedInto(parent: (Function|string)): ProvideOnSyntax<T>;
Expand All @@ -27,8 +30,8 @@ namespace interfaces {
whenAnyAncestorTagged(tag: string, value: any): ProvideOnSyntax<T>;
whenNoAncestorNamed(name: string): ProvideOnSyntax<T>;
whenNoAncestorTagged(tag: string, value: any): ProvideOnSyntax<T>;
whenAnyAncestorMatches(constraint: (request: inversify.interfaces.Request) => boolean): ProvideOnSyntax<T>;
whenNoAncestorMatches(constraint: (request: inversify.interfaces.Request) => boolean): ProvideOnSyntax<T>;
whenAnyAncestorMatches(constraint: (request: inversifyInterfaces.Request) => boolean): ProvideOnSyntax<T>;
whenNoAncestorMatches(constraint: (request: inversifyInterfaces.Request) => boolean): ProvideOnSyntax<T>;
}

export interface ProvideWhenOnSyntax<T> extends ProvideWhenSyntax<T>, ProvideOnSyntax<T> {}
Expand Down
Loading

0 comments on commit f3cdd21

Please sign in to comment.