-
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: initial implementation of boot + ControllerBooter
Initial booter. WIP.
- Loading branch information
Showing
26 changed files
with
1,278 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.tgz | ||
dist* | ||
package |
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 @@ | ||
package-lock=false |
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,5 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "6" | ||
- "8" |
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,25 @@ | ||
Copyright (c) IBM Corp. 2018. All Rights Reserved. | ||
Node module: @loopback/boot | ||
This project is licensed under the MIT License, full text below. | ||
|
||
-------- | ||
|
||
MIT license | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,84 @@ | ||
# @loopback/boot | ||
|
||
A collection of Booters for LoopBack Applications | ||
|
||
# Overview | ||
|
||
A Booter is a Class that can be bound to an Application and is called | ||
to perform a task before the Application is started. A Booter may have multiple | ||
phases to complete it's task. | ||
|
||
An example task of a Booter may be to discover and bind all artifacts of a | ||
given type. | ||
|
||
## Installation | ||
|
||
```shell | ||
$ npm i @loopback/boot | ||
``` | ||
|
||
## Basic Use | ||
|
||
```ts | ||
import {ControllerBooter} from '@loopback/boot'; | ||
app.booter(ControllerBooter); // register booter | ||
await app.boot(); // Booter gets run by the Application | ||
``` | ||
|
||
## Available Booters | ||
|
||
### ControllerBooter | ||
|
||
#### Description | ||
Discovers and binds Controller Classes using `app.controller()`. | ||
|
||
#### Options | ||
The Options for this can be passed via `ApplicationConfig` in the Application | ||
constructor or via `BootOptions` when calling `app.boot(options:BootOptions)`. | ||
|
||
The options for this are passed in a `controllers` object on `boot`. | ||
|
||
Available Options on the `boot.controllers` are as follows: | ||
|Options|Type|Default|Description| | ||
|-|-|-|-| | ||
|`dirs`|`string | string[]`|`['controllers']`|Paths relative to projectRoot to look in for Controller artifacts| | ||
|`extensions`|`string | string[]`|`['.controller.js']`|File extensions to match for Controller artifacts| | ||
|`nested`|`boolean`|`true`|Look in nested directories in `dirs` for Controller artifacts| | ||
|
||
#### Examples | ||
**Via Application Config** | ||
```ts | ||
new Application({ | ||
boot: { | ||
projectRoot: '', | ||
controllers: {...} | ||
} | ||
}); | ||
``` | ||
|
||
**Via BootOptions** | ||
```ts | ||
app.boot({ | ||
boot: { | ||
projectRoot: '', | ||
controllers: {...} | ||
} | ||
}); | ||
``` | ||
|
||
## Contributions | ||
|
||
- [Guidelines](https://github.com/strongloop/loopback-next/wiki/Contributing#guidelines) | ||
- [Join the team](https://github.com/strongloop/loopback-next/issues/110) | ||
|
||
## Tests | ||
|
||
Run `npm test` from the root folder. | ||
|
||
## Contributors | ||
|
||
See [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
||
## License | ||
|
||
MIT |
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,8 @@ | ||
{ | ||
"content": ["index.ts", "src/index.ts", "src/controller.booter.ts"], | ||
"codeSectionDepth": 4, | ||
"assets": { | ||
"/": "/docs", | ||
"/docs": "/docs" | ||
} | ||
} |
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,6 @@ | ||
// Copyright IBM Corp. 2013,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 | ||
|
||
export * from './dist/src'; |
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,8 @@ | ||
// Copyright IBM Corp. 2013,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 | ||
|
||
const nodeMajorVersion = +process.versions.node.split('.')[0]; | ||
module.exports = | ||
nodeMajorVersion >= 7 ? require('./dist/src') : require('./dist6/src'); |
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,6 @@ | ||
// Copyright IBM Corp. 2013,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 | ||
|
||
export * from './src'; |
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,50 @@ | ||
{ | ||
"name": "@loopback/boot", | ||
"version": "4.0.0-alpha.1", | ||
"description": "A collection of Booters for LoopBack 4 Applications", | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"acceptance": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/acceptance/**/*.js'", | ||
"build": "npm run build:dist && npm run build:dist6", | ||
"build:current": "lb-tsc", | ||
"build:dist": "lb-tsc es2017", | ||
"build:dist6": "lb-tsc es2015", | ||
"build:apidocs": "lb-apidocs", | ||
"clean": "lb-clean loopback-core*.tgz dist dist6 package api-docs", | ||
"prepare": "npm run build && npm run build:apidocs", | ||
"pretest": "npm run build:current", | ||
"integration": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/integration/**/*.js'", | ||
"test": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js' 'DIST/test/integration/**/*.js' 'DIST/test/acceptance/**/*.js'", | ||
"unit": "lb-dist mocha --opts ../../test/mocha.opts 'DIST/test/unit/**/*.js'", | ||
"verify": "npm pack && tar xf loopback-core*.tgz && tree package && npm run clean" | ||
}, | ||
"author": "IBM", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@loopback/context": "^4.0.0-alpha.24", | ||
"@loopback/core": "^4.0.0-alpha.26", | ||
"@types/glob": "^5.0.34", | ||
"glob": "^7.1.2" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^4.0.0-alpha.7", | ||
"@loopback/openapi-v2": "^4.0.0-alpha.3", | ||
"@loopback/rest": "^4.0.0-alpha.18", | ||
"@loopback/testlab": "^4.0.0-alpha.17" | ||
}, | ||
"files": [ | ||
"README.md", | ||
"index.js", | ||
"index.d.ts", | ||
"dist/src", | ||
"dist6/src", | ||
"api-docs", | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/strongloop/loopback-next.git" | ||
} | ||
} |
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,129 @@ | ||
// Copyright IBM Corp. 2013,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 {CoreBindings, Application, Booter, BootOptions} from '@loopback/core'; | ||
import {inject} from '@loopback/context'; | ||
import * as glob from 'glob'; | ||
|
||
/** | ||
* ControllerBooter is a class that implements the Booter inferface. The purpose | ||
* of this booter is to allow for convention based booting of `Controller` | ||
* artifacts for LoopBack 4 Applications. | ||
* | ||
* It supports the following boot phases: config, discover, boot | ||
*/ | ||
export class ControllerBooter implements Booter { | ||
options: ControllerOptions; | ||
projectRoot: string; | ||
|
||
/** | ||
* | ||
* @param app Application instance | ||
* @param bootConfig Config options for boot | ||
*/ | ||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) public app: Application, | ||
@inject(CoreBindings.BOOT_CONFIG) public bootConfig: BootOptions, | ||
) { | ||
if (!bootConfig.controllers) bootConfig.controllers = {}; | ||
this.options = bootConfig.controllers; | ||
this.projectRoot = bootConfig.projectRoot; | ||
} | ||
|
||
/** | ||
* This phase is responsible for configuring this Booter. It converts | ||
* options and assigns default values for missing values so ther phases | ||
* don't have to perform checks / conversions. | ||
*/ | ||
async config() { | ||
this.options = Object.assign({}, ControllerDefaults, this.options); | ||
if (typeof this.options.dirs === 'string') { | ||
this.options.dirs = [this.options.dirs]; | ||
} | ||
if (typeof this.options.extensions === 'string') { | ||
this.options.extensions = [this.options.extensions]; | ||
} | ||
} | ||
|
||
/** | ||
* This phase is responsible for discovering artifact files based on the | ||
* given parameters. Sets options.discovered to an array of discovered | ||
* artifact files. | ||
*/ | ||
async discover() { | ||
const dirs = <string[]>this.options.dirs; | ||
const exts = <string[]>this.options.extensions; | ||
|
||
// glob pattern | ||
let pattern = `/@(${dirs.join('|')})/`; | ||
pattern += this.options.nested ? '**/*' : '*'; | ||
pattern += `@(${exts.join('|')})`; | ||
|
||
this.options.discovered = glob.sync(pattern, {root: this.projectRoot}); | ||
} | ||
|
||
/** | ||
* This phase is responsible for reading the discovered files, checking the | ||
* Classes exported and binding them to the Application instance for use. | ||
* | ||
* It will skip any files it isn't able to load and throw an error containing | ||
* a list of skipped files. Other files that were read will still be bound | ||
* to the Application instance. | ||
*/ | ||
async boot() { | ||
const files: string[] = this.options.discovered; | ||
let errFiles: string[] = []; | ||
files.forEach(file => { | ||
try { | ||
const ctrl = require(file); | ||
const classes: string[] = Object.keys(ctrl); | ||
classes.forEach(cls => { | ||
this.app.controller(ctrl[cls]); | ||
}); | ||
} catch (err) { | ||
errFiles.push(file.substring(this.projectRoot.length)); | ||
} | ||
}); | ||
|
||
// Only throw 1 error. Allows user to catch it and ignore if needed | ||
if (errFiles.length > 0) { | ||
throw new Error( | ||
`ControllerBooter failed to load the following files: ${JSON.stringify( | ||
errFiles, | ||
)}`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Type definition for ControllerOptions. These are the options supported by | ||
* this Booter. | ||
* | ||
* @param dirs String / String Array of directories to check for artifacts. | ||
* Paths must be relative. Defaults to ['controllers'] | ||
* @param extensions String / String Array of file extensions to match artifact | ||
* files in dirs. Defaults to ['.controller.js'] | ||
* @param nested Boolean to control if artifact discovery should check nested | ||
* folders or not. Default to true | ||
* @param discovered An array of discovered files. This is set by the | ||
* discover phase. | ||
*/ | ||
export type ControllerOptions = { | ||
dirs: string | string[]; | ||
extensions: string | string[]; | ||
discovered: string[]; | ||
nested: boolean; | ||
}; | ||
|
||
/** | ||
* Default values for ControllerOptions described above is no value is provided. | ||
*/ | ||
export const ControllerDefaults: ControllerOptions = { | ||
dirs: ['controllers'], | ||
extensions: ['.controller.js'], | ||
nested: true, | ||
discovered: [], | ||
}; |
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,6 @@ | ||
// Copyright IBM Corp. 2013,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 | ||
|
||
export * from './controller.booter'; |
Oops, something went wrong.