UNPKG

mihawk

Version:

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

130 lines (129 loc) 5.69 kB
'use strict'; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createWatcher = exports.refreshModule = void 0; const color_cc_1 = __importDefault(require("color-cc")); const chokidar = __importStar(require("chokidar")); const print_1 = require("../../src/utils/print"); const path_1 = require("../../src/utils/path"); const loader_1 = require("../../src/composites/loader"); const consts_1 = require("../consts"); const WATCHER_IGNORES = [ '**/.*', '**/.DS_Store', '**/*.md', '**/*.txt', '**/*.{jpg,jpeg,png,gif,bmp,webp,svg}', '**/*.{mp4,mpeg,mpg,avi,mov,flv,wmv,rmvb,mkv}', '**/*.{wav,mp3,ogg,aac,flac,wma,m4a}', '**/*.{doc,docx,ppt,pptx,xls,xlsx,pdf}', '**/*.{pages,page,pagelet,pagelets,key,numbers}', '**/tsconfig.json', '**/tsconfig.*.json', '**/node_modules/**', '**/bower_components/**', '**/.idea/**', '**/.vscode/**', '**/.git/**', '**/.gitkeep', '**/.gitignore', '**/.svn/**', ]; const LOGFLAG_WATCHER = `${color_cc_1.default.cyan('[watcher]')}${color_cc_1.default.gray(':')}`; function refreshModule(filePath, allowLogicFileExt) { if (!filePath) return; filePath = (0, path_1.absifyPath)(filePath); const fileRelPath4log = color_cc_1.default.gray((0, path_1.unixifyPath)((0, path_1.relPathToCWD)(filePath))); try { if (allowLogicFileExt && filePath.endsWith(`.${allowLogicFileExt}`)) { (0, loader_1.refreshTsOrJs)(filePath); print_1.Printer.log(LOGFLAG_WATCHER, color_cc_1.default.success('Refresh script module!'), fileRelPath4log); } else if (filePath.endsWith('.json') || filePath.endsWith('.json5')) { (0, loader_1.refreshJson)(filePath); print_1.Printer.log(LOGFLAG_WATCHER, color_cc_1.default.success('Refresh json module!'), fileRelPath4log); } else { print_1.Printer.log(LOGFLAG_WATCHER, color_cc_1.default.gray('Skip refresh unnecessary module!'), fileRelPath4log); } } catch (error) { print_1.Printer.error(LOGFLAG_WATCHER, color_cc_1.default.fail('Refresh module failed!'), fileRelPath4log, '\n', error); } } exports.refreshModule = refreshModule; function createWatcher(config, callback) { const { mockDir, mockLogicFileType } = config; const watchTargetPath = (0, path_1.absifyPath)(mockDir); const logicFileExt = (0, path_1.getLogicFileExt)(mockLogicFileType); const watcher = chokidar.watch(watchTargetPath, { ignored: WATCHER_IGNORES, persistent: true, ignoreInitial: true, }); const cb = typeof callback === 'function' ? callback : () => { }; watcher.on('all', (eventName, filePath) => { if (eventName === 'rename') { return; } const fileRelPath4log = color_cc_1.default.gray((0, path_1.unixifyPath)((0, path_1.relPathToCWD)(filePath))); switch (eventName) { case 'change': { console.log(); print_1.Printer.log(LOGFLAG_WATCHER, 'File has been changed!', fileRelPath4log); refreshModule(filePath, logicFileExt); break; } case 'unlink': { console.log(); print_1.Printer.log(LOGFLAG_WATCHER, 'File has been deleted!', fileRelPath4log); refreshModule(filePath, logicFileExt); break; } case 'unlinkDir': case 'add': case 'addDir': default: break; } cb(eventName, filePath); }); watcher.on('rename', (oldFilePath, newFilePath) => { console.log(); const oldFilePath4Log = color_cc_1.default.gray((0, path_1.unixifyPath)((0, path_1.relPathToCWD)(oldFilePath))); const newFilePath4log = color_cc_1.default.gray((0, path_1.unixifyPath)((0, path_1.relPathToCWD)(newFilePath))); print_1.Printer.log(LOGFLAG_WATCHER, 'File has been rename!', `${oldFilePath4Log} ${consts_1.LOG_ARROW} ${newFilePath4log}`); refreshModule(oldFilePath, logicFileExt); refreshModule(newFilePath, logicFileExt); cb('rename', oldFilePath, newFilePath); }); process.nextTick(() => print_1.Printer.log(LOGFLAG_WATCHER, color_cc_1.default.success('Enable watcher, start watching mock files...'), color_cc_1.default.gray(`./${mockDir}/**/*.{json|json5${logicFileExt ? `|${logicFileExt}` : ''}}`))); return watcher; } exports.createWatcher = createWatcher;