From f98d2cb89c1cabedce15c8eaec6b65e4622ba687 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Mon, 4 Aug 2014 10:33:03 +0200 Subject: [PATCH] Implemented modelSources, bootDirs and bootScripts options --- index.js | 3 +++ lib/compiler.js | 10 +++++++-- test/browser.test.js | 2 +- test/compiler.test.js | 47 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f6d8cbd..f7f0953 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,9 @@ var addInstructionsToBrowserify = require('./lib/bundler'); * `production`; however the applications are free to use any names. * @property {Array.} [modelSources] List of directories where to look * for files containing model definitions. + * @property {Array.} [bootDirs] List of directories where to look + * for boot scripts. + * @property {Array.} [bootScripts] List of script files to execute on boot. * @end * * @header boot(app, [options]) diff --git a/lib/compiler.js b/lib/compiler.js index e75ff6b..bca4a85 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -42,12 +42,18 @@ module.exports = function compile(options) { assertIsValidConfig('data source', dataSourcesConfig); // require directories - var bootScripts = findScripts(path.join(appRootDir, 'boot')); + var bootDirs = options.bootDirs || []; // precedence + bootDirs = bootDirs.concat(path.join(appRootDir, 'boot')); + + var bootScripts = options.bootScripts || []; + bootDirs.forEach(function(dir) { + bootScripts = bootScripts.concat(findScripts(dir)); + }); var modelsMeta = modelsConfig._meta || {}; delete modelsConfig._meta; - var modelSources = modelsMeta.sources || ['./models']; + var modelSources = options.modelSources || modelsMeta.sources || ['./models']; var modelInstructions = buildAllModelInstructions( modelsRootDir, modelsConfig, modelSources); diff --git a/test/browser.test.js b/test/browser.test.js index 1df93f9..1d5c660 100644 --- a/test/browser.test.js +++ b/test/browser.test.js @@ -37,7 +37,7 @@ function browserifyTestApp(appDir, next) { var bundlePath = sandbox.resolve('browser-app-bundle.js'); var out = fs.createWriteStream(bundlePath); b.bundle({ debug: true }).pipe(out); - + out.on('error', function(err) { return next(err); }); out.on('close', function() { next(null, bundlePath); diff --git a/test/compiler.test.js b/test/compiler.test.js index 5b6e450..69b782d 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -218,6 +218,28 @@ describe('compiler', function() { var instructions = boot.compile(appdir.PATH); expect(instructions.files.boot).to.eql([initJs]); }); + + it('supports `bootDirs` option', function() { + appdir.createConfigFilesSync(); + var initJs = appdir.writeFileSync('custom-boot/init.js', + 'module.exports = function(app) { app.fnCalled = true; };'); + var instructions = boot.compile({ + appRootDir: appdir.PATH, + bootDirs: [path.dirname(initJs)] + }); + expect(instructions.files.boot).to.eql([initJs]); + }); + + it('supports `bootScripts` option', function() { + appdir.createConfigFilesSync(); + var initJs = appdir.writeFileSync('custom-boot/init.js', + 'module.exports = function(app) { app.fnCalled = true; };'); + var instructions = boot.compile({ + appRootDir: appdir.PATH, + bootScripts: [initJs] + }); + expect(instructions.files.boot).to.eql([initJs]); + }); it('ignores models/ subdirectory', function() { appdir.createConfigFilesSync(); @@ -267,6 +289,31 @@ describe('compiler', function() { sourceFile: path.resolve(appdir.PATH, 'models', 'car.js') }); }); + + it('supports `sources` option', function() { + appdir.createConfigFilesSync({}, {}, { + Car: { dataSource: 'db' } + }); + appdir.writeConfigFileSync('custom-models/car.json', { name: 'Car' }); + appdir.writeFileSync('custom-models/car.js', ''); + + var instructions = boot.compile({ + appRootDir: appdir.PATH, + modelSources: ['./custom-models'] + }); + + expect(instructions.models).to.have.length(1); + expect(instructions.models[0]).to.eql({ + name: 'Car', + config: { + dataSource: 'db' + }, + definition: { + name: 'Car' + }, + sourceFile: path.resolve(appdir.PATH, 'custom-models', 'car.js') + }); + }); it('supports `sources` option in `model-config.json`', function() { appdir.createConfigFilesSync({}, {}, {