Skip to content

Commit

Permalink
Merge branch 'hotfix/3.5.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
kievechua committed Feb 24, 2017
2 parents ce72ba8 + 46e9c75 commit 9af8e88
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
7 changes: 4 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "magnet-core",
"version": "3.5.1",
"version": "3.5.2",
"description": "Magnet's core, a simple module loader.",
"keywords": [
"core",
Expand All @@ -21,7 +21,7 @@
"repository": "magnetjs/magnet-core",
"scripts": {
"build": "npm run clean-lib && npm run build:index && npm run copy-package",
"build:index": "tsc",
"build:index": "node_modules/.bin/tsc",
"clean-lib": "node_modules/.bin/rimraf ./dist",
"copy-package": "cp -rf package.json LICENSE README.md dist",
"dist": "npm run build && cd dist && npm publish",
Expand All @@ -42,7 +42,8 @@
"ava": "^0.18.1",
"concurrently": "^3.1.0",
"rimraf": "^2.5.4",
"tap-spec": "^4.1.1"
"tap-spec": "^4.1.1",
"typescript": "^2.2.1"
},
"engines": {
"node": ">=6.9.5"
Expand Down
25 changes: 17 additions & 8 deletions src/convert.ts
Expand Up @@ -14,14 +14,23 @@ import { App } from './app'
* @param {[array, function]} options.params Will pass to initializer
* @return void
*/
export default function convert (module, { namespace, initializer, params, teardown }, defaultConfig = {}): any {
export default function convert (module, convertOptions, defaultConfig = {}): any {
const { initializer, params, teardown } = convertOptions
const moduleName = convertOptions.namespace

return class ConvertMagnetModule extends Module {
// Set class name
// http://stackoverflow.com/a/41787315/788518
static get name () { return namespace }
// static get name () { return namespace }

constructor (app: App, options: any = {}) {
super(app, options)

this.name = moduleName
}

async setup (): Promise<void> {
const config = this.setConfig(namespace, defaultConfig)
const config = this.setConfig(moduleName, defaultConfig)

// Prepare parameters
let moduleParams = []
Expand All @@ -43,16 +52,16 @@ export default function convert (module, { namespace, initializer, params, teard

const initialize = module[initializer] || module
if (isClass(initialize)) {
this.app[namespace] = new initialize(...moduleParams)
this.app[moduleName] = new initialize(...moduleParams)
} else {
this.app[namespace] = initialize(...moduleParams)
this.app[moduleName] = initialize(...moduleParams)
}
}

async teardown (): Promise<void> {
if (teardown && this.app[namespace] && this.app[namespace][teardown]) {
this.app[namespace][teardown]()
this.log.info(`${namespace} teardown completed`)
if (teardown && this.app[moduleName] && this.app[moduleName][teardown]) {
this.app[moduleName][teardown]()
this.log.info(`${moduleName} teardown completed`)
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/module.ts
Expand Up @@ -11,6 +11,7 @@ export abstract class Module {
log: LogAbstract
config: any
options: any
private _name: string;

constructor (app: App, options: any = {}) {
this.app = app
Expand All @@ -19,8 +20,12 @@ export abstract class Module {
this.options = options
}

getName (): string {
return this.constructor.name
get name (): string {
return this._name || this.constructor.name
}

set name (newName: string) {
this._name = newName;
}

// depreciated, use getConfig
Expand All @@ -34,7 +39,7 @@ export abstract class Module {
}

async setup (): Promise<void> {
this.log.warn(`Module ${this.getName()} missing setup function`)
this.log.warn(`Module ${this.name} missing setup function`)
}

async teardown (): Promise<any> {}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"sourceMap": true,
"declaration": true,
"skipLibCheck": true,
"target": "es6",
"moduleResolution": "node",
"module": "commonjs",
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -2690,6 +2690,10 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"

typescript@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.2.1.tgz#4862b662b988a4c8ff691cc7969622d24db76ae9"

uid-number@~0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"
Expand Down

0 comments on commit 9af8e88

Please sign in to comment.