From 3f752b9be7ab30c1d9310cdc489c00ba70dcec3b Mon Sep 17 00:00:00 2001 From: Daniel Schuech Date: Tue, 31 May 2016 08:18:41 +0200 Subject: [PATCH 1/3] feat: support typescript --- README.md | 25 +++++++++++++++++++++++++ package.json | 5 +++-- src/injectable.ts | 6 ++++++ tiny-di.d.ts | 37 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 16 ++++++++++++++++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/injectable.ts create mode 100644 tiny-di.d.ts create mode 100644 tsconfig.json diff --git a/README.md b/README.md index fc22e06..09d3c04 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,31 @@ function Module(app, something, another) { look at the `example`-folder for a simple howto +# usage with typescript + +import the definition file +```javascript +/// +``` + +import tiny-di and do some magic stuff +```javascript +import tinyDi = require('tiny-di'); +let injector = new tinyDi(); +``` + +modules which use the $inject configuration have to inherit from TinyDiInjectableBase +```javascript +import TinyDiInjectableBase from 'tiny-di/dist/injectable'; + +export default class MyModule extends TinyDiInjectableBase {} +MyModule.$inject = { + deps: [], + callAs: 'class' +}; +``` + + # develop ```javascript diff --git a/package.json b/package.json index a1341e0..2248f06 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "clean": "rimraf dist", "test": "istanbul cover jasmine", "test-travis": "istanbul cover jasmine && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", - "build": "gulp", + "build": "gulp && tsc", "watch": "gulp watch", "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, @@ -42,7 +42,8 @@ "proxyquire": "1.7.3", "rimraf": "2.4.3", "source-map-support": "0.3.2", - "semantic-release": "^4.3.5" + "semantic-release": "^4.3.5", + "typescript": "^1.8.10" }, "dependencies": { "babel-runtime": "5.8.25" diff --git a/src/injectable.ts b/src/injectable.ts new file mode 100644 index 0000000..fc20443 --- /dev/null +++ b/src/injectable.ts @@ -0,0 +1,6 @@ +export default class TinyDiInjectableBase { + public static $inject: string[] | { + deps: string[], + callAs: 'class' | 'function' + }; +} diff --git a/tiny-di.d.ts b/tiny-di.d.ts new file mode 100644 index 0000000..336e355 --- /dev/null +++ b/tiny-di.d.ts @@ -0,0 +1,37 @@ +declare module "tiny-di" { + export = TinyDiInjector; +} + +declare class TinyDiInjector { + bind(name: string): { + to(module: any): TinyDiInjector, + load(module: any): TinyDiInjector, + lazy(module: any): TinyDiInjector + }; + + ns(namespace: string): { + to(newNamespace: string): TinyDiInjector + } + + get(module: string): any + + setResolver(resolver: Function): void; + + provide(provider: string): { + by(somethingProvider: any): TinyDiInjector + } + + setLogger(logger: any): void; + + set(key: string, object: any): any; + + //Helper + /*getDefaultResolver(req: any): any; + resolveKey(key: string): string; + hasBindings(key): boolean; + setNsBinding(ns: string, dir:string): void; + load(key: string, what: any): any; + lazy(key: string): any; + ... + */ +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..9bffa8e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "noImplicitAny": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "module": "commonjs", + "sourceMap": true, + "declaration": true, + "target": "es5", + "outDir": "dist" + }, + + "exclude": [ + "node_modules" + ] +} From ec2985b74ae7b55e131b5e86afd24aceafa130e0 Mon Sep 17 00:00:00 2001 From: Daniel Schuech Date: Fri, 8 Jul 2016 09:45:37 +0200 Subject: [PATCH 2/3] chore: tests can now be written in typescript --- .gitignore | 1 + .npmignore | 1 + package.json | 12 ++++++++---- spec/support/jasmine.json | 2 +- tsconfig.json | 4 +++- tslint.json | 33 +++++++++++++++++++++++++++++++++ typings.json | 6 ++++++ 7 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 tslint.json create mode 100644 typings.json diff --git a/.gitignore b/.gitignore index 898b527..99af3a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules coverage/ dist +typings diff --git a/.npmignore b/.npmignore index 9741012..7f1defd 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ src spec coverage +typings diff --git a/package.json b/package.json index 2248f06..6ad867c 100644 --- a/package.json +++ b/package.json @@ -6,11 +6,13 @@ "scripts": { "prepublish": "npm run clean && npm run build && npm test", "clean": "rimraf dist", - "test": "istanbul cover jasmine", + "test": "typings install && npm run prepare-spec && istanbul cover jasmine && npm run clean-spec", "test-travis": "istanbul cover jasmine && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", "build": "gulp && tsc", "watch": "gulp watch", - "semantic-release": "semantic-release pre && npm publish && semantic-release post" + "semantic-release": "semantic-release pre && npm publish && semantic-release post", + "prepare-spec": "cd spec && copyfiles **/*.spec.js *.spec.js ../buildspec && cd .. && tsc -p tsconfig.spec.json", + "clean-spec": "rimraf buildspec" }, "repository": { "type": "git", @@ -28,6 +30,7 @@ "author": "Dennis Saenger ", "license": "MIT", "devDependencies": { + "copyfiles": "^1.0.0", "coveralls": "2.11.4", "git-changelog": "0.1.8", "grunt": "0.4.5", @@ -41,9 +44,10 @@ "jasmine": "2.3.2", "proxyquire": "1.7.3", "rimraf": "2.4.3", - "source-map-support": "0.3.2", "semantic-release": "^4.3.5", - "typescript": "^1.8.10" + "source-map-support": "0.3.2", + "typescript": "^1.8.10", + "typings": "^1.3.1" }, "dependencies": { "babel-runtime": "5.8.25" diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json index a5f2932..20e0a07 100644 --- a/spec/support/jasmine.json +++ b/spec/support/jasmine.json @@ -1,7 +1,7 @@ { "spec_dir": "spec", "spec_files": [ - "**/*[sS]pec.js" + "../buildspec/**/*[sS]pec.js" ], "helpers": [ "helpers/**/*.js" diff --git a/tsconfig.json b/tsconfig.json index 9bffa8e..176b621 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,8 @@ }, "exclude": [ - "node_modules" + "node_modules", + "spec", + "dist" ] } diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..0edab6c --- /dev/null +++ b/tslint.json @@ -0,0 +1,33 @@ +{ + "rules": { + "class-name": true, + "indent": [true, "spaces"], + "no-duplicate-variable": true, + "no-eval": true, + "no-internal-module": true, + "no-trailing-whitespace": true, + "no-var-keyword": true, + "one-line": true, + "quotemark": [true, "single"], + "semicolon": true, + "triple-equals": true, + "typedef-whitespace": [true, { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + }], + "variable-name": [true, "ban-keywords"], + "whitespace": [true, + "check-branch", + "check-decl", + "check-operator", + "check-separator", + "check-type" + ], + "eofline": true, + "max-line-length": [true, 100], + "no-consecutive-blank-lines": true + } +} \ No newline at end of file diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..802fc37 --- /dev/null +++ b/typings.json @@ -0,0 +1,6 @@ +{ + "globalDependencies": { + "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", + "node": "registry:dt/node#6.0.0+20160621231320" + } +} From cee725271ca232e792adc4525dad63a932fd0b05 Mon Sep 17 00:00:00 2001 From: Daniel Schuech Date: Fri, 8 Jul 2016 09:46:23 +0200 Subject: [PATCH 3/3] test: test typescript usage --- spec/exampleclass.ts | 11 +++++++++++ spec/typescript.spec.ts | 28 ++++++++++++++++++++++++++++ src/injectable.ts | 2 +- tsconfig.spec.json | 18 ++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 spec/exampleclass.ts create mode 100644 spec/typescript.spec.ts create mode 100644 tsconfig.spec.json diff --git a/spec/exampleclass.ts b/spec/exampleclass.ts new file mode 100644 index 0000000..0428bc1 --- /dev/null +++ b/spec/exampleclass.ts @@ -0,0 +1,11 @@ +import TinyDiInjectableBase from '../dist/injectable'; + +export default class MyClass extends TinyDiInjectableBase { + constructor(private dep: any) { + super(); + } +} +MyClass.$inject = { + deps: ['dependency'], + callAs: 'class' +}; diff --git a/spec/typescript.spec.ts b/spec/typescript.spec.ts new file mode 100644 index 0000000..0b6058e --- /dev/null +++ b/spec/typescript.spec.ts @@ -0,0 +1,28 @@ +/// +import TinyDiInjectableBase from '../dist/injectable'; + +describe('typescript class', () => { + it('can define $inject property', () => { + class MyClass extends TinyDiInjectableBase {} + MyClass.$inject = ['dependency']; + expect(MyClass.$inject).toBeDefined(); + }); + + it('can be loaded by tiny-di', () => { + let tiny = require('../dist'); + let injector: TinyDiInjector = new tiny(); + injector.setResolver(resolver); + injector.bind('dependency').to('exampleDep'); + injector.bind('myClassInstance').load(__dirname + '/exampleclass.js'); + + let instance = injector.get('myClassInstance'); + expect(instance.dep).toEqual('exampleDep'); + }); +}); + +/** + * define resolver to load default exported classes + */ +function resolver(module: string) { + return require(module).default; +} diff --git a/src/injectable.ts b/src/injectable.ts index fc20443..867056a 100644 --- a/src/injectable.ts +++ b/src/injectable.ts @@ -1,5 +1,5 @@ export default class TinyDiInjectableBase { - public static $inject: string[] | { + public static $inject: string[] | { deps: string[], callAs: 'class' | 'function' }; diff --git a/tsconfig.spec.json b/tsconfig.spec.json new file mode 100644 index 0000000..d20422a --- /dev/null +++ b/tsconfig.spec.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "noImplicitAny": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "module": "commonjs", + "sourceMap": true, + "declaration": true, + "target": "es5", + "outDir": "buildspec" + }, + + "exclude": [ + "node_modules", + "src", + "buildspec" + ] +}