Created
December 8, 2016 09:23
-
-
Save nnnikolay/bed4a313239b649ec73dbf2bbe379059 to your computer and use it in GitHub Desktop.
from one of my AWS Lambda projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gulp = require('gulp'), | |
sourcemaps = require('gulp-sourcemaps'), | |
babel = require('gulp-babel'), | |
symlink = require("gulp-sym"), | |
eslint = require('gulp-eslint'), | |
del = require('del'), | |
install = require('gulp-install'), | |
awsLambda = require('node-aws-lambda'), | |
runSequence = require('run-sequence'), | |
zip = require('gulp-zip'), | |
path = require('path'); | |
const paths = { | |
src: ['src/**/*.js'], | |
dst: 'build', | |
// Must be absolute or relative to source map | |
sourceRoot: path.join(__dirname, 'build'), | |
}; | |
// Transpile ES6 -> ES5 | |
gulp.task('build', function (cb) { | |
gulp.src(paths.src) | |
.pipe(sourcemaps.init()) | |
.pipe(babel()) | |
.pipe(sourcemaps.write('.', | |
{ sourceRoot: paths.sourceRoot })) | |
.pipe(gulp.dest(paths.dst)) | |
// takes in a callback so the engine knows when it'll be done | |
cb() | |
}); | |
// send a test message into DEV queue | |
// you can run this command with --template argument | |
// which will force client to use ./message-samples/with.template.js | |
gulp.task('send-test-message', function(cb) { | |
var aws = require('aws-sdk') | |
var yargs = require('yargs').argv | |
var template = yargs.template ? 'with.template.js' : 'without.template.js' | |
require('dotenv').config() | |
var message = require('./message-samples/' + template) | |
// replace recipient to env variable | |
message.body.to = message.body.to.map(function(el) { | |
el.email = process.env.MYMORIA_EMAIL_TO | |
return el | |
}) | |
var sqs = new aws.SQS({ | |
region: process.env.AWS_REGION, | |
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | |
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | |
}); | |
sqs.sendMessage({ | |
QueueUrl: process.env.AWS_QUEUE_URL + '/' + process.env.AWS_ACCOUNT_ID | |
+ '/' + process.env.AWS_QUEUE_NAME, | |
MessageBody: JSON.stringify(message) | |
}, function(err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else console.log(data); // successful response | |
}) | |
cb() | |
}) | |
// Run the lambda function on local docker env | |
gulp.task('local-build', function(cb) { | |
return runSequence( | |
'clean', | |
'lint', | |
'build', | |
[ 'node-mods-install', 'send-test-message' ], | |
'copy-js', // copy from build/ to dist/ | |
'zip', | |
'run-local-docker-instance', | |
callback | |
); | |
}); | |
// it does not work so good | |
gulp.task('run-local-docker-instance', function() { | |
var dockerLambda = require('docker-lambda') | |
require('dotenv').config(); | |
// docker command args | |
lambdaCallbackResult = dockerLambda({ | |
taskDir: __dirname + '/dist/', | |
addEnvVars: true, | |
returnSpawnResult: true | |
}) | |
}) | |
// upload current lambda function to the AWS lambda dev environment | |
gulp.task('upload', function(callback) { | |
require('dotenv').config(); | |
// TODO: implement functionality release vs deploy | |
// it's all about publish property in the config | |
awsLambda.deploy('./dist.zip', require("./lambda-config.js"), callback); | |
}); | |
// deploy to AWS lambda | |
gulp.task('deploy', function(callback) { | |
return runSequence( | |
'clean', // remove build/ | |
'lint', | |
'build', // transpile from src/ to build/ | |
'node-mods-install', // install modules into dist/ | |
'copy-js', // copy from build/ to dist/ | |
'zip', | |
'upload', | |
callback | |
); | |
}); | |
// install for deployment script: del gulp-install node-aws-lambda run-sequence | |
gulp.task('clean', function() { | |
return del(['./build', './dist', './dist.zip']); | |
}); | |
// copy build files into dist/ folder | |
gulp.task('copy-js', function() { | |
return gulp.src(['build/**/*.js', '.env']) | |
// gulp.src('.env') | |
.pipe(gulp.dest('dist/')); | |
}); | |
// copy package.json and run npm i right there | |
gulp.task('node-mods-install', function() { | |
return gulp.src('./package.json') | |
.pipe(gulp.dest('dist/')) | |
.pipe(install({production: true})); | |
}); | |
// archive dist/ folder | |
gulp.task('zip', function() { | |
return gulp.src(['dist/**/*', '!dist/package.json', 'dist/.*']) | |
.pipe(zip('dist.zip')) | |
.pipe(gulp.dest('./')); | |
}); | |
// Lint JavaScript files | |
gulp.task('lint', () => { | |
// ESLint ignores files with "node_modules" paths. | |
// So, it's best to have gulp ignore the directory as well. | |
// Also, Be sure to return the stream from the task; | |
// Otherwise, the task may end before the stream has finished. | |
return gulp.src(['src/**/*.js']) | |
// eslint() attaches the lint output to the "eslint" property | |
// of the file object so it can be used by other modules. | |
.pipe(eslint()) | |
// eslint.format() outputs the lint results to the console. | |
// Alternatively use eslint.formatEach() (see Docs). | |
.pipe(eslint.format()) | |
// To have the process exit with an error code (1) on | |
// lint error, return the stream and pipe to failAfterError last. | |
.pipe(eslint.failAfterError()) | |
}); | |
// Install the GIT hooks | |
gulp.task("hooks", function() { | |
if (process.env.NODE_ENV != 'production') { | |
return gulp | |
.src([ "./git-hooks/pre-commit", "./git-hooks/post-merge" ]) | |
.pipe( | |
symlink([ ".git/hooks/pre-commit", ".git/hooks/post-merge" ], { | |
relative: true, | |
force: true | |
}) | |
); | |
} | |
}); | |
// Default task | |
gulp.task('default', ['lint']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment