All files / src/lib/project engineFields.ts

20.69% Statements 6/29
0% Branches 0/11
0% Functions 0/1
17.86% Lines 5/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 571x 1x   1x 1x           1x                                                                                            
import Path from "path";
import { readJSON, pathExists } from "fs-extra";
import type { EngineFieldSchema } from "store/features/engine/engineState";
import { engineRoot } from "consts";
import glob from "glob";
 
interface EngineData {
  fields?: EngineFieldSchema[];
}
 
export const loadEngineFields = async (
  projectRoot: string
): Promise<EngineFieldSchema[]> => {
  const defaultEngineJsonPath = Path.join(engineRoot, "gb", "engine.json");
  const localEngineJsonPath = Path.join(
    projectRoot,
    "assets",
    "engine",
    "engine.json"
  );
  const pluginsPath = Path.join(projectRoot, "plugins");
 
  let defaultEngine: EngineData = {};
  let localEngine: EngineData = {};
 
  try {
    localEngine = await readJSON(localEngineJsonPath);
  } catch (e) {
    defaultEngine = await readJSON(defaultEngineJsonPath);
  }
 
  let fields: EngineFieldSchema[] = [];
 
  if (localEngine && localEngine.fields) {
    fields = localEngine.fields;
  } else Iif (defaultEngine && defaultEngine.fields) {
    fields = defaultEngine.fields;
  }
 
  const enginePlugins = glob.sync(`${pluginsPath}/*/engine`);
  for (const enginePluginPath of enginePlugins) {
    const enginePluginJsonPath = Path.join(enginePluginPath, "engine.json");
    Iif (await pathExists(enginePluginJsonPath)) {
      try {
        const pluginEngine = await readJSON(enginePluginJsonPath);
        Iif (pluginEngine.fields && pluginEngine.fields.length > 0) {
          fields = fields.concat(pluginEngine.fields);
        }
      } catch (e) {
        console.warn(e);
      }
    }
  }
 
  return fields;
};