Skip to content

Commit c896c78

Browse files
author
Miroslav Bajtoš
committed
Remove app.boot
Modify `app.boot` to throw an exception, pointing users to the new module `loopback-boot`.
1 parent 27a632c commit c896c78

File tree

5 files changed

+9
-476
lines changed

5 files changed

+9
-476
lines changed

lib/application.js

Lines changed: 2 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -372,210 +372,9 @@ app.enableAuth = function() {
372372
this.isAuthEnabled = true;
373373
};
374374

375-
/**
376-
* Initialize an application from an options object or a set of JSON and JavaScript files.
377-
*
378-
* **Deprecated. Use the package
379-
* [loopback-boot](https://github.com/strongloop/loopback-boot) instead.**
380-
*
381-
* This function takes an optional argument that is either a string or an object.
382-
*
383-
* If the argument is a string, then it sets the application root directory based on the string value. Then it:
384-
* 1. Creates DataSources from the `datasources.json` file in the application root directory.
385-
* 2. Creates Models from the `models.json` file in the application root directory.
386-
*
387-
* If the argument is an object, then it looks for `model`, `dataSources`, and `appRootDir` properties of the object.
388-
* If the object has no `appRootDir` property then it sets the current working directory as the application root directory.
389-
* Then it:
390-
* 1. Creates DataSources from the `options.dataSources` object.
391-
* 2. Creates Models from the `options.models` object.
392-
*
393-
* In both cases, the function loads JavaScript files in the `/models` and `/boot` subdirectories of the application root directory with `require()`.
394-
*
395-
* **NOTE:** mixing `app.boot()` and `app.model(name, config)` in multiple
396-
* files may result in models being **undefined** due to race conditions.
397-
* To avoid this when using `app.boot()` make sure all models are passed as part of the `models` definition.
398-
*
399-
* Throws an error if the config object is not valid or if boot fails.
400-
*
401-
* <a name="model-definition"></a>
402-
* **Model Definitions**
403-
*
404-
* The following is example JSON for two `Model` definitions: "dealership" and "location".
405-
*
406-
* ```js
407-
* {
408-
* "dealership": {
409-
* // a reference, by name, to a dataSource definition
410-
* "dataSource": "my-db",
411-
* // the options passed to Model.extend(name, properties, options)
412-
* "options": {
413-
* "relations": {
414-
* "cars": {
415-
* "type": "hasMany",
416-
* "model": "Car",
417-
* "foreignKey": "dealerId"
418-
* }
419-
* }
420-
* },
421-
* // the properties passed to Model.extend(name, properties, options)
422-
* "properties": {
423-
* "id": {"id": true},
424-
* "name": "String",
425-
* "zip": "Number",
426-
* "address": "String"
427-
* }
428-
* },
429-
* "car": {
430-
* "dataSource": "my-db"
431-
* "properties": {
432-
* "id": {
433-
* "type": "String",
434-
* "required": true,
435-
* "id": true
436-
* },
437-
* "make": {
438-
* "type": "String",
439-
* "required": true
440-
* },
441-
* "model": {
442-
* "type": "String",
443-
* "required": true
444-
* }
445-
* }
446-
* }
447-
* }
448-
* ```
449-
* @options {String|Object} options Boot options; If String, this is the application root directory; if object, has below properties.
450-
* @property {String} appRootDir Directory to use when loading JSON and JavaScript files (optional). Defaults to the current directory (`process.cwd()`).
451-
* @property {Object} models Object containing `Model` definitions (optional).
452-
* @property {Object} dataSources Object containing `DataSource` definitions (optional).
453-
* @end
454-
*
455-
* @header app.boot([options])
456-
*/
457-
458375
app.boot = function(options) {
459-
options = options || {};
460-
461-
if(typeof options === 'string') {
462-
options = {appRootDir: options};
463-
}
464-
var app = this;
465-
var appRootDir = options.appRootDir = options.appRootDir || process.cwd();
466-
var ctx = {};
467-
var appConfig = options.app;
468-
var modelConfig = options.models;
469-
var dataSourceConfig = options.dataSources;
470-
471-
if(!appConfig) {
472-
appConfig = tryReadConfig(appRootDir, 'app') || {};
473-
}
474-
if(!modelConfig) {
475-
modelConfig = tryReadConfig(appRootDir, 'models') || {};
476-
}
477-
if(!dataSourceConfig) {
478-
dataSourceConfig = tryReadConfig(appRootDir, 'datasources') || {};
479-
}
480-
481-
assertIsValidConfig('app', appConfig);
482-
assertIsValidConfig('model', modelConfig);
483-
assertIsValidConfig('data source', dataSourceConfig);
484-
485-
appConfig.host =
486-
process.env.npm_config_host ||
487-
process.env.OPENSHIFT_SLS_IP ||
488-
process.env.OPENSHIFT_NODEJS_IP ||
489-
process.env.HOST ||
490-
appConfig.host ||
491-
process.env.npm_package_config_host ||
492-
app.get('host');
493-
494-
appConfig.port = _.find([
495-
process.env.npm_config_port,
496-
process.env.OPENSHIFT_SLS_PORT,
497-
process.env.OPENSHIFT_NODEJS_PORT,
498-
process.env.PORT,
499-
appConfig.port,
500-
process.env.npm_package_config_port,
501-
app.get('port'),
502-
3000
503-
], _.isFinite);
504-
505-
appConfig.restApiRoot =
506-
appConfig.restApiRoot ||
507-
app.get('restApiRoot') ||
508-
'/api';
509-
510-
if(appConfig.host !== undefined) {
511-
assert(typeof appConfig.host === 'string', 'app.host must be a string');
512-
app.set('host', appConfig.host);
513-
}
514-
515-
if(appConfig.port !== undefined) {
516-
var portType = typeof appConfig.port;
517-
assert(portType === 'string' || portType === 'number', 'app.port must be a string or number');
518-
app.set('port', appConfig.port);
519-
}
520-
521-
assert(appConfig.restApiRoot !== undefined, 'app.restApiRoot is required');
522-
assert(typeof appConfig.restApiRoot === 'string', 'app.restApiRoot must be a string');
523-
assert(/^\//.test(appConfig.restApiRoot), 'app.restApiRoot must start with "/"');
524-
app.set('restApiRoot', appConfig.restApiRoot);
525-
526-
for(var configKey in appConfig) {
527-
var cur = app.get(configKey);
528-
if(cur === undefined || cur === null) {
529-
app.set(configKey, appConfig[configKey]);
530-
}
531-
}
532-
533-
// instantiate data sources
534-
forEachKeyedObject(dataSourceConfig, function(key, obj) {
535-
app.dataSource(key, obj);
536-
});
537-
538-
// instantiate models
539-
forEachKeyedObject(modelConfig, function(key, obj) {
540-
app.model(key, obj);
541-
});
542-
543-
// try to attach models to dataSources by type
544-
try {
545-
registry.autoAttach();
546-
} catch(e) {
547-
if(e.name === 'AssertionError') {
548-
console.warn(e);
549-
} else {
550-
throw e;
551-
}
552-
}
553-
554-
// disable token requirement for swagger, if available
555-
var swagger = app.remotes().exports.swagger;
556-
var requireTokenForSwagger = appConfig.swagger
557-
&& appConfig.swagger.requireToken;
558-
if(swagger) {
559-
swagger.requireToken = requireTokenForSwagger || false;
560-
}
561-
562-
// require directories
563-
var requiredModels = requireDir(path.join(appRootDir, 'models'));
564-
var requiredBootScripts = requireDir(path.join(appRootDir, 'boot'));
565-
}
566-
567-
function assertIsValidConfig(name, config) {
568-
if(config) {
569-
assert(typeof config === 'object', name + ' config must be a valid JSON object');
570-
}
571-
}
572-
573-
function forEachKeyedObject(obj, fn) {
574-
if(typeof obj !== 'object') return;
575-
576-
Object.keys(obj).forEach(function(key) {
577-
fn(key, obj[key]);
578-
});
376+
throw new Error(
377+
'`app.boot` was removed, use the new module loopback-boot instead');
579378
}
580379

581380
function classify(str) {
@@ -628,85 +427,6 @@ function configureModel(ModelCtor, config, app) {
628427
registry.configureModel(ModelCtor, config);
629428
}
630429

631-
function requireDir(dir, basenames) {
632-
assert(dir, 'cannot require directory contents without directory name');
633-
634-
var requires = {};
635-
636-
if (arguments.length === 2) {
637-
// if basenames argument is passed, explicitly include those files
638-
basenames.forEach(function (basename) {
639-
var filepath = Path.resolve(Path.join(dir, basename));
640-
requires[basename] = tryRequire(filepath);
641-
});
642-
} else if (arguments.length === 1) {
643-
// if basenames arguments isn't passed, require all javascript
644-
// files (except for those prefixed with _) and all directories
645-
646-
var files = tryReadDir(dir);
647-
648-
// sort files in lowercase alpha for linux
649-
files.sort(function (a,b) {
650-
a = a.toLowerCase();
651-
b = b.toLowerCase();
652-
653-
if (a < b) {
654-
return -1;
655-
} else if (b < a) {
656-
return 1;
657-
} else {
658-
return 0;
659-
}
660-
});
661-
662-
files.forEach(function (filename) {
663-
// ignore index.js and files prefixed with underscore
664-
if ((filename === 'index.js') || (filename[0] === '_')) { return; }
665-
666-
var filepath = path.resolve(path.join(dir, filename));
667-
var ext = path.extname(filename);
668-
var stats = fs.statSync(filepath);
669-
670-
// only require files supported by require.extensions (.txt .md etc.)
671-
if (stats.isFile() && !(ext in require.extensions)) { return; }
672-
673-
var basename = path.basename(filename, ext);
674-
675-
requires[basename] = tryRequire(filepath);
676-
});
677-
678-
}
679-
680-
return requires;
681-
};
682-
683-
function tryRequire(modulePath) {
684-
try {
685-
return require.apply(this, arguments);
686-
} catch(e) {
687-
console.error('failed to require "%s"', modulePath);
688-
throw e;
689-
}
690-
}
691-
692-
function tryReadDir() {
693-
try {
694-
return fs.readdirSync.apply(fs, arguments);
695-
} catch(e) {
696-
return [];
697-
}
698-
}
699-
700-
function tryReadConfig(cwd, fileName) {
701-
try {
702-
return require(path.join(cwd, fileName + '.json'));
703-
} catch(e) {
704-
if(e.code !== "MODULE_NOT_FOUND") {
705-
throw e;
706-
}
707-
}
708-
}
709-
710430
function clearHandlerCache(app) {
711431
app._handlers = undefined;
712432
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"strong-task-emitter": "0.0.x",
5858
"supertest": "~0.13.0",
5959
"chai": "~1.9.1",
60+
"loopback-boot": "1.x >=1.1",
6061
"loopback-testing": "~0.2.0",
6162
"browserify": "~4.1.6",
6263
"grunt": "~0.4.5",

0 commit comments

Comments
 (0)