doc.cjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Doc = void 0;
  4. class Doc {
  5. constructor(args = []) {
  6. this.content = [];
  7. this.indent = 0;
  8. if (this)
  9. this.args = args;
  10. }
  11. indented(fn) {
  12. this.indent += 1;
  13. fn(this);
  14. this.indent -= 1;
  15. }
  16. write(arg) {
  17. if (typeof arg === "function") {
  18. arg(this, { execution: "sync" });
  19. arg(this, { execution: "async" });
  20. return;
  21. }
  22. const content = arg;
  23. const lines = content.split("\n").filter((x) => x);
  24. const minIndent = Math.min(...lines.map((x) => x.length - x.trimStart().length));
  25. const dedented = lines.map((x) => x.slice(minIndent)).map((x) => " ".repeat(this.indent * 2) + x);
  26. for (const line of dedented) {
  27. this.content.push(line);
  28. }
  29. }
  30. compile() {
  31. const F = Function;
  32. const args = this?.args;
  33. const content = this?.content ?? [``];
  34. const lines = [...content.map((x) => ` ${x}`)];
  35. // console.log(lines.join("\n"));
  36. return new F(...args, lines.join("\n"));
  37. }
  38. }
  39. exports.Doc = Doc;