-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (56 loc) · 1.73 KB
/
index.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
const BbPromise = require("bluebird");
const _ = require("lodash");
const fs = require("fs");
class AWSApiKeyOutputPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider("aws");
this.apikeyoutput = this.serverless.service.custom.apikeyoutput;
this.commands = {};
this.hooks = {
"aws:info:displayStackOutputs": () =>
BbPromise.bind(this).then(this.getApiKey)
};
}
saveApiKeys(apiKeys) {
const outputFile = this.apikeyoutput.file || "./apikeys.json";
if (apiKeys.length > 0) {
return new Promise((resolve, reject) => {
fs.writeFile(
outputFile,
JSON.stringify({ keys: apiKeys }, null, 4),
err => {
if (err) {
this.serverless.cli.log("Error writing keys to JSON");
reject();
}
resolve();
}
);
});
}
this.serverless.cli.log("No API Keys in this project!");
return;
}
getApiKey() {
return new Promise(resolve => {
const apiKeyNames = this.serverless.service.provider.apiKeys || [];
this.provider
.request("APIGateway", "getApiKeys", { includeValues: true })
.then(allApiKeys => {
const { items } = allApiKeys;
const filteredItems = items.filter(item =>
_.includes(apiKeyNames, item.name)
);
// console.log(filteredItems);
this.saveApiKeys(filteredItems).then(() => {
resolve();
});
}).catch((err) => {
this.serverless.cli.log("Error getting API Keys. Not saving any to file");
});
});
}
}
module.exports = AWSApiKeyOutputPlugin;