-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunctions.js
54 lines (47 loc) · 1.54 KB
/
functions.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
const fs = require("fs");
const yaml = require("yaml");
const renameFunctions = (serviceName, basePath, funcs) => {
const keys = Object.keys(funcs);
const functions = {};
keys.forEach((k) => {
const func = funcs[k];
func.handler = `${basePath}${func.handler}`;
if (func.events) {
func.events = func.events.map((e) => {
if (!e.http) return e;
if (serviceName.match(/admin/)) {
e.http.path = `${serviceName.replace("-", "/")}${e.http.path}`;
} else {
e.http.path = `${serviceName}${e.http.path}`;
}
if (e.http.authorizer === "authorizer") {
e.http.authorizer = `${serviceName}-authorizer`;
}
return e;
});
}
functions[`${serviceName}-${k}`] = funcs[k];
});
return functions;
};
const getDirectories = (source) =>
fs
.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory() && dirent.name.match(/^api-/))
.map(({ name }) => name.replace("api-", ""));
module.exports = () => {
const services = getDirectories("packages");
let functions = {};
services.forEach((serviceName) => {
const basePath = `./packages/api-${serviceName}/`;
let file = fs.readFileSync(`${basePath}functions.yml`, "utf8");
file = file.replace(/\$\{self\:service\}/g, serviceName);
file = file.replace(/\$\{self\:provider.stage\}/g, "dev");
const funcs = yaml.parse(file);
functions = {
...functions,
...renameFunctions(serviceName, basePath, funcs),
};
});
return functions;
};