Skip to content

chore/SOF-6430 #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": ["@exabyte-io/eslint-config"]
"extends": [
"@exabyte-io/eslint-config",
"plugin:import/typescript"
]
}

94 changes: 94 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"eslint-plugin-react": "7.30.0",
"eslint-plugin-simple-import-sort": "7.0.0",
"eslint-import-resolver-exports": "^1.0.0-beta.2",
"eslint-import-resolver-typescript": "^3.6.1",
"husky": "^7.0.4",
"lint-staged": "^12.1.2",
"mocha": "^9.1.3",
Expand Down
85 changes: 85 additions & 0 deletions src/utils/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import fs from "fs";
import yaml from "js-yaml";
import setValue from "lodash/set";
import path from "path";

import { JsYamlAllSchemas } from "./yaml";

const FILE_EXTENSION_TO_PROGRAMMING_LANGUAGE_MAP = {
in: "fortran",
sh: "shell",
Expand Down Expand Up @@ -76,3 +80,84 @@ export function createObjectPathFromFilePath(filePath, root) {
const parentDirs = root ? path.relative(root, dirname).split(path.sep) : [];
return [...parentDirs, basename].map((item) => `['${item}']`).join("");
}

/**
* 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.
*/
export function loadAndInsertAssetData(targetObject, assetPath, assetRoot) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file be in TypeScript already? The rest of the repo already is, it seems

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there are still a couple of JS files in src/utils: https://github.com/Exabyte-io/code.js/tree/main/src/utils

const fileContent = fs.readFileSync(assetPath, "utf8");
const data = yaml.load(fileContent, { schema: JsYamlAllSchemas });
const objectPath = createObjectPathFromFilePath(assetPath, assetRoot);
setValue(targetObject, objectPath, data);
}

/**
* Traverse asset folder recursively and load Yaml 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.
*/
export function getAssetDataFromPath(currPath, targetObj, assetRoot) {
const branches = getDirectories(currPath);
const assetFiles = getFilesInDirectory(currPath, [".yml", ".yaml"], false);

assetFiles.forEach((asset) => {
try {
loadAndInsertAssetData(targetObj, path.join(currPath, asset), assetRoot);
} catch (e) {
console.log(e);
}
});
branches.forEach((b) => {
getAssetDataFromPath(path.resolve(currPath, b), targetObj, assetRoot);
});
}

/**
* Serialize object as JS source file.
* @param {object} obj
* @param {string} obj.config - Object config to serialize.
* @param {string} obj.targetPath - Path to target JS source file.
* @param {string} obj.dataKey - Object key for data in target JS source file.
* @param {boolean} obj.debug - Whether to print messages to console.
* @param {boolean} obj.eslintDisable - Whether add eslint-disable flag to top of file.
*/
export function buildJSAssetFromConfig({
config,
targetPath,
dataKey = "",
debug = true,
eslintDisable = true,
}) {
const ignore = eslintDisable ? "/* eslint-disable */\n" : "";
const moduleExport = dataKey
? `module.exports = {${dataKey}: ` + JSON.stringify(config) + "}"
: `module.exports = ${JSON.stringify(config)}`;
fs.writeFileSync(targetPath, ignore + moduleExport + "\n", "utf8");
if (debug) console.log(`Written config to "${targetPath}"`);
}

/**
* Serialize object from Yaml as JS source file.
* @param {object} obj
* @param {string} obj.assetPath - Path to Yaml asset.
* @param {string} obj.targetPath - Path to target JS source file.
* @param {string} obj.dataKey - Object key for data in target JS source file.
* @param {boolean} obj.debug - Whether to print messages to console.
* @param {boolean} obj.eslintDisable - Whether add eslint-disable flag to top of file.
*/
export function buildJsAssetFromYaml({
assetPath,
targetPath,
dataKey = "",
debug = true,
eslintDisable = true,
}) {
const fileContent = fs.readFileSync(assetPath);
const config = yaml.load(fileContent, { schema: JsYamlAllSchemas });
buildJSAssetFromConfig({ config, targetPath, dataKey, debug: false, eslintDisable });
if (debug) console.log(`Written asset "${assetPath}" to "${targetPath}"`);
}
8 changes: 7 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { cloneClass, extendClass, extendClassStaticProps, extendThis } from "./c
import { deepClone } from "./clone";
import { refreshCodeMirror } from "./codemirror";
import {
buildJSAssetFromConfig,
buildJsAssetFromYaml,
createObjectPathFromFilePath,
formatFileSize,
getAssetDataFromPath,
getDirectories,
getFilesInDirectory,
getProgrammingLanguageFromFileExtension,
Expand All @@ -27,7 +30,7 @@ import {
sortKeysDeepForObject,
stringifyObject,
} from "./object";
import { getSchemaWithDependencies, buildNamedEntitySchema } from "./schemas";
import { buildNamedEntitySchema, getSchemaWithDependencies } from "./schemas";
import { getSearchQuerySelector } from "./selector";
import {
convertArabicToRoman,
Expand Down Expand Up @@ -62,6 +65,9 @@ export {
sortKeysDeepForObject,
stringifyObject,
getProgrammingLanguageFromFileExtension,
getAssetDataFromPath,
buildJSAssetFromConfig,
buildJsAssetFromYaml,
formatFileSize,
calculateHashFromObject,
calculateHashFromString,
Expand Down
4 changes: 1 addition & 3 deletions src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { JSONSchema } from "@exabyte-io/esse.js/schema";
import { JSONSchema7Definition } from "json-schema";
import forEach from "lodash/forEach";
import hasProperty from "lodash/has";
import isEmpty from "lodash/isEmpty";

import { JSONSchema7Definition } from "json-schema";

import { JSONSchemasInterface } from "../JSONSchemasInterface";

export * from "@exabyte-io/esse.js/lib/js/esse/schemaUtils";
Expand Down Expand Up @@ -245,7 +244,6 @@ const buildNamedEntitiesDependencies = (entities: NamedEntity[]) => {
schemaByNamedEntityName(entity.name) ||
defaultNamedEntitySchema(entity.name);
return {

...filterForGenerativeProperties(schema),
};
}),
Expand Down