This repository was archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (136 loc) · 4.5 KB
/
index.js
File metadata and controls
158 lines (136 loc) · 4.5 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const fs = require('fs');
const path = require('path');
const copy = require('copy');
const install = require('gulp-install');
const zip = require('gulp-zip');
const del = require('del');
const log = require('fancy-log');
const tsPipeline = require('gulp-webpack-typescript-pipeline');
const AWS = require('aws-sdk');
const localServer = require('./localServer');
const awsLambda = require('node-aws-lambda');
const handleError = (msg) => {
if (err) throw new PluginError('es6Pipeline', err);
log(
'[es6Pipeline]',
stats.toString({
colors: true,
chunks: false
})
);
};
const doesFileExistSync = (filePath, silent) => {
try {
return fs.existsSync(filePath);
} catch (e) {
if (!silent) { console.log(e) };
return false;
}
}
const getLambdaConfig = (lambdaDir) => {
const configPath = path.join(lambdaDir, 'lambda-config.js');
return doesFileExistSync(configPath, true) ? require(configPath) : {};
};
const runLocalServer = (lambdaDir) => {
localServer.runServer(path.join(lambdaDir, 'index.ts'), getLambdaConfig(lambdaDir));
};
const registerBuildGulpTasks = (gulp, lambdaDir) => {
const lambdaName = lambdaDir.split(path.sep).pop();
const pathToLambda = path.join(lambdaDir, 'index.ts');
const distRootDir = path.join(lambdaDir, 'dist');
const dist = path.join(distRootDir, lambdaName);
const config = getLambdaConfig(lambdaDir);
if (config.region) {
AWS.region = config.region;
}
if (!config.functionName) {
config.functionName = lambdaName;
}
// check that lambda exists
if (!doesFileExistSync(pathToLambda)) {
handleError(`${pathToLambda} does not exist`);
}
const lambda = new AWS.Lambda({
apiVersion: '2015-03-31',
region: config.region || 'us-west-2',
accessKeyId: 'accessKeyId' in config ? config.accessKeyId : '',
secretAccessKey: 'secretAccessKey' in config ? config.secretAccessKey : '',
sessionToken: 'sessionToken' in config ? config.sessionToken : ''
});
const tsOptions = {
entryPoints: {
index: pathToLambda
},
outputDir: dist,
isNodeLibrary: true,
externals: [/aws-sdk/]
};
tsPipeline.registerBuildGulpTasks(gulp, tsOptions);
gulp.task('lambda:clean', (done) => {
del(distRootDir).then(() => {
done();
});
});
gulp.task('lambda:run', () => {
runLocalServer(lambdaDir, lambdaName);
});
gulp.task('lambda:info', (done) => {
lambda.getFunction({ FunctionName: lambdaName }, (err, data) => {
if (err) {
if (err.statusCode === 404) {
log(`Unable to find lambda function ${lambdaName}. Verify the lambda function name and AWS region are correct.`);
} else {
log('AWS API request failed. Check your AWS credentials and permissions.');
}
}
console.log(data);
done();
});
});
gulp.task('lambda:copyCommonFiles', (done) => {
const npmrcFile = path.join(path.dirname(pathToLambda), '.npmrc');
if (doesFileExistSync(npmrcFile, true)) {
copy(npmrcFile, dist, done);
} else {
done();
}
});
gulp.task('lambda:build', gulp.series('tsPipeline:build:release', 'lambda:copyCommonFiles'));
gulp.task('lambda:npm', () => {
return gulp
.src(path.join(path.dirname(pathToLambda), 'package.json'))
.pipe(gulp.dest(dist))
.pipe(install({ production: true }));
});
gulp.task(
'lambda:zip',
gulp.series('lambda:build', 'lambda:npm', () => {
return gulp
.src([`${dist}/**/*`, `${dist}/.*`])
.pipe(zip(`${lambdaName}.zip`))
.pipe(gulp.dest(distRootDir));
})
);
gulp.task('lambda:upload', (done) => {
awsLambda.deploy(path.join(distRootDir, `${lambdaName}.zip`), config, done);
});
gulp.task('lambda:lint', gulp.series('tsPipeline:build:dev'));
gulp.task('lambda:package', gulp.series('lambda:clean', 'lambda:zip'));
gulp.task('lambda:deploy', gulp.series('lambda:package', 'lambda:upload'));
gulp.task('lambda:init', (done) => {
copy(path.join(__dirname, 'templates', '**.*'), lambdaDir, done);
});
gulp.task('lambda', () => {
console.log(`the following tasks are available:
gulp lambda:init - set up your directory for typescript and easy debugging
gulp lambda:run - run your function inside a local express frontend
gulp lambda:package - package up your function for deployment
gulp lambda:deploy - package up, then deploy your lambda function
gulp lambda:info - display info about your deployed lambda function
`);
});
};
module.exports = {
registerBuildGulpTasks,
runLocalServer
};