UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA Functions and Pages.

109 lines (108 loc) 4.17 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import path from 'path'; import spawn from 'cross-spawn'; import t from '../../../i18n/index.js'; import logger from '../../../libs/logger.js'; import { getRoot } from '../../../utils/fileUtils/base.js'; import { getDevConf } from '../../../utils/fileUtils/index.js'; class MockWorkerServer { constructor(props) { this.instance = null; this.restarting = false; this.command = props.command || 'deno'; } start() { if (this.instance) { return; } return new Promise((resolve) => { var _a, _b, _c; const root = getRoot(); const inspectPort = getDevConf('inspectPort', 'dev', 9229); // @ts-ignore const id = global.id || ''; let inspectOption = '--inspect'; if (inspectPort !== 9229) { inspectOption = `--inspect=127.0.0.1:${inspectPort}`; } this.instance = spawn(this.command, [ 'run', '--no-lock', '--allow-net', '--allow-read', '--allow-write', '--allow-env', inspectOption, path.join(root, `.dev/index-${id}.js`), id ], { stdio: ['pipe', 'pipe', 'pipe'] }); (_a = this.instance.stdout) === null || _a === void 0 ? void 0 : _a.setEncoding('utf8'); (_b = this.instance.stdout) === null || _b === void 0 ? void 0 : _b.on('data', this.stdoutHandler.bind(this)); (_c = this.instance.stderr) === null || _c === void 0 ? void 0 : _c.on('data', this.stderrHandler.bind(this)); this.instance.on('close', this.closeHandler.bind(this)); this.instance.on('error', this.errorHandler.bind(this)); setTimeout(() => { resolve(true); }, 500); }); } stdoutHandler(chunk) { logger.log(chunk.toString().trim()); } stderrHandler(chunk) { logger.subError(chunk.toString().trim()); } errorHandler(err) { var _a; logger.error(err.message ? err.message : err); (_a = this.instance) === null || _a === void 0 ? void 0 : _a.kill(); } closeHandler() { if (this.restarting) { this.restarting = false; return; } logger.log(t('dev_server_closed').d('Worker server closed')); logger.info('Worker server closed'); // @ts-ignore global.port = undefined; } runCommand(command) { var _a, _b; (_b = (_a = this.instance) === null || _a === void 0 ? void 0 : _a.stdin) === null || _b === void 0 ? void 0 : _b.write(command); } stop() { return new Promise((resolve) => { if (!this.instance) { resolve(false); return; } const onExit = () => { this.instance = null; resolve(true); }; this.instance.on('exit', onExit); this.instance.kill('SIGTERM'); }); } restart() { return __awaiter(this, void 0, void 0, function* () { this.restarting = true; yield this.stop(); this.start(); logger.log(t('dev_server_restart').d('Worker server restarted')); logger.info('Worker server restarted'); }); } } export default MockWorkerServer;