UNPKG

mihawk

Version:

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

93 lines (92 loc) 3.92 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPathInDir = exports.getRouteByJsonPath = exports.formatMockPath = exports.formatPath = exports.removeSpecialExt = exports.getRoutesFileExt = exports.getLogicFileExt = exports.getRootAbsPath = exports.isExistedSync = exports.unixifyPath = exports.absifyPath = exports.relPathToCWD = void 0; const path_1 = require("path"); const fs_extra_1 = require("fs-extra"); const consts_1 = require("../consts"); function relPathToCWD(targetPath) { return (0, path_1.relative)(consts_1.CWD, targetPath); } exports.relPathToCWD = relPathToCWD; function absifyPath(targetPath, rootPath = consts_1.CWD) { rootPath = rootPath || consts_1.CWD; targetPath = targetPath ? targetPath.trim() : rootPath; const absPath = (0, path_1.isAbsolute)(targetPath) ? targetPath : (0, path_1.resolve)(rootPath, targetPath); return absPath.replace(/[/\\]+$/, ''); } exports.absifyPath = absifyPath; function unixifyPath(targetPath) { return process.platform === 'win32' ? targetPath.replace(/\\+/g, '/') : targetPath; } exports.unixifyPath = unixifyPath; function isExistedSync(targetPath, rootPath = consts_1.CWD) { if (!targetPath) { return false; } targetPath = absifyPath(targetPath.trim(), rootPath); return (0, fs_extra_1.existsSync)(targetPath); } exports.isExistedSync = isExistedSync; let _pkgRootAbsPath = ''; function getRootAbsPath() { if (!_pkgRootAbsPath) { const curDirPath = (0, path_1.join)(__dirname, '../'); const curParentDirPath = (0, path_1.join)(curDirPath, '../'); const curParentDirName = (0, path_1.basename)(curParentDirPath); const isInDist = ['cjs', 'esm', 'types'].some(distSubDir => curParentDirName == distSubDir); const rootPath = isInDist ? (0, path_1.join)(curParentDirPath, '../../') : curParentDirPath; _pkgRootAbsPath = (0, path_1.resolve)(rootPath); } return _pkgRootAbsPath; } exports.getRootAbsPath = getRootAbsPath; function getLogicFileExt(fileType) { switch (fileType) { case 'js': case 'javascript': return 'js'; case 'cjs': return 'cjs'; case 'ts': case 'typescript': return 'ts'; case 'none': default: return ''; } } exports.getLogicFileExt = getLogicFileExt; function getRoutesFileExt(fileType) { return (getLogicFileExt(fileType) || 'json'); } exports.getRoutesFileExt = getRoutesFileExt; function removeSpecialExt(filePath) { return filePath.replace(/\.(js|cjs|ts|json|json5)$/g, ''); } exports.removeSpecialExt = removeSpecialExt; function formatPath(targetPath) { return unixifyPath((0, path_1.normalize)(targetPath)); } exports.formatPath = formatPath; function formatMockPath(mockPath) { let newPath = formatPath(removeSpecialExt(mockPath)); newPath = newPath.endsWith('/') ? `${newPath}index` : newPath; return newPath; } exports.formatMockPath = formatMockPath; function getRouteByJsonPath(jsonRelPath, jsonExt = 'json') { const ext = (jsonExt || 'json').replace(/^\.+/g, ''); const routePath = unixifyPath(jsonRelPath.replace(/^\/+/, '').replace(new RegExp(`\\.${ext}$`), '')); const [first, ...others] = routePath.split('/'); return { method: first.toUpperCase(), path: `/${others.join('/')}`, }; } exports.getRouteByJsonPath = getRouteByJsonPath; function isPathInDir(targetPath, dirPath) { targetPath = unixifyPath((0, path_1.resolve)(targetPath)); dirPath = unixifyPath((0, path_1.resolve)(dirPath)); return targetPath !== dirPath && (targetPath === null || targetPath === void 0 ? void 0 : targetPath.length) >= (dirPath === null || dirPath === void 0 ? void 0 : dirPath.length) && (targetPath === null || targetPath === void 0 ? void 0 : targetPath.startsWith(dirPath)); } exports.isPathInDir = isPathInDir;