UNPKG

mihawk

Version:

A tiny & simple mock server tool, support json,js,cjs,ts(typescript).

100 lines (99 loc) 5.22 kB
'use strict'; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const color_cc_1 = __importDefault(require("color-cc")); const deepmerge_1 = __importDefault(require("deepmerge")); const print_1 = require("../../src/utils/print"); const rc_1 = require("../../src/composites/rc"); const consts_1 = require("../../src/consts"); const index_1 = __importDefault(require("../../src/index")); const watcher_1 = require("../../src/composites/watcher"); const process_1 = require("../../src/utils/process"); const async_1 = require("../../src/utils/async"); const path_1 = require("../../src/utils/path"); const callback = function start(args) { return __awaiter(this, void 0, void 0, function* () { print_1.Debugger.log('CliConfig:', args); const rootConfig = yield (0, rc_1.getRcData)(`.${consts_1.PKG_NAME}rc`, { initConfig: Object.assign({}, consts_1.DEFAULT_RC) }); print_1.Debugger.log('RootConfig:', rootConfig); const finalConfig = (0, deepmerge_1.default)(rootConfig || {}, args); print_1.Debugger.log('FinalConfig:', finalConfig); const controller = { watcher: null, serverHandle: null }; yield _start(controller, finalConfig); if (finalConfig.watch) { setTimeout(() => { const reload = (0, async_1.throttleAsync)(_restart, 500); controller.watcher = (0, watcher_1.createWatcher)(finalConfig, (eventName, ...args) => { if (!['addDir', 'unlinkDir'].includes(eventName)) { const [filePath, newFilePath] = args || []; const isLogicFile = _isLogicFile(filePath); const isRoutesFile = filePath.endsWith(`routes.${(0, path_1.getRoutesFileExt)(finalConfig.mockLogicFileType)}`); const isNewLigicFile = newFilePath && _isLogicFile(newFilePath); const needReload = isLogicFile || isRoutesFile || isNewLigicFile; needReload && reload(controller, finalConfig); } }); }, 0); } }); }; function _isLogicFile(filePath) { return filePath && ['.js', '.cjs', '.ts'].some(ext => filePath.endsWith(ext)); } function _start(ctrl, config, isRestart) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { try { ctrl.serverHandle = yield (0, index_1.default)(config, isRestart); } catch (error) { print_1.Printer.error('Occurs error during runing async-function mihawk(config):\n', error); print_1.Printer.warn('Please check your config or try to run it again.'); if (typeof ((_a = ctrl.watcher) === null || _a === void 0 ? void 0 : _a.close) === 'function') { print_1.Printer.log('Will close file watcher...'); try { yield ctrl.watcher.close(); } catch (error) { print_1.Printer.warn('Error occurs during close watcher:', error); } } if (typeof ((_b = ctrl.serverHandle) === null || _b === void 0 ? void 0 : _b.destory) === 'function') { print_1.Printer.log('Will destory server...'); try { yield ((_c = ctrl.serverHandle) === null || _c === void 0 ? void 0 : _c.destory()); } catch (error) { print_1.Printer.warn('Error occurs during destory server:', error); } } print_1.Printer.warn(color_cc_1.default.yellow('Will exit process...')); (0, process_1.processExit)(1); } }); } function _restart(ctrl, config) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { if (typeof ((_a = ctrl.serverHandle) === null || _a === void 0 ? void 0 : _a.close) === 'function') { console.log(); print_1.Printer.log(`🔄 ${color_cc_1.default.magenta('Will restart the Mock-Tool...')}`); yield ((_b = ctrl.serverHandle) === null || _b === void 0 ? void 0 : _b.close()); yield (0, async_1.sleep)(0); print_1.Printer.log(color_cc_1.default.gray('🔜 The Mock-Tool is ready to lanuch...')); yield _start(ctrl, config, true); } }); } exports.default = callback;