Code coverage report for CommandController.ts

# All files CommandController.ts

92.73% Statements 51/55
90.32% Branches 28/31
90.91% Functions 10/11
92.73% Lines 51/55

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
    1341x

    1x
    1x
    1x
    1x
    1x

    1x

    12x

    12x
    12x

    12x

    12x

    12x
    1x
    1x
    1x

    12x
    10x
    1x

    9x
    9x

    9x
    8x

    12x
    4x
    3x

    4x

    4x
    3x

    12x
    3x
    1x

    2x

    12x
    2x
    1x

    13x
    12x

    13x

    13x

    13x

    13x

    13x

    13x
    13x

    13x

    13x
    13x

    13x

    13x

    13x

     import PHPServer, { PHPServerConfig } from '../server/PHPServer';
    import { ExtensionConfiguration } from '../extension';
    import { platform } from 'os';
    import { relative } from 'path';
    import { AnyBrowser } from '../browser/Browser';
    import Messages from '../Messages';
    import VSCodeLogger, { Logger } from '../VSCodeLogger';

    interface CommandControllerContext {
      extension: {
        path: string;
        getConfiguration(): ExtensionConfiguration;
      };
      notify(message: string): void;
      getRootPath(): string | undefined;
      getAbsolutePathToActiveFile(): string | undefined;
    }

    export default class CommandController {
      private server: PHPServer;
      private logger: Logger = new VSCodeLogger('PHP Server');

      constructor(private context: CommandControllerContext) {
        this.server = new PHPServer();

        this.server.on('data', this.logger.appendLine);

        this.server.on('close', () => {
          this.context.notify(Messages.SERVER_STOPPED);
        });

        this.server.on('error', (errorMessage) => {
          this.logger.show();
          this.logger.appendLine(errorMessage);
          this.server.stop();
        });
      }

      serveProject = (): void => {
        if (this.server.isRunning()) {
          throw Error(Messages.SERVER_IS_ALREADY_RUNNING);
        }

        this.logger.clear();
        this.logger.show();

        this.startServer();
        this.openBrowserIfPossible();
      };

      reloadServer = (): void => {
        if (this.server.isRunning()) {
          this.server.stop();
        }

        this.startServer();

        if (this.context.extension.getConfiguration().autoOpenOnReload) {
          this.openBrowserIfPossible();
        }
      };

      openFileInBrowser = (): void => {
        if (!this.server.isRunning()) {
          throw Error(Messages.SERVER_IS_NOT_RUNNING);
        }

        this.openBrowserIfPossible();
      };

      stopServer = (): void => {
        if (this.server.isRunning()) {
          this.server.stop();
        }
      };

      private startServer() {
        this.server.start(this.getServerConfiguration());
        this.context.notify(Messages.SERVING_PROJECT);
      }

      private getServerConfiguration(): PHPServerConfig {
        const extensionConfig = this.context.extension.getConfiguration();

        const relativePath = extensionConfig.relativePath || '.';

        return {
          host: extensionConfig.ip || 'localhost',
          port: extensionConfig.port || 3000,
          rootPath: this.context.getRootPath()!, // TODO handle optionality
          relativePath,
          phpExecutablePath: extensionConfig.phpPath || 'php',
          phpRouterPath: this.getPhpServerRouterPath(
            relativePath,
            extensionConfig.router
          ),
          phpConfigPath: extensionConfig.phpConfigPath,
        };
      }

      private getPhpServerRouterPath(
        relativePath: string,
        routerPathFromConfig?: string
      ) {
        Iif (!routerPathFromConfig && platform() === 'win32') {
          process.env.PHP_SERVER_RELATIVE_PATH = relativePath;
          return `${this.context.extension.path}\\src\\server\\logger.php`;
        }

        return routerPathFromConfig;
      }

      private openBrowserIfPossible() {
        const { host, port } = this.server.getCurrentConfig() || {};
        const rootPath = this.context.getRootPath();

        Iif (!rootPath || !host || !port) {
          return;
        }

        const { browser } = this.context.extension.getConfiguration();
        const absolutePathToActiveFile = this.context.getAbsolutePathToActiveFile();

        const relativePathToActiveFile = relative(
          rootPath,
          absolutePathToActiveFile || rootPath
        ).replace(/\\/g, '/');

        const url = `http://${host}:${port}/${relativePathToActiveFile}`;

        new AnyBrowser(browser).open(url);
      }
    }