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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdGVkLWFwcC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydW5uZXIvdGVzdGVkLWFwcC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUNBLCtCQUFrRDtBQUNsRCxpQ0FBZ0Q7QUFDaEQsbUNBQThCO0FBQzlCLDBEQUE2QjtBQUM3QiwwREFBMkI7QUFDM0IsMkRBQW1DO0FBQ25DLCtDQUFpRDtBQUNqRCwyQ0FBaUQ7QUFDakQsdUdBQTRFO0FBQzVFLGtEQUFnQztBQUVoQyxNQUFNLGVBQWUsR0FBRyxJQUFBLHFDQUF3QixFQUFDLHFCQUFxQixDQUFDLENBQUM7QUFFeEUsTUFBTSxZQUFZLEdBQUcsQ0FBQztJQUNsQixJQUFJLG1CQUFFLENBQUMsR0FBRyxFQUFFO1FBQ1IsSUFBSSxPQUFPLEdBQUcsTUFBTSxDQUFDO1FBRXJCLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxDQUFDLE9BQU8sQ0FBQyxHQU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