-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_filter_trees.js
59 lines (53 loc) · 2.17 KB
/
build_filter_trees.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
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const lodash = require("lodash");
const utils = require("@mat3ra/code/dist/js/utils");
const MODEL_ASSET_PATH = path.resolve(__dirname, "models");
const METHOD_ASSET_PATH = path.resolve(__dirname, "methods");
const MODEL_FILTER_TREE = {};
const METHOD_FILTER_TREE = {};
/**
* Reads asset file and stores asset data in target object under object path which reflects the file system.
* @param {Object} targetObject - Object in which asset data should be stored
* @param {string} assetPath - Absolute path to asset file.
* @param {string} assetRoot - Path to asset root directory to construct relative path.
*/
function loadAndInsertAssetData(targetObject, assetPath, assetRoot) {
const fileContent = fs.readFileSync(assetPath, "utf8");
const data = yaml.load(fileContent, { schema: utils.JsYamlAllSchemas });
const objectPath = utils.createObjectPathFromFilePath(assetPath, assetRoot);
lodash.set(targetObject, objectPath, data);
}
/**
* Traverse asset folder recursively and load asset files.
* @param currPath {string} - path to asset directory
* @param {Object} targetObj - Object in which assets are assigned
* @param {string} assetRoot - Path to asset root directory to construct relative path.
*/
const getAssetData = (currPath, targetObj, assetRoot) => {
const branches = utils.getDirectories(currPath);
const assetFiles = utils.getFilesInDirectory(currPath, [".yml", ".yaml"], false);
assetFiles.forEach((asset) => {
try {
loadAndInsertAssetData(targetObj, path.join(currPath, asset), assetRoot);
} catch (e) {
console.log(e);
}
});
branches.forEach((b) => {
getAssetData(path.resolve(currPath, b), targetObj, assetRoot);
});
};
getAssetData(MODEL_ASSET_PATH, MODEL_FILTER_TREE, MODEL_ASSET_PATH);
getAssetData(METHOD_ASSET_PATH, METHOD_FILTER_TREE, METHOD_ASSET_PATH);
const ignore = "/* eslint-disable */\n";
const data = {
models: MODEL_FILTER_TREE,
methods: METHOD_FILTER_TREE,
};
fs.writeFileSync(
"./src/js/data/filter_trees.js",
ignore + `module.exports = ${JSON.stringify(data)}`,
"utf8",
);