-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
121 lines (103 loc) · 2.78 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
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
'use strict';
const fs = require('fs');
const _ = require('lodash');
class InfoJson {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
this.commands = {
info: {
options: {
json: {
usage: 'Output as JSON',
shortcut: 'j',
},
file: {
usage: 'Output to a file',
shortcut: 'f'
}
}
}
};
this.hooks = {
'before:info:info': this.patchInfoPlugin.bind(this)
};
}
getInfoPlugin() {
return this.serverless.pluginManager.getPlugins().find((element) => element.constructor.name === 'AwsInfo');
}
patchInfoPlugin() {
if (this.options.file) {
const s = fs.createWriteStream(this.options.file);
process.stdout.write = process.stderr.write = s.write.bind(s);
process.on('exit', () => {
s.end();
});
}
if (this.options.json) {
const infoPlugin = this.getInfoPlugin();
// <1.13.0
infoPlugin.display = this.displayJson.bind(this);
// >=1.13.0
infoPlugin.displayServiceInfo = this.displayJson.bind(this);
infoPlugin.displayApiKeys = () => {};
infoPlugin.displayEndpoints = () => {};
infoPlugin.displayFunctions = () => {};
infoPlugin.displayStackOutputs = () => {};
infoPlugin.displayLayers = () => {};
}
}
displayJson() {
const gatheredData = this.getInfoPlugin().gatheredData;
let data = {
info: {
service: gatheredData.info.service,
stage: gatheredData.info.stage,
region: gatheredData.info.region,
apiKeys: {},
endpoints: [],
functions: {},
layers: []
}
};
gatheredData.info.apiKeys.forEach((key) => {
data.info.apiKeys[key.name] = key.value;
});
if (gatheredData.info.endpoint) {
_.forEach(this.serverless.service.functions, (functionObject) => {
functionObject.events.forEach(event => {
if (event.http) {
let method;
let path;
if (typeof event.http === 'object') {
method = event.http.method.toUpperCase();
path = event.http.path;
} else {
method = event.http.split(' ')[0].toUpperCase();
path = event.http.split(' ')[1];
}
path = path !== '/' ? `/${path.split('/').filter(p => p !== '').join('/')}` : '';
data.info.endpoints.push({method, path});
}
});
});
}
if (gatheredData.info.layers) {
gatheredData.info.layers.forEach((layer) => {
data.info.layers[layer.name] = layer.arn;
});
}
gatheredData.info.functions.forEach((func) => {
data.info.functions[func.name] = func.deployedName;
});
if (this.options.verbose) {
data.outputs = {};
gatheredData.outputs.forEach((output) => {
data.outputs[output.OutputKey] = output.OutputValue;
});
}
this.serverless.cli.consoleLog(JSON.stringify(data, null, 2));
}
}
module.exports = InfoJson;