Code coverage report for Server.ts

# All files Server.ts

65.59% Statements 61/93
50% Branches 28/56
71.43% Functions 10/14
65.59% Lines 61/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    1741x
    1x
    1x

    1x

    11x
    11x

    11x
    11x
    11x
    11x

    4x
    4x
    4x
    4x

    17x

    11x

    11x

    11x

    11x

    11x

    11x
    11x

    11x

    11x

    11x

    11x

    11x

    11x

    11x

    11x
    11x
    11x

    11x
    4x
    4x

    11x
    4x
    7x

    11x

    12x
    12x
    12x

    12x

    8x
    8x

    8x

    12x
    12x

    12x
    10x

    2x

    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x

    2x
    2x

     import * as vscode from 'vscode';
    import { spawn, exec, ChildProcess } from 'child_process';
    import { platform } from 'os';

    export default class Server {
      private relativePath: string;
      private router?: string;
      private phpPath?: string;
      private phpConfigPath?: string;
      private extensionPath: string;
      private port: number;
      private ip: string;
      private terminal?: ChildProcess;
      private running: boolean = false;
      private outputChanneL: vscode.OutputChannel = vscode.window.createOutputChannel("PHP Server");

      constructor(ip: string, port: number, relativePath: string, extensionPath: string) {
        this.ip = ip;
        this.port = port;
        this.relativePath = relativePath;
        this.extensionPath = extensionPath;
      }

      public shutDown() {
        Eif (this.running && this.terminal !== undefined) {
          this.terminal.kill();
          this.terminal = undefined;
          this.running = false;
        }
      }

      public isRunning(): boolean {
        return this.running && this.terminal !== undefined;
      }

      public setPhpPath(phpPath: string) {
        this.phpPath = phpPath;
      }

      public setPhpConfigPath(phpConfigPath: string) {
        this.phpConfigPath = phpConfigPath;
      }

      public setRouter(router: string) {
        this.router = router;
      }

      public run() {
        Iif (this.isRunning()) {
          vscode.window.showErrorMessage("Server is already running!");
          return;
        }

        this.relativePath

        this.outputChanneL.clear();
        this.outputChanneL.show();

        this.terminal = spawn(this.phpPath ? this.phpPath : "php", this.argsToArray(), {
          cwd: vscode.workspace.rootPath
        });

        this.terminal.stdout?.on("data", (data: Buffer) => {
          this.outputChanneL.appendLine(data.toString());
        });
        this.terminal.stderr?.on("data", (data: Buffer) => {
          this.outputChanneL.appendLine(data.toString());
        });
        this.terminal.on("error", (err: Error) => {
          this.running = false;
          vscode.window.showErrorMessage(`Server error: ${err.stack}`);
        });
        this.terminal.on("close", code => {
          this.shutDown();
          vscode.window.showInformationMessage("Server Stopped");
        });

        this.running = true;

        vscode.window.showInformationMessage("Serving Project");
      }

      private argsToArray(): string[] {
        const args: string[] = ["-S", `${this.ip}:${this.port}`];
        args.push("-t");
        args.push(this.relativePath);

        if (this.phpConfigPath) {
          args.push("-c");
          args.push(this.phpConfigPath);
        }

        if (this.router) {
          args.push(this.router);
        } else Iif (platform() === "win32") {
          args.push(`${this.extensionPath}\\src\\logger.php`);
          process.env.PHP_SERVER_RELATIVE_PATH = this.relativePath;
        }
        return args;
      }

      public execBrowser(browser: string, fullFileName?: string) {
        const ip = this.ip;
        const port = this.port;
        const fileName = this.fullToRelativeFileName(fullFileName);

        switch (browser.toLowerCase()) {
          case "firefox":
            Eif (platform() === 'linux') {
              browser = `${browser} http://${ip}:${port}/${fileName}`;
            } else if (platform() === "win32") {
              browser = `start ${browser} http://${ip}:${port}/${fileName}`;
            } else if (platform() === 'darwin') {
              browser = `open -a "Firefox" http://${ip}:${port}/${fileName}`;
            } else {
              browser = '';
            }
            break;
          case "chrome":
            if (platform() === 'linux') {
              browser = `google-chrome http://${ip}:${port}/${fileName}`;
            } else if (platform() === "win32") {
              browser = `start ${browser} http://${ip}:${port}/${fileName}`;
            } else if (platform() === 'darwin') {
              browser = `open -a "Google Chrome" http://${ip}:${port}/${fileName}`;
            } else {
              browser = '';
            }
            break;
          case "edge":
            if (platform() === 'win32') {
              browser = `start microsoft-edge:http://${ip}:${port}/${fileName}`;
            } else {
              browser = '';
            }
            break;
          case "safari":
            if (platform() === 'darwin') {
              browser = `open -a "Safari" http://${ip}:${port}/${fileName}`;
            } else {
              browser = '';
            }
        }
        Eif (browser !== "") {
          exec(browser);
        }

      }

      private fullToRelativeFileName(fullFileName?: string): string {
        if (!fullFileName) {
          return '';
        }

        let i = 0;

        Eif (this.relativePath.length > 1) {
          let char = platform() === 'win32' ? '\\' : '/';
          let relativePath = this.relativePath;
          Eif (relativePath.startsWith('.' + char)) {
            relativePath = relativePath.substring(2);
          }
          const fullRelativePath = vscode.workspace.rootPath + char + relativePath;
          i = fullFileName.indexOf(fullRelativePath) + fullRelativePath.length;
          Iif (!fullRelativePath.endsWith(char)) {
            i++;
          }
        } else {
          i = fullFileName.indexOf(vscode.workspace.rootPath!) + vscode.workspace.rootPath!.length + 1;
        }
        const ret = fullFileName.substring(i);
        return ret.replace(new RegExp(/\s/g),"%20");
      }
    }