Home Reference Source

cables_dev/cables_electron/src/utils/doc_util.js

  1. import { SharedDocUtil, utilProvider } from "cables-shared-api";
  2. import fs from "fs";
  3. import path from "path";
  4. import jsonfile from "jsonfile";
  5. import opsUtil from "./ops_util.js";
  6. import helper from "./helper_util.js";
  7. import cables from "../cables.js";
  8.  
  9. class DocUtil extends SharedDocUtil
  10. {
  11. getDocForOp(opName, docs = null)
  12. {
  13. if (!opName) return null;
  14. if (!this._opsUtil.isOpNameValid(opName)) return null;
  15.  
  16. const fromFile = this.getOpDocsFromFile(opName);
  17. if (fromFile) fromFile.name = opName;
  18. return fromFile;
  19. }
  20.  
  21.  
  22. getOpDocsInDir(opDir)
  23. {
  24. const opDocs = [];
  25. if (fs.existsSync(opDir))
  26. {
  27. const opJsons = helper.getFilesRecursive(opDir, ".json");
  28. for (let jsonPath in opJsons)
  29. {
  30. const opName = path.basename(jsonPath, ".json");
  31. if (opsUtil.isOpNameValid(opName))
  32. {
  33. try
  34. {
  35. const opDoc = jsonfile.readFileSync(path.join(opDir, jsonPath));
  36. opDoc.name = opName;
  37. opDocs[jsonPath] = opDoc;
  38. }
  39. catch (e)
  40. {
  41. this._log.warn("failed to parse opdocs for", opName, "from", jsonPath);
  42. }
  43. }
  44. }
  45. }
  46. return opDocs;
  47. }
  48.  
  49. makeReadable(opDocs)
  50. {
  51. const readables = super.makeReadable(opDocs);
  52. readables.forEach((opDoc) =>
  53. {
  54. const relativeDir = opsUtil.getOpSourceDir(opDoc.name, true);
  55. const absolute = opsUtil.getOpSourceDir(opDoc.name);
  56. const opDir = absolute.replace(relativeDir, "");
  57. if (opDir !== cables.getOpsPath())
  58. {
  59. opDoc.opDir = opDir;
  60. }
  61. opDoc.opDirFull = absolute;
  62. });
  63. return readables;
  64. }
  65. }
  66. export default new DocUtil(utilProvider);