Skip to content

Commit

Permalink
feat: 自动生成配置模板 Closes #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbin committed Nov 19, 2018
1 parent 3314db7 commit 65968cd
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 207 deletions.
103 changes: 12 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,18 @@ English | [简体中文](./docs/README-zh_CN.md)

## Getting Started

### Installing
### Installation

```bash
npm install @tiandatong/anymock -g
```

### Usage

create a directory contains *.config.json and mocks.json
(recommend api.config.json and file.config.json)

#### api.config.json

```json
{
"disabled": 0,
"mocks": [{
"URL": "//api.github.com/repos/temberature/anymock/branches",
"OPTIONS": ["normal", "protected"],
"enabled": ["normal"]
}, {
"URL": "https://suggest.taobao.com/sug",
"enabled": 1
}]
}
```

#### file.config.json

```json
{
"disabled": 0,
"mocks": [{
"url": "https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js",
"enabled": "./mock/jquery.js"
}]
}
```

#### mocks.json

```json
[{
"url": "//api.github.com/repos/temberature/anymock/branches",
"normal": {
"request": {
"queries": {
"protected": "0"
}
},
"body": [{
"name": "master1",
"commit": {
"sha": "51a0a39acfb1d029345e896cca6a6a2c3625816b",
"url": "https://api.github.com/repos/temberature/anymock/commits/51a0a39acfb1d029345e896cca6a6a2c3625816b"
}
}]
},
"protected": {
"request": {
"queries": {
"protected": "1"
}
},
"body": {
"message": "Not Found2",
"documentation_url": "https://developer.github.com/v3/repos/branches/#list-branches"
}
}
}, {
"url": "https://suggest.taobao.com/sug",
"default": {
"Content-Type": "text/html",
"fileHead": "",
"fileFooter": "",
"body": {
"result": [
[
"apple watch4",
"14770"
],
[
"apple pencil",
"12500"
]
],
"shop": "apple",
"tmall": "apple"
}
}
}]
```

run command below under the root dir
run command below anywhere

```bash
λ anymock
λ anymock init
[AnyProxy Log][2018-11-15 12:35:52]: throttle :10000kb/s
[AnyProxy Log][2018-11-15 12:35:52]: clearing cache file...
[AnyProxy Log][2018-11-15 12:35:52]: ==>>> clearing cache
Expand All @@ -124,10 +39,16 @@ run command below under the root dir
[AnyProxy Log][2018-11-15 12:35:52]: Active rule is: a rule to hack response
```

configure chrome's proxy to http://127.0.0.1:8001 via [Proxy SwitchyOmega](https://chrome.google.com/webstore/detail/proxy-switchyomega/padekgcemlokbadohgkifijomclgjgif)
* first run with 'init' will create a directory 'mock' contains *.config.json and mocks.json template
(default api.config.json and file.config.json)

* configure chrome's proxy to http://127.0.0.1:8001 via [Proxy SwitchyOmega](https://chrome.google.com/webstore/detail/proxy-switchyomega/padekgcemlokbadohgkifijomclgjgif)

then you can browse https://api.github.com/repos/temberature/anymock/branches?protected=0 or https://suggest.taobao.com/sug?code=utf-8&callback=KISSY.Suggest.callback&q=apple or https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
and see the corresponding mock result
* then you can browse
* https://api.github.com/repos/temberature/anymock/branches?protected=0
* https://suggest.taobao.com/sug?code=utf-8&callback=KISSY.Suggest.callback&q=apple
* https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
* and see the corresponding mock result

```json
[{
Expand Down
139 changes: 84 additions & 55 deletions docs/README-zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,116 @@
# Anymock
111
One Paragraph of project description goes here
<h1 align="center">Anymock</h1>

## Getting Started
<div align="center">

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
基于[Anyproxy](https://www.npmjs.com/package/anyproxy) 的mock 服务.

### Prerequisites
[![Build Status](https://travis-ci.org/temberature/anymock.svg?branch=master)](https://travis-ci.org/temberature/anymock)
[![Codecov](https://img.shields.io/codecov/c/github/temberature/anymock/master.svg?style=flat-square)](https://codecov.io/gh/temberature/anymock/branch/master)
[![Dependencies](https://img.shields.io/david/temberature/anymock.svg)](https://david-dm.org/temberature/anymock)
[![DevDependencies](https://img.shields.io/david/dev/temberature/anymock.svg)](https://david-dm.org/temberature/anymock?type=dev)

What things you need to install the software and how to install them
[![npm package](https://img.shields.io/npm/v/@tiandatong/anymock.svg?style=flat-square)](https://www.npmjs.org/package/@tiandatong/anymock)
[![NPM downloads](http://img.shields.io/npm/dm/@tiandatong/anymock.svg?style=flat-square)](http://npmjs.com/@tiandatong/anymock)
[![Percentage of issues still open](http://isitmaintained.com/badge/open/temberature/anymock.svg)](http://isitmaintained.com/project/temberature/anymock "Percentage of issues still open")
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
</div>

```
Give examples
```

### Installing
[English](./README.md) | 简体中文

A step by step series of examples that tell you how to get a development env running
## 起步

Say what the step will be
### 安装

```
Give the example
```bash
npm install @tiandatong/anymock -g
```

