Code coverage report for out/src/Server.js

# All files / out/src Server.js

61.29% Statements 57/93
42.86% Branches 24/56
64.29% Functions 9/14
61.29% Lines 57/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
    1x
    1x
    1x
    1x

    6x
    6x
    6x
    6x
    6x
    6x

    6x

    6x

    6x

    6x

    6x

    6x
    6x
    6x

    6x

    6x

    6x

    6x

    6x
    6x

    6x
    6x
    6x
    6x
    2x
    2x

    6x
    2x

    4x

    6x

    6x
    6x
    6x
    6x

    4x
    4x

    4x

    6x
    6x

    6x
    5x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x
     "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const vscode = require("vscode");
    const child_process_1 = require("child_process");
    const os_1 = require("os");
    class Server {
        constructor(ip, port, relativePath, extensionPath) {
            this.running = false;
            this.outputChanneL = vscode.window.createOutputChannel("PHP Server");
            this.ip = ip;
            this.port = port;
            this.relativePath = relativePath;
            this.extensionPath = extensionPath;
        }
        shutDown() {
            if (this.running && this.terminal !== undefined) {
                this.terminal.kill();
                this.terminal = undefined;
                this.running = false;
            }
        }
        isRunning() {
            return this.running && this.terminal !== undefined;
        }
        setPhpPath(phpPath) {
            this.phpPath = phpPath;
        }
        setPhpConfigPath(phpConfigPath) {
            this.phpConfigPath = phpConfigPath;
        }
        setRouter(router) {
            this.router = router;
        }
        run() {
            var _a, _b;
            Iif (this.isRunning()) {
                vscode.window.showErrorMessage("Server is already running!");
                return;
            }
            this.outputChanneL.clear();
            this.outputChanneL.show();
            this.terminal = child_process_1.spawn(this.phpPath ? this.phpPath : "php", this.argsToArray(), {
                cwd: vscode.workspace.rootPath
            });
            (_a = this.terminal.stdout) === null || _a === void 0 ? void 0 : _a.on("data", (data) => {
                this.outputChanneL.appendLine(data.toString());
            });
            (_b = this.terminal.stderr) === null || _b === void 0 ? void 0 : _b.on("data", (data) => {
                this.outputChanneL.appendLine(data.toString());
            });
            this.terminal.on("error", (err) => {
                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");
        }
        argsToArray() {
            const args = ["-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 (os_1.platform() === "win32") {
                args.push(`${this.extensionPath}\\src\\logger.php`);
                process.env.PHP_SERVER_RELATIVE_PATH = this.relativePath;
            }
            return args;
        }
        execBrowser(browser, fullFileName) {
            const ip = this.ip;
            const port = this.port;
            const fileName = this.fullToRelativeFileName(fullFileName);
            switch (browser.toLowerCase()) {
                case "firefox":
                    Eif (os_1.platform() === 'linux') {
                        browser = `${browser} http://${ip}:${port}/${fileName}`;
                    }
                    else if (os_1.platform() === "win32") {
                        browser = `start ${browser} http://${ip}:${port}/${fileName}`;
                    }
                    else if (os_1.platform() === 'darwin') {
                        browser = `open -a "Firefox" http://${ip}:${port}/${fileName}`;
                    }
                    else {
                        browser = '';
                    }
                    break;
                case "chrome":
                    if (os_1.platform() === 'linux') {
                        browser = `google-chrome http://${ip}:${port}/${fileName}`;
                    }
                    else if (os_1.platform() === "win32") {
                        browser = `start ${browser} http://${ip}:${port}/${fileName}`;
                    }
                    else if (os_1.platform() === 'darwin') {
                        browser = `open -a "Google Chrome" http://${ip}:${port}/${fileName}`;
                    }
                    else {
                        browser = '';
                    }
                    break;
                case "edge":
                    if (os_1.platform() === 'win32') {
                        browser = `start microsoft-edge:http://${ip}:${port}/${fileName}`;
                    }
                    else {
                        browser = '';
                    }
                    break;
                case "safari":
                    if (os_1.platform() === 'darwin') {
                        browser = `open -a "Safari" http://${ip}:${port}/${fileName}`;
                    }
                    else {
                        browser = '';
                    }
            }
            Eif (browser !== "") {
                child_process_1.exec(browser);
            }
        }
        fullToRelativeFileName(fullFileName) {
            if (!fullFileName) {
                return '';
            }
            let i = 0;
            Eif (this.relativePath.length > 1) {
                let char = os_1.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");
        }
    }
    exports.default = Server;
    //# sourceMappingURL=Server.js.map