Fix coding style issues, add API docs
This commit is contained in:
parent
b6c60a297d
commit
0563840006
1
.jscsrc
1
.jscsrc
|
@ -5,6 +5,7 @@
|
|||
"try",
|
||||
"catch"
|
||||
],
|
||||
"disallowMultipleVarDecl": "exceptUndefined",
|
||||
"disallowSpacesInsideObjectBrackets": null,
|
||||
"requireSpaceAfterLineComment": true,
|
||||
"maximumLineLength": {
|
||||
|
|
3
index.js
3
index.js
|
@ -97,6 +97,9 @@ var addInstructionsToBrowserify = require('./lib/bundler');
|
|||
* @property {String} [appConfigRootDir] Directory to use when loading
|
||||
* `config.json`. Defaults to `appRootDir`.
|
||||
* @property {Object} [models] Object containing `Model` configurations.
|
||||
* @property {Array} [modelDefinitions] List of model definitions to use.
|
||||
* When `options.modelDefinitions` is provided, loopback-boot does not
|
||||
* search filesystem and use only the models provided in this argument.
|
||||
* @property {Object} [dataSources] Object containing `DataSource` definitions.
|
||||
* @property {String} [modelsRootDir] Directory to use when loading
|
||||
* `model-config.json`. Defaults to `appRootDir`.
|
||||
|
|
|
@ -81,10 +81,8 @@ module.exports = function compile(options) {
|
|||
delete modelsConfig._meta;
|
||||
|
||||
var modelSources = options.modelSources || modelsMeta.sources || ['./models'];
|
||||
var modelDefinitions = options.modelDefinitions || undefined;
|
||||
|
||||
var modelInstructions = buildAllModelInstructions(
|
||||
modelsRootDir, modelsConfig, modelSources, modelDefinitions);
|
||||
modelsRootDir, modelsConfig, modelSources, options.modelDefinitions);
|
||||
|
||||
var mixinDirs = options.mixinDirs || [];
|
||||
var mixinInstructions = buildAllMixinInstructions(
|
||||
|
@ -194,8 +192,10 @@ function tryReadDir() {
|
|||
}
|
||||
}
|
||||
|
||||
function buildAllModelInstructions(rootDir, modelsConfig, sources, modelDefinitions) {
|
||||
var registry = verifyModelDefinitions(rootDir, modelDefinitions) || findModelDefinitions(rootDir, sources);
|
||||
function buildAllModelInstructions(rootDir, modelsConfig, sources,
|
||||
modelDefinitions) {
|
||||
var registry = verifyModelDefinitions(rootDir, modelDefinitions) ||
|
||||
findModelDefinitions(rootDir, sources);
|
||||
|
||||
var modelNamesToBuild = addAllBaseModels(registry, Object.keys(modelsConfig));
|
||||
|
||||
|
@ -277,9 +277,10 @@ function sortByInheritance(instructions) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns matching files with a supported `sourceFile` extension like `.js` or `.coffee`
|
||||
* @param allFiles {Array}
|
||||
* @param basename {string}
|
||||
* Returns matching files with a supported `sourceFile` extension like
|
||||
* `.js` or `.coffee`
|
||||
* @param {Array} allFiles
|
||||
* @param {string} basename
|
||||
* @returns {Array}
|
||||
*/
|
||||
function filterValidSourceFiles(allFiles, basename) {
|
||||
|
@ -329,7 +330,9 @@ function verifyModelDefinitions(rootDir, modelDefinitions) {
|
|||
|
||||
var modelName = definition.definition.name;
|
||||
if (!modelName) {
|
||||
debug('Skipping model definition without Model name (from options.modelDefinitions @ index %s)', idx);
|
||||
debug('Skipping model definition without Model name ' +
|
||||
'(from options.modelDefinitions @ index %s)',
|
||||
idx);
|
||||
return;
|
||||
}
|
||||
registry[modelName] = definition;
|
||||
|
|
|
@ -81,165 +81,178 @@ describe('compiler', function() {
|
|||
|
||||
describe('with custom model definitions', function() {
|
||||
var dataSources = {
|
||||
'the-db': {
|
||||
connector: 'memory',
|
||||
defaultForType: 'db'
|
||||
}
|
||||
'the-db': { connector: 'memory' }
|
||||
};
|
||||
|
||||
it('should loads model without definition.', function() {
|
||||
it('loads model without definition', function() {
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'model-without-definitions': {
|
||||
'model-without-definition': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('model-without-definitions');
|
||||
expect(instruction.models[0].name)
|
||||
.to.equal('model-without-definition');
|
||||
expect(instruction.models[0].definition).to.equal(undefined);
|
||||
expect(instruction.models[0].sourceFile).to.equal(undefined);
|
||||
});
|
||||
|
||||
it('should load coffeescript models', function() {
|
||||
require('coffee-script/register');
|
||||
|
||||
appdir.writeFileSync('custom-models/coffee-model-with-definitions.coffee', '');
|
||||
it('loads coffeescript models', function() {
|
||||
var modelScript = appdir.writeFileSync(
|
||||
'custom-models/coffee-model-with-definition.coffee', '');
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'coffee-model-with-definitions': {
|
||||
'coffee-model-with-definition': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [
|
||||
{
|
||||
definition: {
|
||||
name: 'coffee-model-with-definitions'
|
||||
name: 'coffee-model-with-definition'
|
||||
},
|
||||
sourceFile: path.join(appdir.PATH, 'custom-models', 'coffee-model-with-definitions.coffee')
|
||||
sourceFile: modelScript
|
||||
}
|
||||
],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('coffee-model-with-definitions');
|
||||
expect(instruction.models[0].definition).not.to.equal(undefined);
|
||||
expect(instruction.models[0].sourceFile).not.to.equal(undefined);
|
||||
expect(instruction.models[0].name)
|
||||
.to.equal('coffee-model-with-definition');
|
||||
expect(instruction.models[0].definition).to.eql({
|
||||
name: 'coffee-model-with-definition'
|
||||
});
|
||||
expect(instruction.models[0].sourceFile).to.equal(modelScript);
|
||||
});
|
||||
|
||||
it('should handle sourceFile path without extension.', function() {
|
||||
require('coffee-script/register');
|
||||
|
||||
appdir.writeFileSync('custom-models/coffee-model-without-ext.coffee', '');
|
||||
appdir.writeFileSync('custom-models/js-model-without-ext.js', '');
|
||||
it('handles sourceFile path without extension (.js)', function() {
|
||||
var modelScript = appdir.writeFileSync(
|
||||
'custom-models/model-without-ext.coffee',
|
||||
'');
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'coffee-model-without-ext': {
|
||||
'model-without-ext': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [{
|
||||
definition: {
|
||||
name: 'model-without-ext'
|
||||
},
|
||||
'js-model-without-ext': {
|
||||
sourceFile: pathWithoutExtension(modelScript)
|
||||
}],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('model-without-ext');
|
||||
expect(instruction.models[0].sourceFile).to.equal(modelScript);
|
||||
});
|
||||
|
||||
it('handles sourceFile path without extension (.coffee)', function() {
|
||||
var modelScript = appdir.writeFileSync(
|
||||
'custom-models/model-without-ext.coffee',
|
||||
'');
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'model-without-ext': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [
|
||||
{
|
||||
definition: {
|
||||
name: 'coffee-model-without-ext'
|
||||
},
|
||||
sourceFile: path.join(appdir.PATH, 'custom-models', 'coffee-model-without-ext')
|
||||
modelDefinitions: [{
|
||||
definition: {
|
||||
name: 'model-without-ext'
|
||||
},
|
||||
{
|
||||
definition: {
|
||||
name: 'js-model-without-ext'
|
||||
},
|
||||
sourceFile: path.join(appdir.PATH, 'custom-models', 'js-model-without-ext')
|
||||
}
|
||||
],
|
||||
sourceFile: pathWithoutExtension(modelScript)
|
||||
}],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('coffee-model-without-ext');
|
||||
expect(instruction.models[0].sourceFile)
|
||||
.to.equal(path.join(appdir.PATH, 'custom-models', 'coffee-model-without-ext.coffee'));
|
||||
|
||||
expect(instruction.models[1].name).to.equal('js-model-without-ext');
|
||||
expect(instruction.models[1].sourceFile)
|
||||
.to.equal(path.join(appdir.PATH, 'custom-models', 'js-model-without-ext.js'));
|
||||
expect(instruction.models[0].name).to.equal('model-without-ext');
|
||||
expect(instruction.models[0].sourceFile).to.equal(modelScript);
|
||||
});
|
||||
|
||||
it('should set source file path if the file exist.', function() {
|
||||
appdir.writeFileSync('custom-models/model-with-definitions.js', '');
|
||||
it('sets source file path if the file exist', function() {
|
||||
var modelScript = appdir.writeFileSync(
|
||||
'custom-models/model-with-definition.js',
|
||||
'');
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'model-with-definitions': {
|
||||
'model-with-definition': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [
|
||||
{
|
||||
definition: {
|
||||
name: 'model-with-definitions'
|
||||
name: 'model-with-definition'
|
||||
},
|
||||
sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions.js')
|
||||
sourceFile: modelScript
|
||||
}
|
||||
],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('model-with-definitions');
|
||||
expect(instruction.models[0].name).to.equal('model-with-definition');
|
||||
expect(instruction.models[0].definition).not.to.equal(undefined);
|
||||
expect(instruction.models[0].sourceFile)
|
||||
.to.equal(path.join(appdir.PATH, 'custom-models', 'model-with-definitions.js'));
|
||||
expect(instruction.models[0].sourceFile).to.equal(modelScript);
|
||||
});
|
||||
|
||||
it('should not set source file path if the file does not exist.', function() {
|
||||
it('does not set source file path if the file does not exist.',
|
||||
function() {
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'model-with-definitions-with-falsey-source-file': {
|
||||
'model-with-definition-with-falsey-source-file': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [
|
||||
{
|
||||
definition: { // sourceFile is a not existing file.
|
||||
name: 'model-with-definitions-with-falsey-source-file'
|
||||
definition: {
|
||||
name: 'model-with-definition-with-falsey-source-file'
|
||||
},
|
||||
sourceFile: path.join(appdir.PATH, 'custom-models', 'model-with-definitions-with-falsey-source-file.js')
|
||||
sourceFile: appdir.resolve('custom-models',
|
||||
'file-does-not-exist.js')
|
||||
}
|
||||
],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('model-with-definitions-with-falsey-source-file');
|
||||
expect(instruction.models[0].name)
|
||||
.to.equal('model-with-definition-with-falsey-source-file');
|
||||
expect(instruction.models[0].definition).not.to.equal(undefined);
|
||||
expect(instruction.models[0].sourceFile).to.equal(undefined);
|
||||
});
|
||||
|
||||
it('should not set source file path if no source file supplied.', function() {
|
||||
it('does not set source file path if no source file supplied.',
|
||||
function() {
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
'model-with-definitions-without-source-file-property': {
|
||||
'model-with-definition-without-source-file-property': {
|
||||
dataSource: 'the-db'
|
||||
}
|
||||
},
|
||||
modelDefinitions: [
|
||||
{
|
||||
definition: { // sourceFile is a not existing file.
|
||||
name: 'model-with-definitions-without-source-file-property'
|
||||
definition: {
|
||||
name: 'model-with-definition-without-source-file-property'
|
||||
}
|
||||
// sourceFile is not set
|
||||
}
|
||||
],
|
||||
dataSources: dataSources
|
||||
});
|
||||
expect(instruction.models[0].name).to.equal('model-with-definitions-without-source-file-property');
|
||||
expect(instruction.models[0].name)
|
||||
.to.equal('model-with-definition-without-source-file-property');
|
||||
expect(instruction.models[0].definition).not.to.equal(undefined);
|
||||
expect(instruction.models[0].sourceFile).to.equal(undefined);
|
||||
});
|
||||
|
||||
it('should loads models defined in `models` only.', function() {
|
||||
it('loads models defined in `models` only.', function() {
|
||||
var instruction = boot.compile({
|
||||
appRootDir: appdir.PATH,
|
||||
models: {
|
||||
|
@ -255,14 +268,15 @@ describe('compiler', function() {
|
|||
},
|
||||
{
|
||||
definition: {
|
||||
name: 'some-fictional-model'
|
||||
name: 'another-model'
|
||||
}
|
||||
}
|
||||
],
|
||||
dataSources: dataSources
|
||||
});
|
||||
|
||||
expect(instruction.models).to.have.length(1);
|
||||
expect(instruction.models.map(getNameProperty))
|
||||
.to.eql(['some-model']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1924,3 +1938,9 @@ function givenMiddlewareEntrySync(config) {
|
|||
function expectFirstMiddlewareParams(instructions) {
|
||||
return expect(instructions.middleware.middleware[0].config.params);
|
||||
}
|
||||
|
||||
function pathWithoutExtension(value) {
|
||||
return path.join(
|
||||
path.dirname(value),
|
||||
path.basename(value, path.extname(value)));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue