75 lines
10 KiB
JavaScript
75 lines
10 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const path_1 = require("path");
|
||
|
const execa_1 = require("execa");
|
||
|
const lodash_1 = require("lodash");
|
||
|
const tree_kill_1 = __importDefault(require("tree-kill"));
|
||
|
const os_family_1 = __importDefault(require("os-family"));
|
||
|
const delay_1 = __importDefault(require("../utils/delay"));
|
||
|
const runtime_1 = require("../errors/runtime");
|
||
|
const types_1 = require("../errors/types");
|
||
|
const resolve_path_relatively_cwd_1 = __importDefault(require("../utils/resolve-path-relatively-cwd"));
|
||
|
const debug_1 = __importDefault(require("debug"));
|
||
|
const MODULES_BIN_DIR = (0, resolve_path_relatively_cwd_1.default)('./node_modules/.bin');
|
||
|
const ENV_PATH_KEY = (function () {
|
||
|
if (os_family_1.default.win) {
|
||
|
let pathKey = 'Path';
|
||
|
Object.keys(process.env).forEach(key => {
|
||
|
if (key.toLowerCase() === 'path')
|
||
|
pathKey = key;
|
||
|
});
|
||
|
return pathKey;
|
||
|
}
|
||
|
return 'PATH';
|
||
|
})();
|
||
|
class TestedApp {
|
||
|
constructor() {
|
||
|
this._process = null;
|
||
|
this._killed = false;
|
||
|
this._stdoutLogger = (0, debug_1.default)('testcafe:tested-app:stdout');
|
||
|
this._stderrLogger = (0, debug_1.default)('testcafe:tested-app:stderr');
|
||
|
this.errorPromise = null;
|
||
|
}
|
||
|
async _run(command) {
|
||
|
const env = Object.assign({}, process.env);
|
||
|
const path = env[ENV_PATH_KEY] || '';
|
||
|
const pathParts = path.split(path_1.delimiter);
|
||
|
pathParts.unshift(MODULES_BIN_DIR);
|
||
|
env[ENV_PATH_KEY] = pathParts.join(path_1.delimiter);
|
||
|
this._process = (0, execa_1.command)(command, { shell: true, env });
|
||
|
if (this._process.stdout)
|
||
|
this._process.stdout.on('data', data => this._stdoutLogger(String(data)));
|
||
|
if (this._process.stderr)
|
||
|
this._process.stderr.on('data', data => this._stderrLogger(String(data)));
|
||
|
try {
|
||
|
await this._process;
|
||
|
}
|
||
|
catch (err) {
|
||
|
if (this._killed)
|
||
|
return;
|
||
|
const message = err.stack || String(err);
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.testedAppFailedWithError, message);
|
||
|
}
|
||
|
}
|
||
|
async start(command, initDelay) {
|
||
|
// NOTE: We should not resolve it if no error was thrown
|
||
|
this.errorPromise = this
|
||
|
._run(command)
|
||
|
.then(() => new Promise(lodash_1.noop));
|
||
|
await Promise.race([
|
||
|
(0, delay_1.default)(initDelay),
|
||
|
this.errorPromise,
|
||
|
]);
|
||
|
}
|
||
|
async kill() {
|
||
|
this._killed = true;
|
||
|
const killPromise = new Promise(resolve => (0, tree_kill_1.default)(this._process.pid, 'SIGTERM', resolve));
|
||
|
await killPromise;
|
||
|
}
|
||
|
}
|
||
|
exports.default = TestedApp;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdGVkLWFwcC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydW5uZXIvdGVzdGVkLWFwcC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUNBLCtCQUFrRDtBQUNsRCxpQ0FBZ0Q7QUFDaEQsbUNBQThCO0FBQzlCLDBEQUE2QjtBQUM3QiwwREFBMkI7QUFDM0IsMkRBQW1DO0FBQ25DLCtDQUFpRDtBQUNqRCwyQ0FBaUQ7QUFDakQsdUdBQTRFO0FBQzVFLGtEQUFnQztBQUVoQyxNQUFNLGVBQWUsR0FBRyxJQUFBLHFDQUF3QixFQUFDLHFCQUFxQixDQUFDLENBQUM7QUFFeEUsTUFBTSxZQUFZLEdBQUcsQ0FBQztJQUNsQixJQUFJLG1CQU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