-
Notifications
You must be signed in to change notification settings - Fork 3
/
runfile.js
125 lines (105 loc) · 3.11 KB
/
runfile.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const { run } = require('runjs');
const fs = require('fs');
const config = require('./config/config.json');
function isConfigured() {
const requiredFiles = ['./config/config.json', './config/firebase-config.js'];
let valid = true;
/* eslint-disable-next-line no-restricted-syntax */
for (const f of requiredFiles) {
if (!fs.existsSync(f)) {
console.log(`Missing required file "${f}". See README.md for instructions on how to generate this file`);
valid = false;
}
}
return valid;
}
function clean() {
run('rm -f lambda-messenger-template.yaml');
run('rm -f *.zip');
}
function zip() {
run('npm prune --production');
/* eslint-disable-next-line no-useless-escape */
const ts = new Date().toISOString().replace(/[:\.]/g, '-');
const zipfile = `lambda-package-${ts}.zip`;
console.log(`Zipping file ${zipfile} ...`);
run(`zip -q -r ${zipfile} node_modules/* src/index.js src/api.js config/config.json config/firebase-config.js config/serviceAccountKey.json`);
return zipfile;
}
function s3Bucket() {
run(`aws s3 mb s3://${config.LAMBDA_PACKAGE_S3_BUCKET}`);
}
function upload(zipfile) {
run(`aws s3 cp ${zipfile} s3://${config.LAMBDA_PACKAGE_S3_BUCKET}`);
}
function deploy(zipfile) {
run(
`aws cloudformation deploy \
--template-file lambda-messenger-template.yaml \
--stack-name ${config.CLOUDFORMATION_LAMBDA_STACK_NAME} \
--capabilities CAPABILITY_IAM \
--parameter-overrides \
BucketName=${config.LAMBDA_PACKAGE_S3_BUCKET} \
LambdaRole=${config.LAMBDA_IAM_ROLE} \
ZipFile=${zipfile}`,
);
}
function getAppsyncSchema() {
run(
`aws appsync get-introspection-schema \
--api-id ${config.APPSYNC_API_ID} --format JSON schema.json`,
);
}
function generateClientStubs() {
getAppsyncSchema();
run(
'npx aws-appsync-codegen generate config/posts.graphql --schema schema.json --output API.swift --target swift',
);
}
function sam() {
run(
`aws cloudformation package \
--force-upload \
--template-file config/sam.yaml \
--s3-bucket ${config.LAMBDA_PACKAGE_S3_BUCKET} \
--output-template-file lambda-messenger-template.yaml`,
);
}
function test(testFiles = 'tests/*Tests.js') {
if (isConfigured() === false) {
return;
}
run(`mocha ${testFiles} --timeout=10000`);
}
function coverage(testFiles = 'tests/*Tests.js') {
run(`npx nyc mocha ${testFiles} --timeout=10000`);
}
function all() {
if (isConfigured() === false) {
return;
}
clean();
s3Bucket();
const zipfile = zip();
upload(zipfile);
sam();
deploy(zipfile);
console.log('*********************************************************');
console.log('* After deployig this package, please run \'npm install\' *');
console.log('* (This is because the node_modules directory gets *');
console.log('* pruned to remove all dev dependencies prior to *');
console.log('* packaging a zip file) *');
console.log('*********************************************************');
}
module.exports = {
all,
clean,
s3Bucket,
zip,
upload,
sam,
deploy,
test,
coverage,
generateClientStubs,
};