Skip to content

Commit

Permalink
feat: support mocker config in the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpipy authored and jaywcjlove committed Oct 27, 2020
1 parent ac598b0 commit b233b66
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"express": "4.17.1",
"http-proxy": "1.18.1",
"local-ip-url": "1.0.3",
"minimist": "^1.2.5",
"path-to-regexp": "6.2.0"
}
}
50 changes: 36 additions & 14 deletions packages/core/src/bin/mocker.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
#!/usr/bin/env node
import path from 'path';
import { existsSync } from 'fs';
import prepareUrls from 'local-ip-url/prepareUrls';
import detect from 'detect-port';
import color from 'colors-cli/safe';
import express from 'express';
import minimist from 'minimist';
import apiMocker from '../';

interface MockerConfig {
host: string;
port: number;
}

(async () => {
if (!process.argv.slice(2).length) {
const DEFAULTMOCKERCONFIGPATH = './mocker.config.json';
const DEFAULTMOCKPATH = './mock';

const argvs = minimist(process.argv.slice(2));
const paths = argvs['_'];
let mockPath = paths[0] || DEFAULTMOCKPATH;
let mockConfigPath = DEFAULTMOCKERCONFIGPATH;
let mockerConfig: MockerConfig = {
host: process.env.HOST || '0.0.0.0',
port: Number(process.env.PORT) || 3721
};

if (paths.length === 0) {
console.log(color.red('Error: Need to pass parameters!'));
console.log(`E.g: ${color.yellow('mocker <File path>')}\n`);
return;
}
let mockpath = process.argv[2];

mockpath = require.resolve(path.resolve(mockpath));

const HOST = process.env.HOST || '0.0.0.0';
let DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3721;
const PORT = await detect(DEFAULT_PORT);
if (argvs.config) {
mockConfigPath = paths.config;
}

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

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

const DEFAULT_PORT = mockerConfig.port;

const app = express();

app.all('/*', (req, res, next) => {
Expand All @@ -34,13 +57,12 @@ import apiMocker from '../';
next();
});

apiMocker(app, mockpath);
apiMocker(app, mockDir);

app.listen(DEFAULT_PORT, () => {
const localIpUrl = prepareUrls({
protocol: 'http',
host: HOST,
port: DEFAULT_PORT,
...mockerConfig
});
console.log(`> Server Listening at Local: ${color.green(localIpUrl.localUrl)}`);
console.log(`> On Your Network: ${color.green(localIpUrl.lanUrl)}\n`);
Expand All @@ -52,7 +74,7 @@ import apiMocker from '../';
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof PORT === 'string' ? 'Pipe ' + PORT : 'Port ' + PORT;
const bind = typeof DEFAULT_PORT === 'string' ? `Pipe ${DEFAULT_PORT}` : `Port ${DEFAULT_PORT}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
Expand Down

0 comments on commit b233b66

Please sign in to comment.