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

RauliL / juokse / 6085018545

05 Sep 2023 01:05PM UTC coverage: 67.596% (-0.3%) from 67.923%
6085018545

push

github

RauliL
Do not colorize timestamps when requested

174 of 261 branches covered (0.0%)

Branch coverage included in aggregate %.

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

529 of 779 relevant lines covered (67.91%)

131.82 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 { sync as readPackageSync } from "read-pkg";
×
6
import stripAnsi from "strip-ansi";
×
7

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

14
type CommandLineOptions = {
15
  args: string[];
16
  echo: boolean;
17
  filename?: string;
18
  stripAnsi: boolean;
19
  timeStampFormat: "HH:mm:ss";
20
};
21

22
const parseCommandLineOptions = (): CommandLineOptions => {
×
23
  program
×
24
    .version(readPackageSync().version)
25
    .usage("[options] [file] [arguments]")
26
    .option("-s, --strip-ansi", "Strip ANSI escape codes from process outputs")
27
    .option("-t, --time-stamp-format [format]", "Specify format of timestamps")
28
    .option("--no-echo", "Do not output executed commands")
29
    .parse(process.argv);
30

31
  const opts = program.opts();
×
32
  let filename: string | undefined;
33
  let args: string[];
34

35
  if (program.args.length > 0) {
×
36
    args = program.args.slice(1);
×
37
    if (program.args[0] !== "-") {
×
38
      filename = program.args[0];
×
39
    }
40
  } else {
41
    args = [];
×
42
  }
43

44
  return {
×
45
    args,
46
    echo: opts.echo,
47
    filename,
48
    stripAnsi: opts.stripAnsi,
49
    timeStampFormat: opts.timeStampFormat ?? "HH:mm:ss",
×
50
  };
51
};
52

53
const log = (
×
54
  options: CommandLineOptions,
55
  input: Buffer | Error | string,
56
  stream: NodeJS.WriteStream,
57
  color: ChalkFunction | null = null
×
58
) => {
59
  // Convert buffer into string and remove any trailing new lines.
60
  const text = input.toString().replace(/(\r?\n){1,2}$/, "");
×
61

62
  if (!text.length) {
×
63
    return;
×
64
  }
65

66
  text.split(/\r?\n/).forEach((line) => {
×
67
    const noColor = process.env["NO_COLOR"] != null;
×
68
    let timeStamp = "";
×
69

70
    if (options.timeStampFormat) {
×
71
      let now = format(Date.now(), options.timeStampFormat);
×
72

73
      if (!noColor) {
×
74
        now = chalk.grey(now);
×
75
      }
76
      timeStamp = `[${now}] `;
×
77
    }
78
    if (options.stripAnsi) {
×
79
      line = stripAnsi(line);
×
80
    }
81
    if (color && !noColor) {
×
82
      line = color(line);
×
83
    }
84
    stream.write(`${timeStamp}${line.replace(/\r?\n$/, "")}\n`);
×
85
  });
86
};
87

88
const createContext = (options: CommandLineOptions): Context => {
×
89
  const instance = new Context();
×
90

91
  // Copy environment variables from system into context.
92
  Object.assign(instance.environment, process.env);
×
93

94
  instance.variables["0"] = options.filename ?? "";
×
95
  instance.variables["#"] = `${options.args.length}`;
×
96
  instance.variables["*"] = options.args.join(" ");
×
97
  for (let i = 0; i < options.args.length; ++i) {
×
98
    instance.variables[i + 1] = options.args[i];
×
99
  }
100

101
  instance.on("exit", (status) => process.exit(status));
×
102

103
  if (options.echo) {
×
104
    instance.on("process start", ({ executable, args }) => {
×
105
      log(
×
106
        options,
107
        `${[executable, ...args].join(" ")}`,
108
        process.stdout,
109
        chalk.green
110
      );
111
    });
112
  }
113

114
  instance.stdout.on("data", (data) => log(options, data, process.stdout));
×
115
  instance.stderr.on("data", (data) =>
×
116
    log(options, data, process.stderr, chalk.red)
×
117
  );
118

119
  return instance;
×
120
};
121

122
const execute = (
×
123
  options: CommandLineOptions,
124
  context: Context,
125
  source: string,
126
  filename: string
127
) => {
128
  compile(filename, source)
×
129
    .then((script) => executeScript(context, script))
×
130
    .catch((err) => {
131
      log(options, err, process.stderr, chalk.red);
×
132
      process.exit(ExitStatus.ERROR);
×
133
    });
134
};
135

136
export const run = () => {
×
137
  const options = parseCommandLineOptions();
×
138
  const context = createContext(options);
×
139

140
  if (options.filename) {
×
141
    execute(
×
142
      options,
143
      context,
144
      fs.readFileSync(options.filename, "utf-8"),
145
      options.filename
146
    );
147
  } else if (process.stdin.isTTY) {
×
148
    runInteractive(context);
×
149
  } else {
150
    let source = "";
×
151

152
    process.stdin.resume();
×
153
    process.stdin.on("data", (buffer) => {
×
154
      source += buffer.toString();
×
155
    });
156
    process.stdin.on("end", () => {
×
157
      execute(options, context, source, "<stdin>");
×
158
    });
159
  }
160
};
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