-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathintegrations.build.js
More file actions
141 lines (120 loc) · 3.71 KB
/
integrations.build.js
File metadata and controls
141 lines (120 loc) · 3.71 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
const fs = require("fs"),
rimraf = require("rimraf"),
colors = require("colors"),
matter = require("gray-matter"),
{ integrationsConfig: config } = require("./utils/config"),
{ removeWhitespaces, capitalizeEach } = require("./utils/capitalizer");
colors.setTheme({
info: "blue",
help: "cyan",
warn: "yellow",
success: "green",
error: "red",
});
// For the documentation on this script look at the README.md of this repository
class Integration {
constructor(title, type, usecase, path, imageUrl) {
this.title = title;
this.type = type;
this.usecase = usecase;
this.path = path;
this.imageUrl = imageUrl;
}
isEmpty() {
return !this.title && !this.usecase;
}
}
if (fs.existsSync(config.targetFile)) {
rimraf.sync(config.targetFile);
console.warn(
`WARN: ${config.targetFile.info} already existed and was removed.`.warn
);
}
// Inform about subdirectories (this runs asynchronously)
for (const dir of config.integrationDirs) {
fs.readdir(
`docs/${dir}`,
{ encoding: "utf8", withFileTypes: true },
function (err, files) {
if (err) {
console.error(
`ERROR: Could not read directory ${("docs/" + dir).info}.`.error,
err.message.error
);
} else {
files
.filter((dirent) => dirent.isDirectory())
.map((dirent) =>
console.warn(
`WARN: Found subdirectory ${dirent.name.help}. If it contains integrations as well you must specify it explicitly.`
.warn
)
);
}
}
);
}
const itgsArray = [];
// Build integrations from files
for (const dir of config.integrationDirs) {
const integrations = [],
fileNames = fs
.readdirSync(`docs/${dir}`, { encoding: "utf8", withFileTypes: true })
.filter((dirent) => dirent.isFile() && dirent.name.endsWith(".md"))
.map((dirent) => dirent.name.split(".").slice(0, -1).join("."));
for (const fileName of fileNames) {
const content = fs.readFileSync(`docs/${dir}/${fileName}.md`, {
encoding: "utf8",
}),
attributes = matter(content).data,
integration = new Integration(
attributes.title,
attributes.type,
attributes.usecase
);
if (integration.isEmpty()) {
console.warn(
`WARN: Skipping: ${fileName.help}. Frontmatter does not provide required fields.`
.warn,
`Title: ${integration.title}`,
`Description: ${integration.usecase}`
);
} else {
const imageUrl = `img/integrationIcons/${integration.title}.svg`;
if (fs.existsSync(`static/${imageUrl}`)) {
integration.imageUrl = imageUrl;
} else {
integration.imageUrl = config.defaultIcon;
console.warn(
`WARN: Could not resolve ${imageUrl.info}. Using default image.`.warn
);
}
integration.path = `docs/${dir}/${fileName}`;
integrations.push(integration);
console.log(`SUCCESS: Created integration for ${fileName.help}.`.success);
}
}
itgsArray.push([dir, integrations]);
}
const itgsMap = new Map(itgsArray),
itgsStringArray = [],
itgKeys = [];
itgsMap.forEach((itgObject, itgName) => {
const constantName = removeWhitespaces(capitalizeEach(itgName));
itgKeys.push(constantName);
itgsStringArray.push(`
export const ${constantName} = ${JSON.stringify(itgObject)};
`);
});
itgsStringArray.push(`export default { ${itgKeys.join(",")} };`);
fs.writeFile(`${config.targetFile}`, itgsStringArray.join(""), function (err) {
if (err) {
console.error(
`ERROR: Could not build ${config.targetFile.help}.`.error,
err.message.error
);
}
});