UNPKG

mihawk

Version:

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

35 lines (34 loc) 1.4 kB
'use strict'; import { join, relative } from 'path'; import { existsSync, readdirSync } from 'fs-extra'; import { absifyPath, getRouteByJsonPath } from '../utils/path'; import { Printer } from '../utils/print'; export function findJsonFiles(targetDirPath, jsonExt = 'json') { const filePathList = []; targetDirPath = absifyPath(targetDirPath); if (existsSync(targetDirPath)) { const fileExt = `.${(jsonExt || 'json').replace(/^\.+/g, '')}`; function _scanDir(dirPath, baseDirPath) { const subItems = readdirSync(dirPath, { withFileTypes: true }); for (const item of subItems) { const subPath = join(dirPath, item.name); if (item.isFile() && item.name.endsWith(fileExt)) { filePathList.push(relative(baseDirPath, subPath)); } else if (item.isDirectory()) { _scanDir(subPath, baseDirPath); } } } _scanDir(targetDirPath, targetDirPath); } else { Printer.warn(`mock data dir ${targetDirPath} not existed!`); } return filePathList; } export function scanExistedRoutes(dataDirPath, jsonExt = 'json') { const ext = jsonExt || 'json'; const existedJsonPaths = findJsonFiles(dataDirPath, ext); return existedJsonPaths.map(jsonPath => getRouteByJsonPath(jsonPath, ext)); }