Skip to content

Commit

Permalink
feat: @loopback/boot (#858)
Browse files Browse the repository at this point in the history
* feat(boot): initial project setup

* feat(boot): bootstrapper implementation

* feat(boot): controller booter implementation

* feat(core): add boot, booters, and related interfaces / types

* refactor(example-getting-started): use @loopback/boot

* feat(cli): enable loopbackBoot as a option for new app projects

* fix(cli): loopbackBoot should always be enabled and not an option

* fix(boot): move bootOptions to ApplicationConfig

* fix(boot): fix test name

* fix(cli): add comments to template

* style(core): update style for if statement

* refactor: move boot related stuff from core to a mixin
  • Loading branch information
virkt25 authored Feb 21, 2018
1 parent 0dde314 commit c2ca8be
Show file tree
Hide file tree
Showing 43 changed files with 1,576 additions and 29 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @bajtos @raymondfeng @kjdelisle

packages/authentication/* @bajtos @kjdelisle
packages/boot/* @raymondfeng @virkt25
packages/build/* @bajtos @raymondfeng
packages/cli/* @raymondfeng @kjdelisle @shimks
packages/context/* @bajtos @raymondfeng @kjdelisle
Expand Down
1 change: 1 addition & 0 deletions MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The [loopback-next](https://github.com/strongloop/loopback-next) repository uses
|[metadata](packages/metadata) |@loopback/metadata | Utilities to help developers implement TypeScript decorators, define/merge metadata, and inspect metadata |
|[context](packages/context) |@loopback/context | Facilities to manage artifacts and their dependencies in your Node.js applications. The module exposes TypeScript/JavaScript APIs and decorators to register artifacts, declare dependencies, and resolve artifacts by keys. It also serves as an IoC container to support dependency injection. |
|[core](packages/core) |@loopback/core | Define and implement core constructs such as Application and Component |
|[boot](packages/boot) |@loopback/boot | Convention based Bootstrapper and Booters |
|[openapi-spec](packages/openapi-spec) |@loopback/openapi-spec | TypeScript type definitions for OpenAPI Spec/Swagger documents |
|[openapi-spec-builder](packages/openapi-spec-builder) |@loopback/openapi-spec-builder | Builders to create OpenAPI (Swagger) specification documents in tests |
|[openapi-v2](packages/openapi-v2) |@loopback/openapi-v2 | Decorators that annotate LoopBack artifacts with OpenAPI v2 (Swagger) metadata and utilities that transform LoopBack metadata to OpenAPI v2 (Swagger) specifications|
Expand Down
1 change: 1 addition & 0 deletions packages/boot/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 25 additions & 0 deletions packages/boot/LICENSE
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.
97 changes: 97 additions & 0 deletions packages/boot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# @loopback/boot

A convention based project Bootstrapper and 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 its task.

An example task of a Booter may be to discover and bind all artifacts of a
given type.

A Bootstrapper is needed to manage the Booters and execute them. This is provided
in this package. For ease of use, everything needed is packages using a BootMixin.
This Mixin will add convenience methods such as `boot` and `booter`, as well as
properties needed for Bootstrapper such as `projectRoot`. The Mixin also adds the
`BootComponent` to your `Application` which binds the `Bootstrapper` and default
`Booters` made available by this package.

## Installation

```shell
$ npm i @loopback/boot
```

## Basic Use

```ts
import {Application} from '@loopback/core';
import {BootMixin} from '@loopback/boot';
class BootApp extends BootMixin(Application) {}

const app = new BootApp();
app.projectRoot = __dirname;
app.bootOptions = {
controlles: {
// Configure ControllerBooter Conventiones here.
}
}

await app.boot();
await app.start();
```

### BootOptions
List of Options available on BootOptions Object.

|Option|Type|Description|
|-|-|-|
|`controllers`|`ArtifactOptions`|ControllerBooter convention options|

### ArtifactOptions

**Add Table for ArtifactOptions**

### BootExecOptions

**Add Table for BootExecOptions**

## Available Booters

### ControllerBooter

#### Description
Discovers and binds Controller Classes using `app.controller()`.

#### Options
The Options for this can be passed via `BootOptions` when calling `app.boot(options:BootOptions)`.

The options for this are passed in a `controllers` object on `BootOptions`.

Available Options on the `controllers` object on `BootOptions` 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|
|`glob`|`string`||A `glob` pattern string. This takes precendence over above 3 options (which are used to make a glob pattern).|

## 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
16 changes: 16 additions & 0 deletions packages/boot/docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"content": [
"index.ts",
"src/booters/base-artifact.booter.ts",
"src/booters/booter-utils.ts",
"src/booters/controller.booter.ts",
"src/booters/index.ts",
"src/boot.component.ts",
"src/boot.mixin.ts",
"src/bootstrapper.ts",
"src/index.ts",
"src/interfaces.ts",
"src/keys.ts"
],
"codeSectionDepth": 4
}
6 changes: 6 additions & 0 deletions packages/boot/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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

export * from './dist';
6 changes: 6 additions & 0 deletions packages/boot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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

module.exports = require('./dist');
6 changes: 6 additions & 0 deletions packages/boot/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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

export * from './src';
58 changes: 58 additions & 0 deletions packages/boot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@loopback/boot",
"version": "4.0.0-alpha.1",
"description": "A collection of Booters for LoopBack 4 Applications",
"engines": {
"node": ">=8"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"acceptance": "lb-mocha \"DIST/test/acceptance/**/*.js\"",
"build": "npm run build:dist",
"build:current": "lb-tsc",
"build:dist": "lb-tsc es2017",
"build:apidocs": "lb-apidocs",
"clean": "lb-clean loopback-boot*.tgz dist package api-docs",
"prepublishOnly": "npm run build && npm run build:apidocs",
"pretest": "npm run build:current",
"integration": "lb-mocha \"DIST/test/integration/**/*.js\"",
"test": "lb-mocha \"DIST/test/unit/**/*.js\" \"DIST/test/integration/**/*.js\" \"DIST/test/acceptance/**/*.js\"",
"unit": "lb-mocha \"DIST/test/unit/**/*.js\"",
"verify": "npm pack && tar xf loopback-boot*.tgz && tree package && npm run clean"
},
"author": "IBM",
"copyright.owner": "IBM Corp.",
"license": "MIT",
"dependencies": {
"@loopback/context": "^4.0.0-alpha.27",
"@loopback/core": "^4.0.0-alpha.29",
"@types/debug": "0.0.30",
"@types/glob": "^5.0.34",
"debug": "^3.1.0",
"glob": "^7.1.2"
},
"devDependencies": {
"@loopback/build": "^4.0.0-alpha.10",
"@loopback/openapi-v2": "^4.0.0-alpha.3",
"@loopback/rest": "^4.0.0-alpha.18",
"@loopback/testlab": "^4.0.0-alpha.20"
},
"files": [
"README.md",
"index.js",
"index.js.map",
"index.d.ts",
"dist/src",
"dist/index.js",
"dist/index.js.map",
"dist/index.d.ts",
"api-docs",
"src"
],
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git"
}
}
33 changes: 33 additions & 0 deletions packages/boot/src/boot.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 {Bootstrapper} from './bootstrapper';
import {Component, Application, CoreBindings} from '@loopback/core';
import {inject, BindingScope} from '@loopback/context';
import {ControllerBooter} from './booters';
import {BootBindings} from './keys';

/**
* BootComponent is used to export the default list of Booter's made
* available by this module as well as bind the BootStrapper to the app so it
* can be used to run the Booters.
*/
export class BootComponent implements Component {
// Export a list of default booters in the component so they get bound
// automatically when this component is mounted.
booters = [ControllerBooter];

/**
*
* @param app Application instance
*/
constructor(@inject(CoreBindings.APPLICATION_INSTANCE) app: Application) {
// Bound as a SINGLETON so it can be cached as it has no state
app
.bind(BootBindings.BOOTSTRAPPER_KEY)
.toClass(Bootstrapper)
.inScope(BindingScope.SINGLETON);
}
}
Loading

0 comments on commit c2ca8be

Please sign in to comment.