Skip to content

Commit

Permalink
refactor: use asset functions from code.js
Browse files Browse the repository at this point in the history
  • Loading branch information
azech-hqs committed Oct 27, 2023
1 parent f94680c commit 93dc67e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 119 deletions.
41 changes: 2 additions & 39 deletions build_filter_trees.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const lodash = require("lodash");
const utils = require("@exabyte-io/code.js/dist/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);
utils.getAssetDataFromPath(MODEL_ASSET_PATH, MODEL_FILTER_TREE, MODEL_ASSET_PATH);
utils.getAssetDataFromPath(METHOD_ASSET_PATH, METHOD_FILTER_TREE, METHOD_ASSET_PATH);
const ignore = "/* eslint-disable */\n";
const data = {
models: MODEL_FILTER_TREE,
Expand Down
166 changes: 87 additions & 79 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"homepage": "https://github.com/Exabyte-io/application-flavors",
"devDependencies": {
"@exabyte-io/eslint-config": "^2022.11.17-0",
"@exabyte-io/code.js": "2023.8.31-0",
"@exabyte-io/code.js": "git+https://github.com/Exabyte-io/code.js.git#14d84f7809b76c6fcc3339a1d48d436c15af4d50",
"chai": "^4.3.4",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^19.0.2",
Expand Down

0 comments on commit 93dc67e

Please sign in to comment.