Skip to content

Commit

Permalink
fix: Fix type errors, support command line multi-file listeners #144 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 27, 2020
1 parent b233b66 commit 2c4dd5c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/detect-port": "1.3.0",
"@types/express": "4.17.8",
"@types/http-proxy": "1.17.4",
"@types/minimist": "1.2.0",
"@types/node": "13.13.23",
"body-parser": "1.19.0",
"chokidar": "3.4.2",
Expand Down
17 changes: 7 additions & 10 deletions packages/core/src/bin/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ interface MockerConfig {

const argvs = minimist(process.argv.slice(2));
const paths = argvs['_'];
let mockPath = paths[0] || DEFAULTMOCKPATH;

let mockPath = paths || DEFAULTMOCKPATH;
let mockConfigPath = DEFAULTMOCKERCONFIGPATH;
let mockerConfig: MockerConfig = {
host: process.env.HOST || '0.0.0.0',
Expand All @@ -33,20 +34,17 @@ interface MockerConfig {
}

if (argvs.config) {
mockConfigPath = paths.config;
mockConfigPath = argvs.config;
}

if (!existsSync(path.resolve(mockConfigPath))) {
mockerConfig.host = process.env.HOST ? process.env.HOST : mockerConfig.host;
mockerConfig.port = await detect(mockerConfig.port);
mockerConfig.host = process.env.HOST ? process.env.HOST : mockerConfig.host;
mockerConfig.port = await detect(mockerConfig.port);
} else {
mockerConfig = require(path.resolve(mockConfigPath));
mockerConfig = require(path.resolve(mockConfigPath));
}

const mockDir = require.resolve(path.resolve(mockPath));

const DEFAULT_PORT = mockerConfig.port;

const app = express();

app.all('/*', (req, res, next) => {
Expand All @@ -56,8 +54,7 @@ interface MockerConfig {
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
next();
});

apiMocker(app, mockDir);
apiMocker(app, mockPath);

app.listen(DEFAULT_PORT, () => {
const localIpUrl = prepareUrls({
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function pathMatch(options: TokensToRegexpOptions & ParseOptions) {
}

export default function (app: Application, watchFile: string | string[] | MockerProxyRoute, conf: MockerOption = {}) {
const watchFiles = Array.isArray(watchFile) ? watchFile : typeof watchFile === 'string' ? [watchFile] : [];
const watchFiles = (Array.isArray(watchFile) ? watchFile : typeof watchFile === 'string' ? [watchFile] : []).map(str => PATH.resolve(str));

if (watchFiles.some(file => !file)) {
throw new Error('Mocker file does not exist!.');
Expand Down Expand Up @@ -245,6 +245,7 @@ export default function (app: Application, watchFile: string | string[] | Mocker
// header = {}

if (isWatchFilePath) {
console.log('isWatchFilePath:', isWatchFilePath)
// 监听配置入口文件所在的目录,一般为认为在配置文件/mock 目录下的所有文件
// 加上require.resolve,保证 `./mock/`能够找到`./mock/index.js`,要不然就要监控到上一级目录了
const watcher = chokidar.watch(watchFiles.map(watchFile => PATH.dirname(require.resolve(watchFile))), options.watchOptions);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,11 @@
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/minimist@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=

"@types/node@*":
version "14.11.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.11.1.tgz#56af902ad157e763f9ba63d671c39cda3193c835"
Expand Down

0 comments on commit 2c4dd5c

Please sign in to comment.