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