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