diff --git a/lib/classes/Service.js b/lib/classes/Service.js index 6d9d7d116..eeba0a5b6 100644 --- a/lib/classes/Service.js +++ b/lib/classes/Service.js @@ -13,6 +13,7 @@ class Service { // Default properties this.service = null; + this.serviceObject = null; this.provider = { stage: 'dev', region: 'us-east-1', @@ -64,6 +65,9 @@ class Service { if (!serverlessFile.service) { throw new ServerlessError('"service" property is missing in serverless.yml'); } + if (_.isObject(serverlessFile.service) && !serverlessFile.service.name) { + throw new ServerlessError('"service" is missing the "name" property in serverless.yml'); + } if (!serverlessFile.provider) { throw new ServerlessError('"provider" property is missing in serverless.yml'); } @@ -91,7 +95,13 @@ class Service { , {}); } - that.service = serverlessFile.service; + if (_.isObject(serverlessFile.service)) { + that.serviceObject = serverlessFile.service; + that.service = serverlessFile.service.name; + } else { + that.serviceObject = { name: serverlessFile.service }; + that.service = serverlessFile.service; + } that.custom = serverlessFile.custom; that.plugins = serverlessFile.plugins; that.resources = serverlessFile.resources; @@ -148,6 +158,14 @@ class Service { return _.merge(this, data); } + getServiceName() { + return this.serviceObject.name; + } + + getServiceObject() { + return this.serviceObject; + } + getAllFunctions() { return Object.keys(this.functions); } diff --git a/lib/classes/Service.test.js b/lib/classes/Service.test.js index 94163ef50..0493cbf78 100644 --- a/lib/classes/Service.test.js +++ b/lib/classes/Service.test.js @@ -2,6 +2,7 @@ const path = require('path'); const YAML = require('js-yaml'); +const _ = require('lodash'); const expect = require('chai').expect; const sinon = require('sinon'); const Service = require('../../lib/classes/Service'); @@ -22,6 +23,7 @@ describe('Service', () => { const serviceInstance = new Service(serverless); expect(serviceInstance.service).to.be.equal(null); + expect(serviceInstance.serviceObject).to.be.equal(null); expect(serviceInstance.provider).to.deep.equal({ stage: 'dev', region: 'us-east-1', @@ -199,6 +201,52 @@ describe('Service', () => { }); }); + it('should fail when the service name is missing', () => { + const SUtils = new Utils(); + const serverlessYaml = { + service: {}, + provider: 'aws', + }; + + SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'), + YAML.dump(serverlessYaml)); + + const serverless = new Serverless({ servicePath: tmpDirPath }); + serviceInstance = new Service(serverless); + + return serviceInstance.load().then(() => { + // if we reach this, then no error was thrown as expected + // so make assertion fail intentionally to let us know something is wrong + expect(1).to.equal(2); + }).catch(e => { + expect(e.name).to.be.equal('ServerlessError'); + }); + }); + + it('should support service objects', () => { + const SUtils = new Utils(); + const serverlessYaml = { + service: { + name: 'my-service', + foo: 'bar', + }, + provider: 'aws', + }; + + SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yaml'), + YAML.dump(serverlessYaml)); + + const serverless = new Serverless({ servicePath: tmpDirPath }); + serviceInstance = new Service(serverless); + + return serviceInstance.load().then(() => { + // if we reach this, then no error was thrown as expected + // so make assertion fail intentionally to let us know something is wrong + expect(serviceInstance.service).to.equal('my-service'); + expect(serviceInstance.serviceObject).to.deep.equal(serverlessYaml.service); + }); + }); + it('should support Serverless file with a non-aws provider', () => { const SUtils = new Utils(); const serverlessYaml = { @@ -526,6 +574,37 @@ describe('Service', () => { }); }); + describe('#getServiceName()', () => { + it('should return the service name', () => { + const serverless = new Serverless(); + const serviceInstance = new Service(serverless); + serviceInstance.serviceObject = { + name: 'my-service', + }; + + const serviceName = serviceInstance.getServiceName(); + + expect(serviceName).to.equal('my-service'); + }); + }); + + describe('#getServiceObject()', () => { + it('should return the service object with all properties', () => { + const serverless = new Serverless(); + const serviceInstance = new Service(serverless); + const testObject = { + name: 'my-service', + foo: 'bar', + }; + // Use a clone here to check for implicit reference errors + serviceInstance.serviceObject = _.cloneDeep(testObject); + + const serviceObject = serviceInstance.getServiceObject(); + + expect(serviceObject).to.deep.equal(testObject); + }); + }); + describe('#getFunction()', () => { let serviceInstance; before(() => {