-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerateConfig.js
74 lines (66 loc) · 1.73 KB
/
generateConfig.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
const fs = require("fs");
const path = require("path");
// DON'T EDIT OR DELETE THIS FILE. //
let config = {
examples: {},
basic: {},
utils: {},
};
function manageDir(directory, json, target = "", relative) {
fs.readdirSync(directory, { withFileTypes: true }).forEach((file) => {
if (file.isDirectory()) {
manageDir(
path.join(directory, file.name),
json,
target + "/" + file.name,
relative
);
if (!json["dirs"].includes(target + "/" + file.name))
json["dirs"].unshift(target + "/" + file.name);
} else {
let dir = path.relative(
path.join(__dirname, relative),
path.join(directory)
);
json["files"][file.name] = dir.split(/\\|\//g).slice(1).join("/");
}
});
}
function format({ file }) {
return Object.fromEntries(
Object.entries(file).filter((val) => {
val[1].dirs = val[1].dirs.sort(
(a, b) => a.split("/").length - b.split("/").length
);
return true;
})
);
}
function saveFile(json, dir) {
const output = {};
Object.entries(json).forEach(([k, v]) => {
output[k] = format({ file: v });
});
fs.writeFileSync(`${dir}/config.json`, JSON.stringify(output, null, 2));
}
function init(d, config) {
fs.readdirSync(path.join(__dirname, d), {
withFileTypes: true,
}).forEach(function (file) {
if (file.isDirectory()) {
config[file.name] = { files: {}, dirs: [] };
return manageDir(
path.join(__dirname, d, file.name),
config[file.name],
"",
d
);
}
return;
});
}
init("./examples", config.basic);
init("./example-processor/templates", config.examples);
init("./utils", config.utils);
saveFile(config, process.cwd());
// END OF FILE //