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

SAP / cloud-foundry-tools-api / 7573306942

18 Jan 2024 04:51PM UTC coverage: 99.393%. Remained the same
7573306942

Pull #165

github

web-flow
Merge ba8df7d26 into 3d410d68b
Pull Request #165: chore: bump @babel/traverse from 7.19.0 to 7.23.7

187 of 188 branches covered (0.0%)

Branch coverage included in aggregate %.

468 of 471 relevant lines covered (99.36%)

34.66 hits per line

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

97.37
/src/cli.ts
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
import { SpawnOptions, spawn } from "child_process";
3✔
3
import { parse } from "comment-json";
3✔
4
import * as _ from "lodash";
3✔
5
import { CliResult, CF_CMD_EXIT_CODE, CancellationToken } from "./types";
3✔
6

7
export class Cli {
3✔
8
  public static execute(args?: string[], options?: SpawnOptions, token?: CancellationToken): Promise<CliResult> {
9
    token = token || {
48✔
10
      isCancellationRequested: false,
11
      onCancellationRequested: () => {
12
        return;
15✔
13
      },
14
    };
15
    Cli.updateSpawnOptions(options);
48✔
16

17
    return new Promise<CliResult>((resolve) => {
48✔
18
      let stderr = "";
48✔
19
      let stdout = "";
48✔
20

21
      if (token.isCancellationRequested) {
48✔
22
        Cli.cliResultOnExit(stdout, resolve, stderr, CF_CMD_EXIT_CODE.CANCEL_REQ);
3✔
23
        return;
3✔
24
      }
25
      const childProcess = spawn(Cli.CF_CMD, args, options);
45✔
26

27
      childProcess.stdin.end();
45✔
28

29
      childProcess.stdout.on("data", (data: string) => {
45✔
30
        stdout = stdout.concat(data);
45✔
31
      });
32

33
      childProcess.stderr.on("data", (data: string) => {
45✔
34
        stderr = stderr.concat(data);
6✔
35
      });
36

37
      childProcess.on("exit", (code: number) => {
45✔
38
        Cli.cliResultOnExit(stdout, resolve, stderr, code);
39✔
39
      });
40

41
      childProcess.on("error", (err: any) => {
45✔
42
        const message = (
43
          _.get(err, "code") === "ENOENT" ? `${Cli.CF_CMD}: command not found` : _.get(err, "message")
6✔
44
        ) as string;
45
        resolve({ stdout: stdout, stderr: stderr, error: message, exitCode: CF_CMD_EXIT_CODE.ERROR });
6✔
46
      });
47

48
      // eslint-disable-next-line @typescript-eslint/no-unsafe-call
49
      token.onCancellationRequested(() => {
45✔
50
        childProcess.kill();
×
51
        Cli.cliResultOnExit("", resolve, "", CF_CMD_EXIT_CODE.CANCELED);
×
52
      });
53
    });
54
  }
55

56
  private static readonly CF_LOGIN_ERROR = "Not logged in. Use 'cf login' to log in.";
3✔
57
  private static readonly CF_CMD = "cf";
3✔
58

59
  private static cliResultOnExit(
60
    stdout: string,
61
    resolve: (value?: CliResult | PromiseLike<CliResult>) => void,
62
    stderr: string,
63
    code: number
64
  ) {
65
    if (stdout) {
42✔
66
      if (stdout.indexOf("error_code") > 0) {
36✔
67
        try {
6✔
68
          // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
69
          const cfErr = parse(stdout);
6✔
70
          const message = (
71
            _.get(cfErr, "code") === 10002 ? Cli.CF_LOGIN_ERROR : _.get(cfErr, "description", "Internal error occured")
6✔
72
          ) as string;
73
          resolve({ stdout: stdout, stderr: stderr, exitCode: CF_CMD_EXIT_CODE.ERROR, error: message });
6✔
74
          return;
6✔
75
        } catch (e) {
76
          // ignore, as probably not an error
77
        }
78
      } else if (stdout.startsWith("FAILED") && stdout.indexOf("Error creating request") > 0) {
30✔
79
        // most probably not logged in
80
        resolve({ stdout: stdout, stderr: stderr, error: Cli.CF_LOGIN_ERROR, exitCode: CF_CMD_EXIT_CODE.ERROR });
3✔
81
        return;
3✔
82
      } else if (/failed.*\bError\b:/g.test(stdout)) {
27✔
83
        // lgtm [js/polynomial-redos]
84
        // DEVXBUGS-5660
85
        try {
3✔
86
          parse(stdout); // ignore, well structured data - probably not an error
3✔
87
        } catch (e) {
88
          resolve({ stdout: stdout, stderr: stderr, error: stdout, exitCode: CF_CMD_EXIT_CODE.ERROR });
3✔
89
          return;
3✔
90
        }
91
      } else if (stdout.startsWith("FAILED") && stdout.indexOf("No API endpoint set") > 0) {
24✔
92
        // DEVXBUGS-6488
93
        resolve({ stdout: stdout, stderr: stderr, error: stdout, exitCode: CF_CMD_EXIT_CODE.ERROR });
3✔
94
        return;
3✔
95
      }
96
    }
97
    resolve({ stdout: stdout, stderr: stderr, exitCode: code });
27✔
98
  }
99

100
  private static updateSpawnOptions(options: SpawnOptions) {
101
    if (options) {
48✔
102
      options.env = {
6✔
103
        ...process.env,
104
        NODE_VERSION: process.versions.node,
105
        ...options.env,
106
      };
107
      // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
108
      _.defaults(options, { cwd: _.get(options, "cmd", __dirname) });
6✔
109
    }
110
  }
111
}
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