-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boot): add a booter for life cycle scripts
See #2034
- Loading branch information
1 parent
4ce4693
commit fff0b34
Showing
6 changed files
with
159 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/boot | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Constructor, inject, createBindingFromClass} from '@loopback/context'; | ||
import { | ||
Application, | ||
asLifeCycleObserverBinding, | ||
CoreBindings, | ||
isLifeCycleObserverClass, | ||
LifeCycleObserver, | ||
CoreTags, | ||
} from '@loopback/core'; | ||
import {ArtifactOptions} from '../interfaces'; | ||
import {BootBindings} from '../keys'; | ||
import {BaseArtifactBooter} from './base-artifact.booter'; | ||
|
||
import * as debugFactory from 'debug'; | ||
const debug = debugFactory('loopback:boot:lifecycle-observer-booter'); | ||
|
||
type LifeCycleObserverClass = Constructor<LifeCycleObserver>; | ||
|
||
/** | ||
* A class that extends BaseArtifactBooter to boot the 'LifeCycleObserver' artifact type. | ||
* | ||
* Supported phases: configure, discover, load | ||
* | ||
* @param app Application instance | ||
* @param projectRoot Root of User Project relative to which all paths are resolved | ||
* @param [bootConfig] LifeCycleObserver Artifact Options Object | ||
*/ | ||
export class LifeCycleObserverBooter extends BaseArtifactBooter { | ||
observers: LifeCycleObserverClass[]; | ||
|
||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) | ||
public app: Application, | ||
@inject(BootBindings.PROJECT_ROOT) projectRoot: string, | ||
@inject(`${BootBindings.BOOT_OPTIONS}#observers`) | ||
public observerConfig: ArtifactOptions = {}, | ||
) { | ||
super( | ||
projectRoot, | ||
// Set LifeCycleObserver Booter Options if passed in via bootConfig | ||
Object.assign({}, LifeCycleObserverDefaults, observerConfig), | ||
); | ||
} | ||
|
||
/** | ||
* Uses super method to get a list of Artifact classes. Boot each file by | ||
* creating a DataSourceConstructor and binding it to the application class. | ||
*/ | ||
async load() { | ||
await super.load(); | ||
|
||
this.observers = this.classes.filter(isLifeCycleObserverClass); | ||
for (const observer of this.observers) { | ||
debug('Bind life cycle observer: %s', observer.name); | ||
const binding = createBindingFromClass(observer, { | ||
namespace: CoreBindings.LIFE_CYCLE_OBSERVERS, | ||
type: CoreTags.LIFE_CYCLE_OBSERVER, | ||
}).apply(asLifeCycleObserverBinding); | ||
this.app.add(binding); | ||
debug('Binding created for life cycle observer: %j', binding); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Default ArtifactOptions for DataSourceBooter. | ||
*/ | ||
export const LifeCycleObserverDefaults: ArtifactOptions = { | ||
dirs: ['observers'], | ||
extensions: ['.observer.js'], | ||
nested: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/boot/test/fixtures/lifecycle-observer.artifact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/boot | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {LifeCycleObserver} from '@loopback/core'; | ||
|
||
export class MyLifeCycleObserver implements LifeCycleObserver { | ||
status = ''; | ||
|
||
async start() { | ||
this.status = 'started'; | ||
} | ||
|
||
stop() { | ||
this.status = 'stopped'; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/boot/test/integration/lifecycle-observer.booter.integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/boot | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
BindingScope, | ||
ContextTags, | ||
CoreBindings, | ||
CoreTags, | ||
} from '@loopback/core'; | ||
import {expect, TestSandbox} from '@loopback/testlab'; | ||
import {resolve} from 'path'; | ||
import {BooterApp} from '../fixtures/application'; | ||
|
||
describe('lifecycle script booter integration tests', () => { | ||
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox'); | ||
const sandbox = new TestSandbox(SANDBOX_PATH); | ||
|
||
const OBSERVER_PREFIX = CoreBindings.LIFE_CYCLE_OBSERVERS; | ||
const OBSERVER_TAG = CoreTags.LIFE_CYCLE_OBSERVER; | ||
|
||
let app: BooterApp; | ||
|
||
beforeEach('reset sandbox', () => sandbox.reset()); | ||
beforeEach(getApp); | ||
|
||
it('boots life cycle observers when app.boot() is called', async () => { | ||
const expectedBinding = { | ||
key: `${OBSERVER_PREFIX}.MyLifeCycleObserver`, | ||
tags: [ContextTags.TYPE, OBSERVER_TAG], | ||
scope: BindingScope.SINGLETON, | ||
}; | ||
|
||
await app.boot(); | ||
|
||
const bindings = app | ||
.findByTag(OBSERVER_TAG) | ||
.map(b => ({key: b.key, tags: b.tagNames, scope: b.scope})); | ||
expect(bindings).to.containEql(expectedBinding); | ||
}); | ||
|
||
async function getApp() { | ||
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); | ||
await sandbox.copyFile( | ||
resolve(__dirname, '../fixtures/lifecycle-observer.artifact.js'), | ||
'observers/lifecycle-observer.observer.js', | ||
); | ||
|
||
const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp; | ||
app = new MyApp(); | ||
} | ||
}); |