time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

makeProgram.js (2577B)


      1 import fs from "fs";
      2 import util from "util";
      3 import { run } from "./utils.js";
      4 import { Command } from "@commander-js/extra-typings";
      5 import open from "open";
      6 import { parse } from "../public/parse.js";
      7 import { red } from "./colors.js";
      8 export function makeProgram() {
      9     const program = new Command();
     10     const loadSrc = ({ filename, inline, }) => {
     11         let src = "";
     12         if (filename !== undefined && inline !== undefined) {
     13             program.error("Only one of filename and eval string should be set.");
     14         }
     15         else if (filename !== undefined) {
     16             src = fs.readFileSync(filename, "utf-8");
     17         }
     18         else if (inline !== undefined) {
     19             src = inline;
     20         }
     21         else {
     22             program.error("One of filename and eval string should be set.");
     23         }
     24         return src;
     25     };
     26     program
     27         .command("run")
     28         .arguments("[filename]")
     29         .option("-e, --eval <code>", "run a given squiggle code string instead of a file")
     30         .option("-t --time", "output the time it took to evaluate the code")
     31         .option("-q, --quiet", "don't output the results and bindings")
     32         .option("-b, --show-bindings", "show bindings even if the result is present")
     33         .action(async (filename, options) => {
     34         let output = "RESULT_OR_BINDINGS";
     35         if (options.quiet && options.showBindings) {
     36             program.error("--quiet and --show-bindings can't be set at the same time.");
     37         }
     38         else if (options.quiet) {
     39             output = "NONE";
     40         }
     41         else if (options.showBindings) {
     42             output = "RESULT_AND_BINDINGS";
     43         }
     44         const src = loadSrc({ filename, inline: options.eval });
     45         const sampleCount = process.env["SAMPLE_COUNT"];
     46         await run({ src, filename, output, measure: options.time, sampleCount });
     47     });
     48     program
     49         .command("parse")
     50         .arguments("[filename]")
     51         .option("-e, --eval <code>", "parse a given squiggle code string instead of a file")
     52         .action((filename, options) => {
     53         const src = loadSrc({ filename, inline: options.eval });
     54         const parseResult = parse(src);
     55         if (parseResult.ok) {
     56             console.log(util.inspect(parseResult.value, { depth: Infinity, colors: true }));
     57         }
     58         else {
     59             console.log(red(parseResult.value.toString()));
     60         }
     61     });
     62     program.command("playground").action(() => {
     63         open("https://www.squiggle-language.com/playground");
     64     });
     65     return program;
     66 }
     67 //# sourceMappingURL=makeProgram.js.map