UNPKG

ragemp-auto-reload-webpack-plugin

Version:
44 lines (43 loc) 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path_1 = require("path"); const child_process_1 = require("child_process"); const utils_1 = require("./utils"); class RagempAutoReloadPlugin { constructor(config) { this.validateConfig = () => { if (!this.config) { throw Error('Failed to read the config. Config is not provided'); } const { serverPath } = this.config; if (!utils_1.isString(serverPath)) { throw TypeError('Failed to read the config. "serverPath" must be a string'); } if (!fs.existsSync(serverPath)) { throw Error(`The server file not found. Path: ${serverPath}`); } }; this.determineServerDirectory = () => { this.serverDirectory = path_1.dirname(this.config.serverPath); }; this.determineServerProcessName = () => { this.serverProcessName = path_1.basename(this.config.serverPath); }; this.apply = (compiler) => { compiler.hooks.done.tap('RagempAutoReloadPlugin', () => { const { serverDirectory, serverProcessName } = this; try { child_process_1.execSync(`taskkill /T /F /IM ${this.serverProcessName}`, { stdio: 'ignore' }); } catch (e) { } child_process_1.exec(`cd ${serverDirectory} && start /MIN ${serverProcessName}`); }); }; this.config = config; this.validateConfig(); this.determineServerDirectory(); this.determineServerProcessName(); } } exports.RagempAutoReloadPlugin = RagempAutoReloadPlugin;