UNPKG

five-server

Version:

Development Server with Live Reload Capability. (Maintained Fork of Live Server)

58 lines 2.15 kB
"use strict"; /** * @author Yannick Deubel (https://github.com/yandeu) * @copyright Copyright (c) 2021 Yannick Deubel * @license {@link https://github.com/yandeu/five-server/blob/main/LICENSE LICENSE} */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExecPHP = void 0; const public_1 = require("../public"); const path_1 = require("path"); const child_process_1 = require("child_process"); const fs_1 = __importDefault(require("fs")); class ExecPHP { get path() { return this.php; } get ini() { return this.phpIni; } set path(path) { this.php = path; } set ini(path) { this.phpIni = path; } async parseFile(absolutePath, res) { let msg = ''; return new Promise(resolve => { const returnWithError = (msg, template) => { return resolve(template.replace('{msg}', msg)); }; if (!this.php) { msg = 'Could not find PHP executable.'; res.status(500); } if (!msg && this.php && !fs_1.default.existsSync(this.php)) { msg = `Could not find executable: "${this.php}"`; res.status(500); } if (msg) returnWithError(msg, public_1.PHP_TEMPLATE); const cmd = `"${this.php}" "${absolutePath}"`; // https://nodejs.org/docs/latest-v18.x/api/child_process.html#child_processexeccommand-options-callback (0, child_process_1.exec)(cmd, { maxBuffer: 1024 * 1024 * 2, cwd: (0, path_1.dirname)(absolutePath) }, function (error, stdout, stderr) { if (error) returnWithError(`<p>error: ${error.message}</p>`, public_1.PHP_ERROR); if (stderr) returnWithError(`<p>stderr: ${stderr}</p>`, public_1.PHP_ERROR); resolve(stdout); }); }); } } exports.ExecPHP = ExecPHP; //# sourceMappingURL=execPHP.js.map