From ee8d1e8737df446d5aa457a525dbad19335dc59f Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 13:45:04 -0700 Subject: [PATCH 1/7] refactor: move asset function from appl-flavors --- src/utils/file.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/utils/file.js b/src/utils/file.js index b738774e..ef24679c 100644 --- a/src/utils/file.js +++ b/src/utils/file.js @@ -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", @@ -76,3 +80,38 @@ 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) { + 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); + }); +} From b0bd6ef79450efb8c87ec7188f2e52fd4ae9a2e3 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 13:45:42 -0700 Subject: [PATCH 2/7] chore: add plugin for typescript imports --- .eslintrc.json | 5 ++- package-lock.json | 94 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 0b5cb28d..1bee8957 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,4 +1,7 @@ { - "extends": ["@exabyte-io/eslint-config"] + "extends": [ + "@exabyte-io/eslint-config", + "plugin:import/typescript" + ] } diff --git a/package-lock.json b/package-lock.json index 6913a8e6..3581e67a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2835,6 +2835,16 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "enhanced-resolve": { + "version": "5.15.0", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.15.0.tgz", + "integrity": "sha512-LXYT42KJ7lpIKECr2mAXIaMldcNCh/7E0KBKOu4KSfkHmP+mZmSs+8V5gBAqisWBy0OO4W5Oyys0GO1Y8KtdKg==", + "dev": true, + "requires": { + "graceful-fs": "^4.2.4", + "tapable": "^2.2.0" + } + }, "enquirer": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", @@ -3183,6 +3193,52 @@ } } }, + "eslint-import-resolver-typescript": { + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/eslint-import-resolver-typescript/-/eslint-import-resolver-typescript-3.6.1.tgz", + "integrity": "sha512-xgdptdoi5W3niYeuQxKmzVDTATvLYqhpwmykwsh7f6HIOStGWEIL9iqZgQDF9u9OEzrRwR8no5q2VT+bjAujTg==", + "dev": true, + "requires": { + "debug": "^4.3.4", + "enhanced-resolve": "^5.12.0", + "eslint-module-utils": "^2.7.4", + "fast-glob": "^3.3.1", + "get-tsconfig": "^4.5.0", + "is-core-module": "^2.11.0", + "is-glob": "^4.0.3" + }, + "dependencies": { + "eslint-module-utils": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.8.0.tgz", + "integrity": "sha512-aWajIYfsqCKRDgUfjEXNN/JlrzauMuSEy5sbd7WXbtW3EH6A6MpwEh42c7qD+MqQo9QMJ6fWLAeIJynx0g6OAw==", + "dev": true, + "requires": { + "debug": "^3.2.7" + }, + "dependencies": { + "debug": { + "version": "3.2.7", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz", + "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==", + "dev": true, + "requires": { + "ms": "^2.1.1" + } + } + } + }, + "is-core-module": { + "version": "2.13.1", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", + "integrity": "sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==", + "dev": true, + "requires": { + "hasown": "^2.0.0" + } + } + } + }, "eslint-module-utils": { "version": "2.7.3", "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.7.3.tgz", @@ -3801,6 +3857,15 @@ "get-intrinsic": "^1.1.1" } }, + "get-tsconfig": { + "version": "4.7.2", + "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.7.2.tgz", + "integrity": "sha512-wuMsz4leaj5hbGgg4IvDU0bqJagpftG5l5cXIAvo8uZrqn0NJqwtfupTN00VnkQJPcIRrxYrm1Ue24btpCha2A==", + "dev": true, + "requires": { + "resolve-pkg-maps": "^1.0.0" + } + }, "glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -3940,6 +4005,23 @@ } } }, + "hasown": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", + "integrity": "sha512-vUptKVTpIJhcczKBbgnS+RtcuYMB8+oNzPK2/Hp3hanz8JmpATdmmgLgSaadVREkDm+e2giHwY3ZRkyjSIDDFA==", + "dev": true, + "requires": { + "function-bind": "^1.1.2" + }, + "dependencies": { + "function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true + } + } + }, "he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", @@ -5896,6 +5978,12 @@ "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==", "dev": true }, + "resolve-pkg-maps": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz", + "integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==", + "dev": true + }, "resolve.exports": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/resolve.exports/-/resolve.exports-2.0.2.tgz", @@ -6289,6 +6377,12 @@ } } }, + "tapable": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.1.tgz", + "integrity": "sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==", + "dev": true + }, "test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", diff --git a/package.json b/package.json index a4941544..04ffa0ac 100644 --- a/package.json +++ b/package.json @@ -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", From d757d83636bc014761bf9605d91dc4bafa49b4b1 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 13:46:00 -0700 Subject: [PATCH 3/7] style: fix imports --- src/utils/index.ts | 2 +- src/utils/schemas.ts | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index c464eecd..6b7319df 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -27,7 +27,7 @@ import { sortKeysDeepForObject, stringifyObject, } from "./object"; -import { getSchemaWithDependencies, buildNamedEntitySchema } from "./schemas"; +import { buildNamedEntitySchema, getSchemaWithDependencies } from "./schemas"; import { getSearchQuerySelector } from "./selector"; import { convertArabicToRoman, diff --git a/src/utils/schemas.ts b/src/utils/schemas.ts index 0b0f096b..68d30b07 100644 --- a/src/utils/schemas.ts +++ b/src/utils/schemas.ts @@ -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"; @@ -245,7 +244,6 @@ const buildNamedEntitiesDependencies = (entities: NamedEntity[]) => { schemaByNamedEntityName(entity.name) || defaultNamedEntitySchema(entity.name); return { - ...filterForGenerativeProperties(schema), }; }), From 14d84f7809b76c6fcc3339a1d48d436c15af4d50 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 13:48:54 -0700 Subject: [PATCH 4/7] chore: export getAssetDataFromPath from utils --- src/utils/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils/index.ts b/src/utils/index.ts index 6b7319df..c55bd6f3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -9,6 +9,7 @@ import { getDirectories, getFilesInDirectory, getProgrammingLanguageFromFileExtension, + getAssetDataFromPath, } from "./file"; import { filterEntityList } from "./filter"; import { addUnit, removeUnit, replaceUnit, setNextLinks, setUnitsHead } from "./graph"; @@ -62,6 +63,7 @@ export { sortKeysDeepForObject, stringifyObject, getProgrammingLanguageFromFileExtension, + getAssetDataFromPath, formatFileSize, calculateHashFromObject, calculateHashFromString, From 590a1d5ddbd532e513af6651176132e874f13d9c Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 14:52:30 -0700 Subject: [PATCH 5/7] chore: move buildAsset function from appl-flavors --- src/utils/file.js | 27 +++++++++++++++++++++++++++ src/utils/index.ts | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/utils/file.js b/src/utils/file.js index ef24679c..158810a1 100644 --- a/src/utils/file.js +++ b/src/utils/file.js @@ -115,3 +115,30 @@ export function getAssetDataFromPath(currPath, targetObj, assetRoot) { getAssetDataFromPath(path.resolve(currPath, b), targetObj, assetRoot); }); } + +/** + * 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 obj = yaml.load(fileContent, { schema: JsYamlAllSchemas }); + const ignore = eslintDisable ? "/* eslint-disable */\n" : ""; + fs.writeFileSync( + targetPath, + ignore + `module.exports = {${dataKey}: ` + JSON.stringify(obj) + "}\n", + "utf8", + ); + if (debug) console.log(`Written asset "${assetPath}" to "${targetPath}"`); +} diff --git a/src/utils/index.ts b/src/utils/index.ts index c55bd6f3..3fe7d74e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -10,6 +10,7 @@ import { getFilesInDirectory, getProgrammingLanguageFromFileExtension, getAssetDataFromPath, + buildJsAssetFromYaml, } from "./file"; import { filterEntityList } from "./filter"; import { addUnit, removeUnit, replaceUnit, setNextLinks, setUnitsHead } from "./graph"; @@ -64,6 +65,7 @@ export { stringifyObject, getProgrammingLanguageFromFileExtension, getAssetDataFromPath, + buildJsAssetFromYaml, formatFileSize, calculateHashFromObject, calculateHashFromString, From 71a11756c1ca73050de1fba324dafad51dc24132 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 16:38:48 -0700 Subject: [PATCH 6/7] update: handle serialization without dataKey --- src/utils/file.js | 9 ++++----- src/utils/index.ts | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/utils/file.js b/src/utils/file.js index 158810a1..c97b60db 100644 --- a/src/utils/file.js +++ b/src/utils/file.js @@ -135,10 +135,9 @@ export function buildJsAssetFromYaml({ const fileContent = fs.readFileSync(assetPath); const obj = yaml.load(fileContent, { schema: JsYamlAllSchemas }); const ignore = eslintDisable ? "/* eslint-disable */\n" : ""; - fs.writeFileSync( - targetPath, - ignore + `module.exports = {${dataKey}: ` + JSON.stringify(obj) + "}\n", - "utf8", - ); + const moduleExport = dataKey + ? `module.exports = {${dataKey}: ` + JSON.stringify(obj) + "}" + : `module.exports = ${JSON.stringify(obj)}`; + fs.writeFileSync(targetPath, ignore + moduleExport + "\n", "utf8"); if (debug) console.log(`Written asset "${assetPath}" to "${targetPath}"`); } diff --git a/src/utils/index.ts b/src/utils/index.ts index 3fe7d74e..1eff74e7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,13 +4,13 @@ import { cloneClass, extendClass, extendClassStaticProps, extendThis } from "./c import { deepClone } from "./clone"; import { refreshCodeMirror } from "./codemirror"; import { + buildJsAssetFromYaml, createObjectPathFromFilePath, formatFileSize, + getAssetDataFromPath, getDirectories, getFilesInDirectory, getProgrammingLanguageFromFileExtension, - getAssetDataFromPath, - buildJsAssetFromYaml, } from "./file"; import { filterEntityList } from "./filter"; import { addUnit, removeUnit, replaceUnit, setNextLinks, setUnitsHead } from "./graph"; From 5ac605c645c2e7f3b4a89b549f985acadfb2e911 Mon Sep 17 00:00:00 2001 From: Alexander Zech Date: Fri, 27 Oct 2023 17:11:07 -0700 Subject: [PATCH 7/7] update: add function to serialize config to JS --- src/utils/file.js | 32 ++++++++++++++++++++++++++------ src/utils/index.ts | 2 ++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/utils/file.js b/src/utils/file.js index c97b60db..7eb06e3e 100644 --- a/src/utils/file.js +++ b/src/utils/file.js @@ -116,6 +116,30 @@ export function getAssetDataFromPath(currPath, 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 @@ -133,11 +157,7 @@ export function buildJsAssetFromYaml({ eslintDisable = true, }) { const fileContent = fs.readFileSync(assetPath); - const obj = yaml.load(fileContent, { schema: JsYamlAllSchemas }); - const ignore = eslintDisable ? "/* eslint-disable */\n" : ""; - const moduleExport = dataKey - ? `module.exports = {${dataKey}: ` + JSON.stringify(obj) + "}" - : `module.exports = ${JSON.stringify(obj)}`; - fs.writeFileSync(targetPath, ignore + moduleExport + "\n", "utf8"); + const config = yaml.load(fileContent, { schema: JsYamlAllSchemas }); + buildJSAssetFromConfig({ config, targetPath, dataKey, debug: false, eslintDisable }); if (debug) console.log(`Written asset "${assetPath}" to "${targetPath}"`); } diff --git a/src/utils/index.ts b/src/utils/index.ts index 1eff74e7..4fd2cef4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,6 +4,7 @@ import { cloneClass, extendClass, extendClassStaticProps, extendThis } from "./c import { deepClone } from "./clone"; import { refreshCodeMirror } from "./codemirror"; import { + buildJSAssetFromConfig, buildJsAssetFromYaml, createObjectPathFromFilePath, formatFileSize, @@ -65,6 +66,7 @@ export { stringifyObject, getProgrammingLanguageFromFileExtension, getAssetDataFromPath, + buildJSAssetFromConfig, buildJsAssetFromYaml, formatFileSize, calculateHashFromObject,