diff --git a/docs/api-app.md b/docs/api-app.md index 7768aa95..0b3c7bb8 100644 --- a/docs/api-app.md +++ b/docs/api-app.md @@ -58,6 +58,7 @@ Initialize an application from an options object or a set of JSON and JavaScript 1. **DataSources** are created from an `options.dataSources` object or `datasources.json` in the current directory 2. **Models** are created from an `options.models` object or `models.json` in the current directory 3. Any JavaScript files in the `./models` directory are loaded with `require()`. +4. Any JavaScript files in the `./boot` directory are loaded with `require()`. **Options** diff --git a/lib/application.js b/lib/application.js index d63e292d..a2a0ccfa 100644 --- a/lib/application.js +++ b/lib/application.js @@ -290,6 +290,7 @@ app.boot = function(options) { // require directories var requiredModels = requireDir(path.join(appRootDir, 'models')); + var requiredBootScripts = requireDir(path.join(appRootDir, 'boot')); } function assertIsValidConfig(name, config) { diff --git a/test/app.test.js b/test/app.test.js index 46ff2576..e472541c 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -1,3 +1,6 @@ +var path = require('path'); +var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app'); + describe('app', function() { describe('app.model(Model)', function() { @@ -75,6 +78,23 @@ describe('app', function() { expect(this.app.get('baz')).to.eql(true); }); + describe('boot and models directories', function() { + beforeEach(function() { + var app = this.app = loopback(); + app.boot(SIMPLE_APP); + }); + + it('should run all modules in the boot directory', function () { + assert(process.loadedFooJS); + delete process.loadedFooJS; + }); + + it('should run all modules in the models directory', function () { + assert(process.loadedBarJS); + delete process.loadedBarJS; + }); + }); + describe('PaaS and npm env variables', function() { beforeEach(function() { this.boot = function () { @@ -162,7 +182,7 @@ describe('app', function() { it('Load config files', function () { var app = loopback(); - app.boot(require('path').join(__dirname, 'fixtures', 'simple-app')); + app.boot(SIMPLE_APP); assert(app.models.foo); assert(app.models.Foo); diff --git a/test/fixtures/simple-app/boot/bad.txt b/test/fixtures/simple-app/boot/bad.txt new file mode 100644 index 00000000..81fae523 --- /dev/null +++ b/test/fixtures/simple-app/boot/bad.txt @@ -0,0 +1 @@ +this is not a js file! diff --git a/test/fixtures/simple-app/boot/foo.js b/test/fixtures/simple-app/boot/foo.js new file mode 100644 index 00000000..7e748634 --- /dev/null +++ b/test/fixtures/simple-app/boot/foo.js @@ -0,0 +1 @@ +process.loadedFooJS = true; diff --git a/test/fixtures/simple-app/models/bar.js b/test/fixtures/simple-app/models/bar.js new file mode 100644 index 00000000..0eef5d94 --- /dev/null +++ b/test/fixtures/simple-app/models/bar.js @@ -0,0 +1 @@ +process.loadedBarJS = true;