webpipe-js 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,7 +33,8 @@ __export(index_exports, {
33
33
  getPipelineRanges: () => getPipelineRanges,
34
34
  getVariableRanges: () => getVariableRanges,
35
35
  parseProgram: () => parseProgram,
36
- parseProgramWithDiagnostics: () => parseProgramWithDiagnostics
36
+ parseProgramWithDiagnostics: () => parseProgramWithDiagnostics,
37
+ prettyPrint: () => prettyPrint
37
38
  });
38
39
  module.exports = __toCommonJS(index_exports);
39
40
 
@@ -587,6 +588,116 @@ var ParseFailure = class extends Error {
587
588
  this.at = at;
588
589
  }
589
590
  };
591
+ function prettyPrint(program) {
592
+ const lines = [];
593
+ program.configs.forEach((config) => {
594
+ lines.push(`config ${config.name} {`);
595
+ config.properties.forEach((prop) => {
596
+ const value = formatConfigValue(prop.value);
597
+ lines.push(` ${prop.key}: ${value}`);
598
+ });
599
+ lines.push("}");
600
+ lines.push("");
601
+ });
602
+ program.variables.forEach((variable) => {
603
+ lines.push(`${variable.varType} ${variable.name} = \`${variable.value}\``);
604
+ });
605
+ if (program.variables.length > 0) lines.push("");
606
+ program.pipelines.forEach((pipeline) => {
607
+ lines.push(`pipeline ${pipeline.name} =`);
608
+ pipeline.pipeline.steps.forEach((step) => {
609
+ lines.push(formatPipelineStep(step));
610
+ });
611
+ lines.push("");
612
+ });
613
+ program.routes.forEach((route) => {
614
+ lines.push(`${route.method} ${route.path}`);
615
+ const pipelineLines = formatPipelineRef(route.pipeline);
616
+ pipelineLines.forEach((line) => lines.push(line));
617
+ lines.push("");
618
+ });
619
+ program.describes.forEach((describe) => {
620
+ lines.push(`describe "${describe.name}"`);
621
+ describe.mocks.forEach((mock) => {
622
+ lines.push(` with mock ${mock.target} returning \`${mock.returnValue}\``);
623
+ });
624
+ lines.push("");
625
+ describe.tests.forEach((test) => {
626
+ lines.push(` it "${test.name}"`);
627
+ test.mocks.forEach((mock) => {
628
+ lines.push(` with mock ${mock.target} returning \`${mock.returnValue}\``);
629
+ });
630
+ lines.push(` when ${formatWhen(test.when)}`);
631
+ if (test.input) {
632
+ lines.push(` with input \`${test.input}\``);
633
+ }
634
+ test.conditions.forEach((condition) => {
635
+ const condType = condition.conditionType.toLowerCase();
636
+ const jqPart = condition.jqExpr ? ` \`${condition.jqExpr}\`` : "";
637
+ lines.push(` ${condType} ${condition.field}${jqPart} ${condition.comparison} ${condition.value}`);
638
+ });
639
+ lines.push("");
640
+ });
641
+ });
642
+ return lines.join("\n").trim();
643
+ }
644
+ function formatConfigValue(value) {
645
+ switch (value.kind) {
646
+ case "String":
647
+ return `"${value.value}"`;
648
+ case "EnvVar":
649
+ return value.default ? `$${value.var} || "${value.default}"` : `$${value.var}`;
650
+ case "Boolean":
651
+ return value.value.toString();
652
+ case "Number":
653
+ return value.value.toString();
654
+ }
655
+ }
656
+ function formatPipelineStep(step, indent = " ") {
657
+ if (step.kind === "Regular") {
658
+ return `${indent}|> ${step.name}: ${formatStepConfig(step.config)}`;
659
+ } else {
660
+ const lines = [`${indent}|> result`];
661
+ step.branches.forEach((branch) => {
662
+ const branchName = branch.branchType.kind === "Ok" ? "ok" : branch.branchType.kind === "Default" ? "default" : branch.branchType.name;
663
+ lines.push(`${indent} ${branchName}(${branch.statusCode}):`);
664
+ branch.pipeline.steps.forEach((branchStep) => {
665
+ lines.push(formatPipelineStep(branchStep, indent + " "));
666
+ });
667
+ });
668
+ return lines.join("\n");
669
+ }
670
+ }
671
+ function formatStepConfig(config) {
672
+ if (config.includes("`")) {
673
+ return `\`${config}\``;
674
+ } else if (config.includes(" ") || config.includes("\n")) {
675
+ return `"${config}"`;
676
+ } else {
677
+ return config;
678
+ }
679
+ }
680
+ function formatPipelineRef(ref) {
681
+ if (ref.kind === "Named") {
682
+ return [` |> pipeline: ${ref.name}`];
683
+ } else {
684
+ const lines = [];
685
+ ref.pipeline.steps.forEach((step) => {
686
+ lines.push(formatPipelineStep(step));
687
+ });
688
+ return lines;
689
+ }
690
+ }
691
+ function formatWhen(when) {
692
+ switch (when.kind) {
693
+ case "CallingRoute":
694
+ return `calling ${when.method} ${when.path}`;
695
+ case "ExecutingPipeline":
696
+ return `executing pipeline ${when.name}`;
697
+ case "ExecutingVariable":
698
+ return `executing variable ${when.varType} ${when.name}`;
699
+ }
700
+ }
590
701
  if (import_meta.url === `file://${process.argv[1]}`) {
591
702
  (async () => {
592
703
  const fs = await import("fs/promises");
@@ -605,5 +716,6 @@ if (import_meta.url === `file://${process.argv[1]}`) {
605
716
  getPipelineRanges,
606
717
  getVariableRanges,
607
718
  parseProgram,
608
- parseProgramWithDiagnostics
719
+ parseProgramWithDiagnostics,
720
+ prettyPrint
609
721
  });
package/dist/index.d.cts CHANGED
@@ -127,5 +127,6 @@ declare function getVariableRanges(text: string): Map<string, {
127
127
  start: number;
128
128
  end: number;
129
129
  }>;
130
+ declare function prettyPrint(program: Program): string;
130
131
 
131
- export { type Condition, type Config, type ConfigProperty, type ConfigValue, type Describe, type DiagnosticSeverity, type It, type Mock, type NamedPipeline, type ParseDiagnostic, type Pipeline, type PipelineRef, type PipelineStep, type Program, type ResultBranch, type ResultBranchType, type Route, type Variable, type When, getPipelineRanges, getVariableRanges, parseProgram, parseProgramWithDiagnostics };
132
+ export { type Condition, type Config, type ConfigProperty, type ConfigValue, type Describe, type DiagnosticSeverity, type It, type Mock, type NamedPipeline, type ParseDiagnostic, type Pipeline, type PipelineRef, type PipelineStep, type Program, type ResultBranch, type ResultBranchType, type Route, type Variable, type When, getPipelineRanges, getVariableRanges, parseProgram, parseProgramWithDiagnostics, prettyPrint };
package/dist/index.d.ts CHANGED
@@ -127,5 +127,6 @@ declare function getVariableRanges(text: string): Map<string, {
127
127
  start: number;
128
128
  end: number;
129
129
  }>;
130
+ declare function prettyPrint(program: Program): string;
130
131
 
131
- export { type Condition, type Config, type ConfigProperty, type ConfigValue, type Describe, type DiagnosticSeverity, type It, type Mock, type NamedPipeline, type ParseDiagnostic, type Pipeline, type PipelineRef, type PipelineStep, type Program, type ResultBranch, type ResultBranchType, type Route, type Variable, type When, getPipelineRanges, getVariableRanges, parseProgram, parseProgramWithDiagnostics };
132
+ export { type Condition, type Config, type ConfigProperty, type ConfigValue, type Describe, type DiagnosticSeverity, type It, type Mock, type NamedPipeline, type ParseDiagnostic, type Pipeline, type PipelineRef, type PipelineStep, type Program, type ResultBranch, type ResultBranchType, type Route, type Variable, type When, getPipelineRanges, getVariableRanges, parseProgram, parseProgramWithDiagnostics, prettyPrint };
package/dist/index.mjs CHANGED
@@ -547,6 +547,116 @@ var ParseFailure = class extends Error {
547
547
  this.at = at;
548
548
  }
549
549
  };
550
+ function prettyPrint(program) {
551
+ const lines = [];
552
+ program.configs.forEach((config) => {
553
+ lines.push(`config ${config.name} {`);
554
+ config.properties.forEach((prop) => {
555
+ const value = formatConfigValue(prop.value);
556
+ lines.push(` ${prop.key}: ${value}`);
557
+ });
558
+ lines.push("}");
559
+ lines.push("");
560
+ });
561
+ program.variables.forEach((variable) => {
562
+ lines.push(`${variable.varType} ${variable.name} = \`${variable.value}\``);
563
+ });
564
+ if (program.variables.length > 0) lines.push("");
565
+ program.pipelines.forEach((pipeline) => {
566
+ lines.push(`pipeline ${pipeline.name} =`);
567
+ pipeline.pipeline.steps.forEach((step) => {
568
+ lines.push(formatPipelineStep(step));
569
+ });
570
+ lines.push("");
571
+ });
572
+ program.routes.forEach((route) => {
573
+ lines.push(`${route.method} ${route.path}`);
574
+ const pipelineLines = formatPipelineRef(route.pipeline);
575
+ pipelineLines.forEach((line) => lines.push(line));
576
+ lines.push("");
577
+ });
578
+ program.describes.forEach((describe) => {
579
+ lines.push(`describe "${describe.name}"`);
580
+ describe.mocks.forEach((mock) => {
581
+ lines.push(` with mock ${mock.target} returning \`${mock.returnValue}\``);
582
+ });
583
+ lines.push("");
584
+ describe.tests.forEach((test) => {
585
+ lines.push(` it "${test.name}"`);
586
+ test.mocks.forEach((mock) => {
587
+ lines.push(` with mock ${mock.target} returning \`${mock.returnValue}\``);
588
+ });
589
+ lines.push(` when ${formatWhen(test.when)}`);
590
+ if (test.input) {
591
+ lines.push(` with input \`${test.input}\``);
592
+ }
593
+ test.conditions.forEach((condition) => {
594
+ const condType = condition.conditionType.toLowerCase();
595
+ const jqPart = condition.jqExpr ? ` \`${condition.jqExpr}\`` : "";
596
+ lines.push(` ${condType} ${condition.field}${jqPart} ${condition.comparison} ${condition.value}`);
597
+ });
598
+ lines.push("");
599
+ });
600
+ });
601
+ return lines.join("\n").trim();
602
+ }
603
+ function formatConfigValue(value) {
604
+ switch (value.kind) {
605
+ case "String":
606
+ return `"${value.value}"`;
607
+ case "EnvVar":
608
+ return value.default ? `$${value.var} || "${value.default}"` : `$${value.var}`;
609
+ case "Boolean":
610
+ return value.value.toString();
611
+ case "Number":
612
+ return value.value.toString();
613
+ }
614
+ }
615
+ function formatPipelineStep(step, indent = " ") {
616
+ if (step.kind === "Regular") {
617
+ return `${indent}|> ${step.name}: ${formatStepConfig(step.config)}`;
618
+ } else {
619
+ const lines = [`${indent}|> result`];
620
+ step.branches.forEach((branch) => {
621
+ const branchName = branch.branchType.kind === "Ok" ? "ok" : branch.branchType.kind === "Default" ? "default" : branch.branchType.name;
622
+ lines.push(`${indent} ${branchName}(${branch.statusCode}):`);
623
+ branch.pipeline.steps.forEach((branchStep) => {
624
+ lines.push(formatPipelineStep(branchStep, indent + " "));
625
+ });
626
+ });
627
+ return lines.join("\n");
628
+ }
629
+ }
630
+ function formatStepConfig(config) {
631
+ if (config.includes("`")) {
632
+ return `\`${config}\``;
633
+ } else if (config.includes(" ") || config.includes("\n")) {
634
+ return `"${config}"`;
635
+ } else {
636
+ return config;
637
+ }
638
+ }
639
+ function formatPipelineRef(ref) {
640
+ if (ref.kind === "Named") {
641
+ return [` |> pipeline: ${ref.name}`];
642
+ } else {
643
+ const lines = [];
644
+ ref.pipeline.steps.forEach((step) => {
645
+ lines.push(formatPipelineStep(step));
646
+ });
647
+ return lines;
648
+ }
649
+ }
650
+ function formatWhen(when) {
651
+ switch (when.kind) {
652
+ case "CallingRoute":
653
+ return `calling ${when.method} ${when.path}`;
654
+ case "ExecutingPipeline":
655
+ return `executing pipeline ${when.name}`;
656
+ case "ExecutingVariable":
657
+ return `executing variable ${when.varType} ${when.name}`;
658
+ }
659
+ }
550
660
  if (import.meta.url === `file://${process.argv[1]}`) {
551
661
  (async () => {
552
662
  const fs = await import("fs/promises");
@@ -564,5 +674,6 @@ export {
564
674
  getPipelineRanges,
565
675
  getVariableRanges,
566
676
  parseProgram,
567
- parseProgramWithDiagnostics
677
+ parseProgramWithDiagnostics,
678
+ prettyPrint
568
679
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpipe-js",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Web Pipe parser",
5
5
  "license": "ISC",
6
6
  "author": "William Cotton",