Skip to content

Commit

Permalink
Merge pull request serverless#2561 from doapp-ryanp/master
Browse files Browse the repository at this point in the history
Make `deploy function` lifecycle events more fine grain
  • Loading branch information
austencollins authored Nov 14, 2016
2 parents fe9e9b1 + 594019d commit 700eb2e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lib/classes/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,14 @@ class Utils {
}

let hasCustomResourcesDefined = false;

// check if configuration in resources.Resources is defined
if ((serverless.service.resources &&
serverless.service.resources.Resources &&
Object.keys(serverless.service.resources.Resources).length)) {
hasCustomResourcesDefined = true;
}

// check if configuration in resources.Outputs is defined
if ((serverless.service.resources &&
serverless.service.resources.Outputs &&
Expand All @@ -270,6 +272,7 @@ class Utils {
serverless.service.defaults.variableSyntax !== defaultVariableSyntax) {
hasCustomVariableSyntaxDefined = true;
}

// check if the variableSyntax in the provider section is defined
if (serverless.service.provider &&
serverless.service.provider.variableSyntax &&
Expand Down
15 changes: 10 additions & 5 deletions lib/plugins/aws/deployFunction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ class AwsDeployFunction {
Object.assign(this, validate);

this.hooks = {
'deploy:function:deploy': () => BbPromise.bind(this)
'deploy:function:initialize': () => BbPromise.bind(this)
.then(this.validate)
.then(this.logStatus)
.then(this.checkIfFunctionExists)
.then(this.zipFunction)
.then(this.checkIfFunctionExists),

'deploy:function:packageFunction': () => BbPromise.bind(this)
.then(this.packageFunction),

'deploy:function:deploy': () => BbPromise.bind(this)
.then(this.deployFunction)
.then(this.cleanup),
};
}

logStatus() {
this.serverless.cli.log(`Deploying function: ${this.options.function}...`);
BbPromise.resolve();
return BbPromise.resolve();
}

checkIfFunctionExists() {
Expand Down Expand Up @@ -62,7 +66,8 @@ class AwsDeployFunction {
return BbPromise.resolve();
}

zipFunction() {
packageFunction() {
this.serverless.cli.log(`Packaging function: ${this.options.function}...`);
return this.pkg.packageFunction(this.options.function);
}

Expand Down
39 changes: 25 additions & 14 deletions lib/plugins/aws/deployFunction/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,43 @@ describe('AwsDeployFunction', () => {

expect(awsDeployFunctionWithEmptyOptions.options).to.deep.equal({});
});
});

it('should run promise chain in order', () => {
describe('hooks', () => {
it('should run "deploy:function:initialize" promise chain in order', () => {
const validateStub = sinon
.stub(awsDeployFunction, 'validate').returns(BbPromise.resolve());
const checkIfFunctionExistsStub = sinon
.stub(awsDeployFunction, 'checkIfFunctionExists').returns(BbPromise.resolve());
const zipFunctionStub = sinon
.stub(awsDeployFunction, 'zipFunction').returns(BbPromise.resolve());

return awsDeployFunction.hooks['deploy:function:initialize']().then(() => {
expect(validateStub.calledOnce).to.equal(true);
expect(checkIfFunctionExistsStub.calledAfter(validateStub)).to.equal(true);
awsDeployFunction.checkIfFunctionExists.restore();
});
});

it('should run "deploy:function:packageFunction" promise chain in order', () => {
const packageFunctionStub = sinon
.stub(awsDeployFunction, 'packageFunction').returns(BbPromise.resolve());

return awsDeployFunction.hooks['deploy:function:packageFunction']().then(() => {
expect(packageFunctionStub.calledOnce).to.equal(true);
awsDeployFunction.packageFunction.restore();
});
});

it('should run "deploy:function:deploy" promise chain in order', () => {
const deployFunctionStub = sinon
.stub(awsDeployFunction, 'deployFunction').returns(BbPromise.resolve());
const cleanupStub = sinon
.stub(awsDeployFunction, 'cleanup').returns(BbPromise.resolve());

return awsDeployFunction.hooks['deploy:function:deploy']().then(() => {
expect(validateStub.calledOnce).to.equal(true);
expect(checkIfFunctionExistsStub.calledAfter(validateStub))
.to.equal(true);
expect(zipFunctionStub.calledAfter(checkIfFunctionExistsStub))
.to.equal(true);
expect(deployFunctionStub.calledAfter(zipFunctionStub))
.to.equal(true);
expect(deployFunctionStub.calledOnce).to.equal(true);
expect(cleanupStub.calledAfter(deployFunctionStub))
.to.equal(true);

awsDeployFunction.checkIfFunctionExists.restore();
awsDeployFunction.zipFunction.restore();
awsDeployFunction.deployFunction.restore();
awsDeployFunction.cleanup.restore();
});
Expand Down Expand Up @@ -125,7 +136,7 @@ describe('AwsDeployFunction', () => {
});
});

describe('#zipFunction()', () => {
describe('#packageFunction()', () => {
it('should zip the function', () => {
const pkg = new Package();

Expand All @@ -134,7 +145,7 @@ describe('AwsDeployFunction', () => {
const packageFunctionStub = sinon
.stub(pkg, 'packageFunction').returns(BbPromise.resolve());

return awsDeployFunction.zipFunction().then(() => {
return awsDeployFunction.packageFunction().then(() => {
expect(packageFunctionStub.calledOnce).to.be.equal(true);
expect(packageFunctionStub.args[0][0]).to.be.equal(awsDeployFunction.options.function);

Expand Down
2 changes: 2 additions & 0 deletions lib/plugins/deploy/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Deploy {
function: {
usage: 'Deploy a single function from the service',
lifecycleEvents: [
'initialize',
'packageFunction',
'deploy',
],
options: {
Expand Down
4 changes: 4 additions & 0 deletions lib/plugins/package/lib/packageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ module.exports = {
functionObject.artifact = null; // reset the current artifact

if (funcPackageConfig.artifact) {
if (process.env.SLS_DEBUG) {
this.serverless.cli.log('package.artifact is defined, skipping packaging');
}

functionObject.artifact = funcPackageConfig.artifact;
return BbPromise.resolve(funcPackageConfig.artifact);
}
Expand Down

0 comments on commit 700eb2e

Please sign in to comment.