Skip to content

Commit

Permalink
feat(): adds to spool configuration
Browse files Browse the repository at this point in the history
- #45
  • Loading branch information
scott-wyatt committed Oct 14, 2019
1 parent 857ad57 commit 81fd1c3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export const Core = {
},

/**
* Bind listeners to fabrix application events
* Bind listeners to FabrixApp events
*/
bindApplicationListeners (app: FabrixApp): void {
app.once('spool:all:configured', () => {
Expand Down Expand Up @@ -386,6 +386,7 @@ export const Core = {
})
app.once('fabrix:stop', () => {
app.log.info(Templates.info.stop)
// Unfreezes the config so that modifications can be made before starting again
app.config.unfreeze()
})
},
Expand Down
29 changes: 22 additions & 7 deletions lib/Fabrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { union } from 'lodash'
import { Core } from './Core'
import { Configuration } from './Configuration'
import { LoggerProxy } from './LoggerProxy'
import { Spool, IApi, IPkg, IConfig, IEnv } from './common'
import { Spool, IApi, IPkg, IConfig, IEnv, IVersions } from './common'
import * as Errors from './errors'
import * as pkg from '../package.json'
import { FabrixGeneric } from './common/Generic'
Expand All @@ -13,8 +13,8 @@ import { DatastoreSpool } from './common/spools/datastore'
import { SystemSpool } from './common/spools/system'
import { ToolSpool } from './common/spools/tool'
import { MiscSpool } from './common/spools/misc'

import { enumerable } from './common/decorators/enumerable'
import { IVersions } from './common/interfaces/IVersions'

// inject Error and Resource types into the global namespace
// Deprecate Globals v1.6
Expand All @@ -28,15 +28,23 @@ export interface FabrixApp extends EventEmitter {
[key: string]: any
}

export interface ISpools {
[key: string]: Spool | ServerSpool | ExtensionSpool | DatastoreSpool | SystemSpool | ToolSpool | MiscSpool
}

export interface IApis {
[key: string]: FabrixGeneric | {}
}

export class FabrixApp extends EventEmitter {
private _logger: LoggerProxy
private _env: IEnv
private _pkg: any // IPkg
private _pkg: IPkg
private _versions: IVersions
private _config: Configuration
private _api: IApi
private _fabrix: FabrixApp
private _spools: {[key: string]: Spool | ServerSpool | ExtensionSpool | DatastoreSpool | SystemSpool | ToolSpool | MiscSpool }
private _spools: ISpools
private _resources: string[] = [ ]

// Deprecate Globals v1.6
Expand Down Expand Up @@ -80,6 +88,7 @@ export class FabrixApp extends EventEmitter {
// ensure process.env is an immutable object
const processEnv = Object.freeze(Object.assign({}, JSON.parse(JSON.stringify(process.env))))

// Define some essential properties/methods to the FabrixApp instance
Object.defineProperties(this, {
_logger: {
value: new LoggerProxy(this),
Expand Down Expand Up @@ -118,14 +127,17 @@ export class FabrixApp extends EventEmitter {
// Set the max listeners from the config
this.setMaxListeners(this.config.get('main.maxListeners'))

// Emit the event that the fabrix app is constructing
this.emit('fabrix:constructing')

// Set the resources from the configuration (this bypasses the setter with the initial config
// in case the resourceLock is configured)
this._resources = this.config.get('main.resources')
// See if additional resources can be set
this.resources = union(Object.keys(app.api), this.config.get('main.resources'))

// Set each api resource to make sure it's provided as an object in the app
this.resources.forEach(resource => {
this.resources.forEach((resource: string) => {
app.api[resource] = app.api[resource] || (app.api[resource] = { })
})

Expand All @@ -148,6 +160,8 @@ export class FabrixApp extends EventEmitter {
}
catch (e) {
console.log(e.stack)
// SBW: we may either need to remove the error being thrown and pass it to stop
// this.stop(e)
throw new Errors.SpoolError(Spool, e, 'constructor')
}
})
Expand All @@ -161,6 +175,7 @@ export class FabrixApp extends EventEmitter {
// Bind the Phase listeners for the Spool lifecycle
Core.bindSpoolPhaseListeners(this, Object.values(this.spools))

// Emit the event that the fabrix app was successfully constructed
this.emit('fabrix:constructed')
}

Expand Down Expand Up @@ -201,15 +216,15 @@ export class FabrixApp extends EventEmitter {
* Gets the Spools that have been installed
*/
// @enumerable(false)
get spools (): {[key: string]: Spool} {
get spools (): ISpools {
return this._spools
}

/**
* Gets the api
*/
// @enumerable(false)
get api (): {[key: string]: FabrixGeneric | {}} {
get api (): IApis {
return this._api
}

Expand Down
37 changes: 33 additions & 4 deletions lib/common/Spool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Spool extends FabrixGeneric {
}

export class Spool extends FabrixGeneric {
private _address: string
private _stage = 'pre'
private _config: ISpoolConfig
private _pkg: any // IPkg
Expand Down Expand Up @@ -50,6 +51,10 @@ export class Spool extends FabrixGeneric {
*/
static get defaultLifecycle (): ILifecycle {
return {
validate: {
listen: [ ],
emit: [ ]
},
configure: {
listen: [ ],
emit: [ ]
Expand All @@ -65,11 +70,14 @@ export class Spool extends FabrixGeneric {
}
}

static configuredSpoolLifecycle (config) {
static configuredSpoolLifecycle (config: ISpoolConfig) {
const level1 = config.lifecycle || {}
const level2 = config.spool && config.spool.lifecycle
? config.spool.lifecycle : config.trailpack && config.trailpack.lifecycle
? config.trailpack.lifecycle : {}
? config.spool.lifecycle
: config.trailpack && config.trailpack.lifecycle
? config.trailpack.lifecycle
: {}

const level3 = Spool.defaultLifecycle
return defaultsDeep({}, level1, level2, level3)
}
Expand Down Expand Up @@ -147,12 +155,15 @@ export class Spool extends FabrixGeneric {
this._config = config
}

/**
* Virtual getter for `pkg`
*/
get pkg () {
return this._pkg
}

/**
* Return a reference to the Fabrix logger
* Return a reference to the Fabrix logger, so you can use `this.log` instead of this.app.log
*/
get log (): FabrixApp['log'] {
return this.app.log
Expand Down Expand Up @@ -193,6 +204,15 @@ export class Spool extends FabrixGeneric {

}

/**
* Once the FabrixApp is bootstrapped and ready, the spool can run additional logic
* By defining a function here. However, config is now immutable so this should not
* set any configuration.
*/
ready (): any {

}

/**
* Unload this Spool. This method will instruct the spool to perform
* any necessary cleanup with the expectation that the app will stop or reload
Expand All @@ -208,6 +228,8 @@ export class Spool extends FabrixGeneric {
* Return the name of this Spool. By default, this is the name of the
* npm module (in package.json). This method can be overridden for spools
* which do not follow the "spool-" prefix naming convention.
* Some Trailpacks will just work in fabrix without modifying anything,
* In which case, they will have the "trailpack-" prefix which is also removed
*/
get name (): string {
return this.pkg.name
Expand All @@ -223,4 +245,11 @@ export class Spool extends FabrixGeneric {
}


/**
* Vitual Getter for the "address" for this spool if the app is distributed
*/
get address (): string {
return this._address
}

}
1 change: 1 addition & 0 deletions lib/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export { ISpoolConfig } from './interfaces/ISpoolConfig'
export { IPkg } from './interfaces/IPkg'
export { ILifecycle } from './interfaces/ILifecycle'
export { IEnv } from './interfaces/IEnv'
export { IVersions } from './interfaces/IVersions'
1 change: 1 addition & 0 deletions lib/common/interfaces/IConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export interface IConfig {
main: {
[key: string]: any,
spools: any[] // typeof Spool[]
target?: string
}
}
6 changes: 5 additions & 1 deletion lib/common/interfaces/ILifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface ILifecycle {
validate?: {
listen: string[],
emit: string[]
},
configure: {
listen: string[],
emit: string[]
Expand All @@ -7,7 +11,7 @@ export interface ILifecycle {
listen: string[],
emit: string[]
},
sanity: {
sanity?: {
listen: string[],
emit: string[]
}
Expand Down

0 comments on commit 81fd1c3

Please sign in to comment.