@medux/dev-utils
Advanced tools
| import { NextFunction, Request, Response } from 'express'; | ||
| declare const _default: (mockDir: string) => (req: Request, res: Response, next: NextFunction) => any; | ||
| declare const _default: (mockFile: string) => (req: Request, res: Response, next: NextFunction) => any; | ||
| export = _default; |
@@ -5,17 +5,17 @@ "use strict"; | ||
| const chalk = require("chalk"); | ||
| module.exports = function createMiddleware(mockDir) { | ||
| const indexFile = path.join(mockDir, 'index.js'); | ||
| console.info(`enable ${chalk.magenta('api mock')} file: ${chalk.underline(indexFile)}`); | ||
| module.exports = function createMiddleware(mockFile) { | ||
| console.info(`enable ${chalk.magenta('api mock')} file: ${chalk.underline(mockFile)}`); | ||
| const mockDir = path.dirname(mockFile); | ||
| if (!fs.existsSync(mockDir)) { | ||
| fs.mkdirSync(mockDir); | ||
| } | ||
| if (!fs.existsSync(indexFile)) { | ||
| fs.writeFileSync(indexFile, 'module.exports = {}'); | ||
| if (!fs.existsSync(mockFile)) { | ||
| fs.writeFileSync(mockFile, 'module.exports = {}'); | ||
| } | ||
| return (req, res, next) => { | ||
| const str = fs | ||
| .readFileSync(indexFile) | ||
| .readFileSync(mockFile) | ||
| .toString() | ||
| .replace(/^[^{]+/, 'return ') | ||
| .replace(/\brequire\(/g, '('); | ||
| .replace(/\b(require|import)\(/g, '('); | ||
| const indexMap = new Function(str)(); | ||
@@ -27,3 +27,13 @@ let mockPath; | ||
| const extname = path.extname(indexMap[rule]); | ||
| mockPath = extname ? path.join(mockDir, indexMap[rule]) : path.join(mockDir, `${indexMap[rule]}.js`); | ||
| if (extname) { | ||
| if (fs.existsSync(path.join(mockDir, indexMap[rule]))) { | ||
| mockPath = path.join(mockDir, indexMap[rule]); | ||
| } | ||
| } | ||
| else if (fs.existsSync(path.join(mockDir, `${indexMap[rule]}.js`))) { | ||
| mockPath = path.join(mockDir, `${indexMap[rule]}.js`); | ||
| } | ||
| else if (fs.existsSync(path.join(mockDir, `${indexMap[rule]}.ts`))) { | ||
| mockPath = path.join(mockDir, `${indexMap[rule]}.ts`); | ||
| } | ||
| return true; | ||
@@ -33,3 +43,3 @@ } | ||
| }); | ||
| if (mockPath && fs.existsSync(mockPath)) { | ||
| if (mockPath) { | ||
| delete require.cache[mockPath]; | ||
@@ -36,0 +46,0 @@ const middleware = require(mockPath); |
@@ -9,6 +9,13 @@ "use strict"; | ||
| function patch(_tsconfig, _entryFilePath, _echo) { | ||
| const RootPath = process.cwd(); | ||
| const rootPath = process.cwd(); | ||
| const srcPath = path.join(rootPath, 'src'); | ||
| let tsconfig; | ||
| if (!_tsconfig) { | ||
| tsconfig = require(path.join(RootPath, './tsconfig.json')); | ||
| if (fs.existsSync(path.join(srcPath, './tsconfig.json'))) { | ||
| tsconfig = require(path.join(srcPath, './tsconfig.json')); | ||
| process.chdir('./src'); | ||
| } | ||
| else { | ||
| tsconfig = require(path.join(rootPath, './tsconfig.json')); | ||
| } | ||
| } | ||
@@ -21,3 +28,2 @@ else if (typeof _tsconfig === 'string') { | ||
| } | ||
| const srcPath = path.join(RootPath, 'src'); | ||
| const entryFilePath = _entryFilePath || (fs.existsSync(path.join(srcPath, 'Global.ts')) ? path.join(srcPath, 'Global.ts') : path.join(srcPath, 'Global.tsx')); | ||
@@ -24,0 +30,0 @@ const source = fs.readFileSync(entryFilePath).toString(); |
+1
-1
| { | ||
| "name": "@medux/dev-utils", | ||
| "version": "3.0.4", | ||
| "version": "3.0.5", | ||
| "description": "Common development tools.", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/wooline/medux/tree/master/packages/dev-utils", |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6535
10.91%111
16.84%7
16.67%