• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RauliL / juokse / 4490148044

pending completion
4490148044

push

github

GitHub
Merge pull request #4 from RauliL/replace-moment

83 of 228 branches covered (36.4%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

242 of 622 relevant lines covered (38.91%)

6.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/cli.ts
1
import chalk, { ChalkFunction } from "chalk";
×
2
import { program } from "commander";
×
3
import { format } from "date-fns";
×
4
import fs from "fs";
×
5
import stripAnsi from "strip-ansi";
×
6

7
import { compile } from "./compiler";
×
8
import { Context } from "./context";
×
9
import { executeScript } from "./execute";
×
10
import { ExitStatus } from "./status";
×
11

12
const options = {
×
13
  stripAnsi: false,
14
  timeStampFormat: "HH:mm:ss",
15
};
16

17
export function run() {
×
18
  const context = createContext();
×
19

20
  program
×
21
    // TODO: Read version from package.json.
22
    .version("1.0.0")
23
    .usage("[options] <file> [arguments]")
24
    .option("-s, --strip-ansi", "Strip ANSI escape codes from process outputs")
25
    .option("-t, --time-stamp-format [format]", "Specify format of timestamps")
26
    .parse(process.argv);
27

28
  if (program.opts().stripAnsi) {
×
29
    options.stripAnsi = true;
×
30
  }
31
  if (program.opts().timeStampFormat != null) {
×
32
    options.timeStampFormat = program.opts().timeStampFormat;
×
33
  }
34

35
  if (!program.args.length || program.args[0] === "-") {
×
36
    compileFromStdin(context);
×
37
  } else {
38
    compileFromFile(context, program.args[0]);
×
39
  }
40
}
41

42
function createContext(): Context {
43
  const instance = new Context();
×
44

45
  // Copy environment variables from system into context.
46
  Object.assign(instance.environment, process.env);
×
47

48
  instance.on("exit", (status) => process.exit(status));
×
49

50
  instance.on("process start", ({ executable, args }) => {
×
51
    log(`${[executable, ...args].join(" ")}`, process.stdout, chalk.green);
×
52
  });
53

54
  instance.stdout.on("data", (data) => log(data, process.stdout));
×
55
  instance.stderr.on("data", (data) => log(data, process.stderr, chalk.red));
×
56

57
  return instance;
×
58
}
59

60
function compileFromFile(context: Context, filename: string) {
61
  compile(filename, fs.readFileSync(filename, "utf-8"))
×
62
    .then((nodes) =>
63
      executeScript(context, nodes, (err) => {
×
64
        log(err, process.stderr, chalk.red);
×
65
        process.exit(ExitStatus.ERROR);
×
66
      })
67
    )
68
    .catch((err) => {
69
      process.stderr.write(`${err}\n`);
×
70
      process.exit(ExitStatus.ERROR);
×
71
    });
72
}
73

74
function compileFromStdin(context: Context) {
75
  let source = "";
×
76

77
  process.stdin.resume();
×
78
  process.stdin.on("data", (buffer) => {
×
79
    source += buffer.toString();
×
80
  });
81
  process.stdin.on("end", () => {
×
82
    compile("<stdin>", source)
×
83
      .then((nodes) =>
84
        executeScript(context, nodes, (err) => {
×
85
          log(err, process.stderr, chalk.red);
×
86
          process.exit(ExitStatus.ERROR);
×
87
        })
88
      )
89
      .catch((err) => {
90
        process.stderr.write(`${err}\n`);
×
91
        process.exit(ExitStatus.ERROR);
×
92
      });
93
  });
94
}
95

96
function log(
97
  input: Buffer | Error | string,
98
  stream: NodeJS.WriteStream,
99
  color: ChalkFunction | null = null
×
100
) {
101
  // Convert buffer into string and remove any trailing new lines.
102
  const text = input.toString().replace(/(\r?\n){1,2}$/, "");
×
103

104
  if (!text.length) {
×
105
    return;
×
106
  }
107

108
  text.split(/\r?\n/).forEach((line) => {
×
109
    let timeStamp = "";
×
110

111
    if (options.timeStampFormat) {
×
112
      timeStamp = `[${chalk.grey(
×
113
        format(Date.now(), options.timeStampFormat)
114
      )}] `;
115
    }
116
    if (options.stripAnsi) {
×
117
      line = stripAnsi(line);
×
118
    }
119
    if (color) {
×
120
      line = color(line);
×
121
    }
122
    stream.write(`${timeStamp}${line.replace(/\r?\n$/, "")}\n`);
×
123
  });
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc