mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
100 lines (99 loc) • 3.99 kB
JavaScript
;
import Colors from 'color-cc';
import * as chokidar from 'chokidar';
import { Printer } from '../../src/utils/print';
import { absifyPath, getLogicFileExt, relPathToCWD, unixifyPath } from '../../src/utils/path';
import { refreshJson, refreshTsOrJs } from '../../src/composites/loader';
import { LOG_ARROW } from '../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 = `${Colors.cyan('[watcher]')}${Colors.gray(':')}`;
export function refreshModule(filePath, allowLogicFileExt) {
if (!filePath)
return;
filePath = absifyPath(filePath);
const fileRelPath4log = Colors.gray(unixifyPath(relPathToCWD(filePath)));
try {
if (allowLogicFileExt && filePath.endsWith(`.${allowLogicFileExt}`)) {
refreshTsOrJs(filePath);
Printer.log(LOGFLAG_WATCHER, Colors.success('Refresh script module!'), fileRelPath4log);
}
else if (filePath.endsWith('.json') || filePath.endsWith('.json5')) {
refreshJson(filePath);
Printer.log(LOGFLAG_WATCHER, Colors.success('Refresh json module!'), fileRelPath4log);
}
else {
Printer.log(LOGFLAG_WATCHER, Colors.gray('Skip refresh unnecessary module!'), fileRelPath4log);
}
}
catch (error) {
Printer.error(LOGFLAG_WATCHER, Colors.fail('Refresh module failed!'), fileRelPath4log, '\n', error);
}
}
export function createWatcher(config, callback) {
const { mockDir, mockLogicFileType } = config;
const watchTargetPath = absifyPath(mockDir);
const logicFileExt = 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 = Colors.gray(unixifyPath(relPathToCWD(filePath)));
switch (eventName) {
case 'change': {
console.log();
Printer.log(LOGFLAG_WATCHER, 'File has been changed!', fileRelPath4log);
refreshModule(filePath, logicFileExt);
break;
}
case 'unlink': {
console.log();
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 = Colors.gray(unixifyPath(relPathToCWD(oldFilePath)));
const newFilePath4log = Colors.gray(unixifyPath(relPathToCWD(newFilePath)));
Printer.log(LOGFLAG_WATCHER, 'File has been rename!', `${oldFilePath4Log} ${LOG_ARROW} ${newFilePath4log}`);
refreshModule(oldFilePath, logicFileExt);
refreshModule(newFilePath, logicFileExt);
cb('rename', oldFilePath, newFilePath);
});
process.nextTick(() => Printer.log(LOGFLAG_WATCHER, Colors.success('Enable watcher, start watching mock files...'), Colors.gray(`./${mockDir}/**/*.{json|json5${logicFileExt ? `|${logicFileExt}` : ''}}`)));
return watcher;
}