-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
104 lines (93 loc) · 3.04 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var gulp = require('gulp');
var gutil = require('gulp-util');
var del = require('del');
var rename = require('gulp-rename');
var install = require('gulp-install');
var zip = require('gulp-zip');
var AWS = require('aws-sdk');
var fs = require('fs');
var runSequence = require('run-sequence');
// First we need to clean out the dist folder and remove the compiled zip file.
gulp.task('clean', function(cb) {
del('./dist',
del('./archive.zip', cb)
);
});
// The js task could be replaced with gulp-coffee as desired.
gulp.task('js', function() {
gulp.src('index.js')
.pipe(gulp.dest('dist/'))
});
// Here we want to install npm packages to dist, ignoring devDependencies.
gulp.task('npm', function() {
gulp.src('./package.json')
.pipe(gulp.dest('./dist/'))
.pipe(install({production: true}));
});
// Next copy over environment variables managed outside of source control.
gulp.task('env', function() {
gulp.src('./config.env.production')
.pipe(rename('.env'))
.pipe(gulp.dest('./dist'))
});
// Now the dist directory is ready to go. Zip it.
gulp.task('zip', function() {
gulp.src(['dist/**/*', '!dist/package.json', 'dist/.*'])
.pipe(zip('dist.zip'))
.pipe(gulp.dest('./'));
});
// Per the gulp guidelines, we do not need a plugin for something that can be
// done easily with an existing node module. #CodeOverConfig
//
// Note: This presumes that AWS.config already has credentials. This will be
// the case if you have installed and configured the AWS CLI.
//
// See http://aws.amazon.com/sdk-for-node-js/
gulp.task('upload', function() {
// TODO: This should probably pull from package.json
AWS.config.region = 'us-east-1';
var lambda = new AWS.Lambda();
var functionName = 'video-events';
lambda.getFunction({FunctionName: functionName}, function(err, data) {
if (err) {
if (err.statusCode === 404) {
var warning = 'Unable to find lambda function ' + deploy_function + '. '
warning += 'Verify the lambda function name and AWS region are correct.'
gutil.log(warning);
} else {
var warning = 'AWS API request failed. '
warning += 'Check your AWS credentials and permissions.'
gutil.log(warning);
}
}
// This is a bit silly, simply because these five parameters are required.
var current = data.Configuration;
var params = {
FunctionName: functionName,
Handler: current.Handler,
Mode: current.Mode,
Role: current.Role,
Runtime: current.Runtime
};
fs.readFile('./dist.zip', function(err, data) {
params['FunctionZip'] = data;
lambda.uploadFunction(params, function(err, data) {
if (err) {
var warning = 'Package upload failed. '
warning += 'Check your iam:PassRole permissions.'
gutil.log(warning);
}
});
});
});
});
// The key to deploying as a single command is to manage the sequence of events.
gulp.task('default', function(callback) {
return runSequence(
['clean'],
['js', 'npm', 'env'],
['zip'],
['upload'],
callback
);
});