-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (51 loc) · 1.7 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
63
64
65
const fs = require("fs");
const yaml = require("js-yaml");
const _ = require("lodash");
const AWS = "aws";
const OFFLINE_YAML = "offline.yml";
const SSM = "SSM";
class ServerlessOfflineSSMProvider {
constructor(serverless) {
this.serverless = serverless;
const commands = serverless.providers.aws;
const isOffline = commands && _.get(commands, 'options.offline', false);
if (!isOffline) {
return;
}
try {
this.ssm = this.getOfflineSsmParameters();
} catch (err) {
throw new Error(`Unable to parse ${OFFLINE_YAML}: ${err}`);
}
this.overrideAws();
}
getOfflineSsmParameters() {
if (!fs.existsSync(OFFLINE_YAML)) {
throw new Error(`${OFFLINE_YAML} does not exist`);
}
const doc = yaml.safeLoad(fs.readFileSync(OFFLINE_YAML, "utf8"));
return doc.ssm;
}
overrideAws() {
const aws = this.serverless.getProvider(AWS);
const request = aws.request.bind(aws);
aws.request = (service, method, params, options) => {
if (service !== SSM || method !== "getParameter") {
return request(service, method, params, options);
}
const { Name } = params;
const Value = this.ssm[Name];
if (!Value) {
return Promise.reject(new Error(`SSM parameter ${Name} not found in ${OFFLINE_YAML}`));
}
return Promise.resolve({
Parameter: {
Value
}
});
};
this.serverless.setProvider(AWS, aws);
return "foo";
}
}
module.exports = ServerlessOfflineSSMProvider;