-
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): bind content of package.json to app context
- Loading branch information
1 parent
9ae93d8
commit 083d8d4
Showing
8 changed files
with
136 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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 {CoreBindings, Application} from '@loopback/core'; | ||
import {inject} from '@loopback/context'; | ||
import {BootBindings} from '../keys'; | ||
import {Booter} from '../interfaces'; | ||
import path = require('path'); | ||
|
||
import * as debugModule from 'debug'; | ||
const debug = debugModule('loopback:boot:booter:application-metadata'); | ||
|
||
/** | ||
* | ||
* Configure the application with metadata from `package.json` | ||
* | ||
* @param app Application instance | ||
* @param projectRoot Root of User Project | ||
*/ | ||
export class ApplicationMetadataBooter implements Booter { | ||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) public app: Application, | ||
@inject(BootBindings.PROJECT_ROOT) private projectRoot: string, | ||
) {} | ||
|
||
async configure() { | ||
try { | ||
const pkg = require(path.resolve(this.projectRoot, 'package.json')); | ||
this.app.setMetadata(pkg); | ||
} catch (err) { | ||
debug('package.json not found', err); | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
packages/boot/test/acceptance/application-metadata.booter.acceptance.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,48 @@ | ||
// 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 {givenHttpServerConfig, TestSandbox, expect} from '@loopback/testlab'; | ||
import {resolve} from 'path'; | ||
import {BooterApp} from '../fixtures/application'; | ||
import {CoreBindings} from '@loopback/core'; | ||
|
||
describe('application metadata booter acceptance tests', () => { | ||
let app: BooterApp; | ||
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox'); | ||
const sandbox = new TestSandbox(SANDBOX_PATH); | ||
|
||
beforeEach('reset sandbox', () => sandbox.reset()); | ||
beforeEach(getApp); | ||
|
||
afterEach(stopApp); | ||
|
||
it('binds content of package.json to application metadata', async () => { | ||
await app.boot(); | ||
const metadata = await app.get(CoreBindings.APPLICATION_METADATA); | ||
expect(metadata).containEql({ | ||
name: 'boot-test-app', | ||
version: '1.0.0', | ||
description: 'boot-test-app', | ||
}); | ||
}); | ||
|
||
async function getApp() { | ||
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json')); | ||
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); | ||
|
||
const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp; | ||
app = new MyApp({ | ||
rest: givenHttpServerConfig(), | ||
}); | ||
} | ||
|
||
async function stopApp() { | ||
try { | ||
await app.stop(); | ||
} catch (err) { | ||
console.log(`Stopping the app threw an error: ${err}`); | ||
} | ||
} | ||
}); |
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,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": "" | ||
} |
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