diff --git a/models.json b/models.json index d5435815..b645a871 100644 --- a/models.json +++ b/models.json @@ -41,6 +41,11 @@ "config.*.json" ], "relations": { + "package": { + "type": "hasOne", + "model": "PackageDefinition", + "foreignKey": "componentName" + }, "models": { "type": "hasMany", "model": "ModelDefinition", diff --git a/models/workspace-entity.js b/models/workspace-entity.js index 5d8162fe..d95cf65d 100644 --- a/models/workspace-entity.js +++ b/models/workspace-entity.js @@ -54,9 +54,13 @@ WorkspaceEntity.allFromCache = function(cache) { .map(this.getFromCache.bind(this, cache)); } -WorkspaceEntity.getPath = function(app, obj) { +WorkspaceEntity.getPath = function(componentName, obj) { if(obj.configFile) return obj.configFile; - return path.join(app, this.settings.defaultConfigFile); + return path.join(componentName, this.settings.defaultConfigFile); +} + +WorkspaceEntity.getDir = function(componentName, obj) { + return path.dirname(WorkspaceEntity.getPath(componentName, obj)); } WorkspaceEntity.getConfigFile = function(componentName, obj) { diff --git a/models/workspace.js b/models/workspace.js index fb84e712..ddaf4b4b 100644 --- a/models/workspace.js +++ b/models/workspace.js @@ -125,17 +125,13 @@ Workspace.addComponent = function(options, cb) { steps.push(function(cb) { fs.exists(fileTemplatesDir, function(exists) { if(exists) { - steps.push(copyTemplateFiles); - cb(); + ncp(fileTemplatesDir, dest, cb); } else { cb(); } }); }); - function copyTemplateFiles(cb) { - ncp(fileTemplatesDir, dest, cb); - } function setComponentName(obj) { if(Array.isArray(obj)) { obj.forEach(function(item) { diff --git a/templates/api-server/component.js b/templates/api-server/component.js index 4764899d..656c02cc 100644 --- a/templates/api-server/component.js +++ b/templates/api-server/component.js @@ -21,4 +21,4 @@ template.package = { "optionalDependencies": { "loopback-explorer": "^1.1.0" } -}; \ No newline at end of file +}; diff --git a/templates/rest/template/boot/authentication.js b/templates/rest/template/boot/authentication.js index c4fd96e3..9219ffed 100644 --- a/templates/rest/template/boot/authentication.js +++ b/templates/rest/template/boot/authentication.js @@ -1,4 +1,4 @@ var rest = require('../rest'); // enable authentication -server.enableAuth(); \ No newline at end of file +rest.enableAuth(); diff --git a/templates/rest/template/rest.js b/templates/rest/template/rest.js index 42852423..0e46b1ea 100644 --- a/templates/rest/template/rest.js +++ b/templates/rest/template/rest.js @@ -8,4 +8,4 @@ var server = module.exports = loopback(); boot(server, __dirname); // middleware -server.use(loopback.rest()); \ No newline at end of file +server.use(loopback.rest()); diff --git a/templates/server/template/server.js b/templates/server/template/server.js index 60283d46..acfc0acd 100644 --- a/templates/server/template/server.js +++ b/templates/server/template/server.js @@ -42,4 +42,4 @@ app.start = function() { // start the server if `$ node server.js` if (require.main === module) { app.start(); -} \ No newline at end of file +} diff --git a/test/component-definition.js b/test/component-definition.js new file mode 100644 index 00000000..6b0b6819 --- /dev/null +++ b/test/component-definition.js @@ -0,0 +1,8 @@ +var async = require('async'); +var app = require('../app'); +var ComponentDefinition = app.models.ComponentDefinition; +var ConfigFile = app.models.ConfigFile; + +describe('ComponentDefinition', function() { + +}); diff --git a/test/support.js b/test/support.js index 8deba639..967c1169 100644 --- a/test/support.js +++ b/test/support.js @@ -12,6 +12,10 @@ expectFileExists = function (file) { assert(fs.existsSync(file), file + ' does not exist'); } +getPath = function(relativePath) { + return ConfigFile.toAbsolutePath(relativePath); +} + expectValueInJSONFile = function(file, propertyPath, val) { var contents = fs.readFileSync(file, 'utf8'); var obj = JSON.parse(contents); diff --git a/test/workspace.js b/test/workspace.js index c8513f60..cb10d69d 100644 --- a/test/workspace.js +++ b/test/workspace.js @@ -3,6 +3,7 @@ var app = require('../app'); var TestDataBuilder = require('loopback-testing').TestDataBuilder; var Workspace = app.models.Workspace; var ConfigFile = app.models.ConfigFile; +var ComponentDefinition = app.models.ComponentDefinition; describe('Workspace', function() { describe('Workspace.getAvailableTemplates(callback)', function() { @@ -16,6 +17,19 @@ describe('Workspace', function() { }); }); + describe('Workspace.addComponent(options, cb)', function () { + beforeEach(givenEmptySandbox); + beforeEach(function(done) { + Workspace.addComponent({ + template: 'rest' + }, done); + }); + it('should add the static component files', function () { + expectFileExists(getPath('rest/rest.js')); + expectFileExists(getPath('rest/boot/authentication.js')); + }); + }); + describe('Workspace.createFromTemplate(templateName, callback)', function() { beforeEach(givenBasicWorkspace); beforeEach(findAllEntities); @@ -46,13 +60,23 @@ describe('Workspace', function() { expect(dataSourceNames).to.contain('mail'); expect(dataSourceNames).to.contain('db'); }); + + it('should create a runnable set of components', function (done) { + ComponentDefinition.findOne({ + where: { + name: '.' + } + }, function(err, component) { + if(err) return done(err); + done(); + }); + }); }); describe('Workspace.listUseableConnectors(cb)', function () { it('should return a list of connectors in package.json'); }); - describe('project.listAvailableConnectors(cb)', function() { before(function(done) { Workspace.listAvailableConnectors(function(err, list) {