And repeat
### 用法

```bash
λ anymock init
[AnyProxy Log][2018-11-15 12:35:52]: throttle :10000kb/s
[AnyProxy Log][2018-11-15 12:35:52]: clearing cache file...
[AnyProxy Log][2018-11-15 12:35:52]: ==>>> clearing cache
[AnyProxy Log][2018-11-15 12:35:52]: closing webserver...
[AnyProxy Log][2018-11-15 12:35:52]: Http proxy started on port 8001
[AnyProxy Log][2018-11-15 12:35:52]: Active rule is: a rule to hack response
```
until finished
```

End with an example of getting some data out of the system or using it for a little demo

## Running the tests

Explain how to run the automated tests for this system

### Break down into end to end tests

Explain what these tests test and why

```
Give an example
* 自动创建一个名为mock 的目录, 其中包含*.config.json 和 mocks.json 配置模板
(默认 api.config.json 和 file.config.json)

* 通过[Proxy SwitchyOmega](https://chrome.google.com/webstore/detail/proxy-switchyomega/padekgcemlokbadohgkifijomclgjgif)把Chrome 的代理配置为http://127.0.0.1:8001
* 然后分别访问
* https://api.github.com/repos/temberature/anymock/branches?protected=0
* https://suggest.taobao.com/sug?code=utf-8&callback=KISSY.Suggest.callback&q=apple
* https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
* 你就可以看到相应的mock 结果了。

```json
[{
"name": "master1",
"commit": {
"sha": "51a0a39acfb1d029345e896cca6a6a2c3625816b",
"url": "https://api.github.com/repos/temberature/anymock/commits/51a0a39acfb1d029345e896cca6a6a2c3625816b"
}
}]
```

### And coding style tests

Explain what these tests test and why

```
Give an example
```json
KISSY.Suggest.callback(
{
"result": [
[
"apple watch4",
"14770"
],
[
"apple pencil",
"12500"
]
],
"shop": "apple",
"tmall": "apple"
}
)
```

## Deployment
```js
//uncompressed jquery
```

Add additional notes about how to deploy this on a live system
## 测试

## Built With
```bash
npm test
```

* [Dropwizard](http://www.dropwizard.io/1.0.2/docs/) - The web framework used
* [Maven](https://maven.apache.org/) - Dependency Management
* [ROME](https://rometools.github.io/rome/) - Used to generate RSS Feeds
## 开发

## Contributing
```bash
git clone https://github.com/temberature/anymock.git
cd anymock
npm install
npm start
```

Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us.
## 基础库

## Versioning
* [Anyproxy](https://www.npmjs.com/package/anyproxy)

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags).
## 贡献

## Authors
Please read [CONTRIBUTING.md](.github/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

* **Billie Thompson** - *Initial work* - [PurpleBooth](https://github.com/PurpleBooth)
## 版本化

See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project.
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags).

## License
## 协议

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

## Acknowledgments
## 致谢

* Hat tip to anyone whose code was used
* Inspiration
* etc
* Inspiration: [moco](https://github.com/dreamhead/moco),[wiremock](https://github.com/tomakehurst/wiremock)
4 changes: 4 additions & 0 deletions lib/anymock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const AnyProxy = require('anyproxy');
const fs = require('fs-extra')
const options = {
port: 8001,
rule: require('./anyproxy.config')(),
Expand All @@ -12,6 +13,9 @@ const options = {
wsIntercept: false, // 不开启websocket代理
silent: false
};
if (process.argv[2] === 'init') {
fs.copy(__dirname + '/../mock', process.cwd() + '/mock')
}
const proxyServer = new AnyProxy.ProxyServer(options);

proxyServer.on('ready', () => { /* */ });
Expand Down
1 change: 0 additions & 1 deletion lib/anyproxy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const {
URL,
URLSearchParams
} = require('url');
const R = require('ramda');
const removeProtocol = require('./removeProtocol')
const mock = require('./mock');

Expand Down
53 changes: 0 additions & 53 deletions lib/fileMock.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/startsWith.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require("fs");
const removeProtocol = require('./removeProtocol')

function startsWith(url, pattern) {
Expand Down
Loading

0 comments on commit 65968cd

Please sign in to comment.