Skip to content

Commit

Permalink
feat(boot): bind content of package.json to app context
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Sep 26, 2018
1 parent 4ad6e4f commit d8180ef
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/boot/src/mixins/boot.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {Booter, BootOptions, Bootable} from '../interfaces';
import {BootComponent} from '../boot.component';
import {Bootstrapper} from '../bootstrapper';
import {BootBindings} from '../keys';
import {CoreBindings} from '@loopback/core';
import * as path from 'path';

// Binding is re-exported as Binding / Booter types are needed when consuming
// BootMixin and this allows a user to import them from the same package (UX!)
Expand Down Expand Up @@ -53,6 +55,11 @@ export function BootMixin<T extends Constructor<any>>(superClass: T) {
this.bind(BootBindings.BOOT_OPTIONS).toDynamicValue(
() => this.bootOptions,
);

this.bind(CoreBindings.APPLICATION_PACKAGE_JSON).toDynamicValue(() => {
const pkgFile = path.resolve(this.projectRoot, 'package.json');
return require(pkgFile);
});
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/boot/test/acceptance/controller.booter.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
createRestAppClient,
givenHttpServerConfig,
TestSandbox,
expect,
} from '@loopback/testlab';
import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';
import {CoreBindings} from '@loopback/core';

describe('controller booter acceptance tests', () => {
let app: BooterApp;
Expand All @@ -21,6 +23,15 @@ describe('controller booter acceptance tests', () => {

afterEach(stopApp);

it('binds package.json', async () => {
const pkg = await app.get(CoreBindings.APPLICATION_PACKAGE_JSON);
expect(pkg).containEql({
name: 'boot-test-app',
version: '1.0.0',
description: 'boot-test-app',
});
});

it('binds controllers using ControllerDefaults and REST endpoints work', async () => {
await app.boot();
await app.start();
Expand All @@ -33,6 +44,7 @@ describe('controller booter acceptance tests', () => {
});

async function getApp() {
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'));
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js'),
Expand Down
19 changes: 19 additions & 0 deletions packages/boot/test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "boot-test-app",
"version": "1.0.0",
"description": "boot-test-app",
"keywords": [
"loopback-application",
"loopback"
],
"engines": {
"node": ">=8.9"
},
"scripts": {
},
"repository": {
"type": "git"
},
"author": "",
"license": ""
}
15 changes: 15 additions & 0 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export class Application extends Context {
const instance = this.getSync<Component>(componentKey);
mountComponent(this, instance);
}

public packageJson(pkg: PackageJson) {
this.bind(CoreBindings.APPLICATION_PACKAGE_JSON).to(pkg);
}
}

/**
Expand All @@ -207,3 +211,14 @@ export interface ApplicationConfig {

// tslint:disable-next-line:no-any
export type ControllerClass = Constructor<any>;

/**
* Type description for `package.json`
*/
export interface PackageJson {
name: string;
version: string;
description: string;
// tslint:disable-next-line:no-any
[name: string]: any;
}
9 changes: 8 additions & 1 deletion packages/core/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {BindingKey} from '@loopback/context';
import {Application, ControllerClass} from './application';
import {Application, ControllerClass, PackageJson} from './application';

/**
* Namespace for core binding keys
Expand All @@ -25,6 +25,13 @@ export namespace CoreBindings {
'application.config',
);

/**
* Binding key for the content of `package.json`
*/
export const APPLICATION_PACKAGE_JSON = BindingKey.create<PackageJson>(
'application.package-json',
);

// server
/**
* Binding key for servers
Expand Down

0 comments on commit d8180ef

Please sign in to comment.