UNPKG

3.13 kBJavaScriptView Raw
1'use strict';
2
3const BbPromise = require('bluebird');
4const fs = require('fs-extra');
5const path = require('path')
6const CompileFunctions = require('../compile/functions/')
7
8class OpenWhiskDeployFunction {
9 constructor(serverless, options) {
10 this.serverless = serverless;
11 this.options = options || {};
12 this.provider = this.serverless.getProvider('openwhisk');
13
14 // Temporary hack until we have a better way to access existing plugins.
15 const is_package_plugin = plugin => plugin.hasOwnProperty('packageFunction')
16 this.pkg = serverless.pluginManager.getPlugins().find(is_package_plugin)
17
18 this.compileFunctions = new CompileFunctions(serverless, options)
19
20 this.hooks = {
21 'deploy:function:initialize': () => BbPromise.bind(this)
22 .then(this.validate),
23 'deploy:function:packageFunction': () => BbPromise.bind(this)
24 .then(this.packageFunction)
25 .then(this.compileFunction),
26 'deploy:function:deploy': () => BbPromise.bind(this)
27 .then(this.deployFunction)
28 .then(this.cleanup)
29 };
30 }
31
32 validate () {
33 if (!this.serverless.config.servicePath) {
34 throw new this.serverless.classes.Error('This command can only be run inside a service');
35 }
36
37 this.options.stage = this.options.stage
38 || (this.serverless.service.provider && this.serverless.service.provider.stage)
39 || 'dev';
40 this.options.region = this.options.region
41 || (this.serverless.service.provider && this.serverless.service.provider.region)
42 || 'us-east-1';
43
44 return BbPromise.resolve();
45 }
46
47 compileFunction () {
48 const functionObject = this.serverless.service.getFunction(this.options.function);
49 return this.compileFunctions.compileFunction(this.options.function, functionObject).then(action => this.action = action)
50 }
51
52 packageFunction () {
53 this.serverless.cli.log(`Packaging function: ${this.options.function}...`);
54 const functionObject = this.serverless.service.getFunction(this.options.function);
55 // sequences do not need packaging, no files to deploy
56 if (functionObject.sequence) {
57 return BbPromise.resolve();
58 }
59
60 this.serverless.service.package.individually = true
61 return this.pkg.packageFunction(this.options.function);
62 }
63
64 deployFunction (data) {
65 this.serverless.cli.log(`Deploying function: ${this.options.function}...`);
66 return this.provider.client().then(ow =>
67 ow.actions.create(this.action).then(() => {
68 this.serverless.cli.log(`Successfully deployed function: ${this.options.function}`);
69 }).catch(err => {
70 throw new this.serverless.classes.Error(
71 `Failed to deploy function (${functionHandler.actionName}) due to error: ${err.message}`
72 );
73 })
74 );
75 }
76
77 cleanup () {
78 if (this.serverless.config.servicePath) {
79 const serverlessTmpDirPath = path.join(this.serverless.config.servicePath, '.serverless');
80
81 if (this.serverless.utils.dirExistsSync(serverlessTmpDirPath)) {
82 fs.removeSync(serverlessTmpDirPath);
83 }
84 }
85
86 return BbPromise.resolve();
87 }
88}
89
90module.exports = OpenWhiskDeployFunction;