warp-proxy
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -12,13 +12,15 @@ #!/usr/bin/env node | ||
.usage('Usage: $0 <command> [options]') | ||
.command('web', 'starts the server in web mode', args => { | ||
.command('web', 'runs in web mode', args => { | ||
args.option('port', { | ||
alias: ['p'], | ||
require: true, | ||
describe: 'port to be used by the local server', | ||
type: 'number', | ||
describe: 'Port to be used by the local server', | ||
}).option('target', { | ||
alias: ['t'], | ||
require: true, | ||
describe: 'address of the server to send your requests', | ||
type: 'string', | ||
describe: 'Address of the server to send your requests', | ||
}) | ||
.example('$0 web --port 3000 --target http://my.api.com'); | ||
.example('$0 web --port 3000 --target https://api.github.com'); | ||
}, argv => { | ||
@@ -28,18 +30,32 @@ startMessage(argv.port, argv.target); | ||
}) | ||
.command('mock', 'starts the server in mock mode', args => { | ||
.command('mock', 'runs in mock mode', args => { | ||
args.option('port', { | ||
alias: ['p'], | ||
require: true, | ||
describe: 'port to be used by the local server', | ||
type: 'number', | ||
describe: 'Port to be used by the local server', | ||
}).option('directory', { | ||
alias: ['d'], | ||
require: true, | ||
describe: 'directory to read the mock files', | ||
type: 'string', | ||
describe: 'Directory to read the mock files', | ||
}) | ||
.example('$0 mock --port 3000 --directory ./mocks'); | ||
.option('fileExtension', { | ||
alias: ['fe'], | ||
type: 'string', | ||
default: 'json', | ||
describe: 'File exntesion of the mock files', | ||
}) | ||
.option('keepExtensions', { | ||
alias: ['ke'], | ||
type: 'boolean', | ||
default: true, | ||
describe: 'If the request ends with a file extension, use it instead', | ||
}) | ||
.example('$0 mock --port 3000 --directory ./mocks --fileExtension json'); | ||
}, argv => { | ||
startMessage(argv.port, argv.directory); | ||
files(argv.port, argv.directory); | ||
files(argv.port, argv.directory, argv.fileFormat, argv.keepExtensions); | ||
}) | ||
.command('run', 'starts the server using a configuration [--config]', args => { | ||
.command('run', 'runs loading the configuration from a file [--config]', args => { | ||
args.option('config', { | ||
@@ -53,3 +69,5 @@ alias: ['c'], | ||
const configPath = path.resolve(process.cwd(), argv.config); | ||
const {mode, port, target, directory, ...config} = require(configPath); | ||
const { | ||
mode, port, target, directory, fileFormat, keepExtensions, ...config | ||
} = require(configPath); | ||
@@ -62,5 +80,6 @@ if (mode === 'web') { | ||
startMessage(port, directory); | ||
files(port, directory); | ||
files(port, directory, fileFormat, keepExtensions); | ||
} | ||
}) | ||
.help('help') | ||
.argv; |
@@ -9,17 +9,23 @@ const chalk = require('chalk'); | ||
const run = (port, directory) => { | ||
const run = (port, directory, fileExt, keepExtensions) => { | ||
const filesProxy = http.createServer(); | ||
const files = (req, res) => { | ||
console.log(directory, req.method, req.url); | ||
const filePath = path.join(directory, req.method, `${req.url}.json`); | ||
let status = paintStatus(res.statusCode); | ||
const method = chalk.bold(req.method); | ||
const urlFile = req.url.split('/').splice(-1).pop() || `index.${fileExt}`; | ||
const urlFileExt = urlFile.indexOf('.') > -1 ? urlFile.split('.').splice(-1).pop() : null; | ||
const urlFileName = urlFile.split(`.${urlFileExt}`)[0]; | ||
const url = req.url.replace(urlFile, ''); | ||
const method = req.method; | ||
const finalFileExtension = keepExtensions && urlFileExt ? urlFileExt : fileExt; | ||
const filePath = path.join(directory, method, url, `${urlFileName}.${finalFileExtension}`); | ||
fs.exists(filePath, exist => { | ||
if (!exist) { | ||
status = paintStatus(404); | ||
res.statusCode = 404; | ||
log(chalk.bgYellowBright.black.bold(`WARNING! THERE IS NO MOCK FILE FOR ${req.url}`)); | ||
} | ||
pump(fs.createReadStream(filePath), res, log(`${status} [${method}] ${filePath}`)); | ||
const status = paintStatus(res.statusCode); | ||
pump(fs.createReadStream(filePath), res, log(`${status}[${chalk.bold(method)}] ${filePath}`)); | ||
}); | ||
@@ -26,0 +32,0 @@ }; |
{ | ||
"name": "warp-proxy", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Proxy requests or return mocks from local files instead", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm test" | ||
"test": "eslint ." | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -16,2 +16,6 @@ <h1 align="center"> | ||
<p align="center"> | ||
<img src="docs/cli.png" alt="Cli" /> | ||
</p> | ||
## Description | ||
@@ -21,3 +25,3 @@ A simple command line to quickly start a web server which proxys your requests to an external server or to local files (JSON). | ||
Not made to be used in production. Use at your own risk. | ||
*Not made to be used in production. Use at your own risk.* | ||
@@ -29,3 +33,5 @@ ## Getting Started | ||
```bash | ||
npm install -g warp-proxy | ||
npm install warp-proxy # globally accessible | ||
npm install warp-proxy --save-dev # only accessible via npm package.json commands | ||
``` | ||
@@ -49,4 +55,3 @@ | ||
## Configuration | ||
You can pass a custom configuration via `--config` argument. | ||
The following properties are also availabe via cli arguments. | ||
Using the `run` command you can pass a custom configuration via `--config` argument. | ||
@@ -100,3 +105,3 @@ ```js | ||
βββββββββββββββββββββββββ ββββββββ βββββββββ | ||
root folder + /GET/ | FOLDER |JSON FILE | ||
root folder + /PUT/ | FOLDER |JSON FILE | ||
``` | ||
@@ -115,2 +120,2 @@ | ||
[MIT licensed](./LICENSE) | ||
[MIT licensed](./LICENSE) |
Sorry, the diff of this file is not supported yet
104393
15
158
116