Skip to content

Commit

Permalink
feat: support just remove suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
zivyangll committed Sep 20, 2019
1 parent 930553d commit 6c9f19a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -31,8 +31,8 @@ $ datahub-import-json projectName
| -s | DataHub server address | http://127.0.0.1:5678 |
| -d | mock data directory path | - |
| --mockSuffix | set mock file suffix | .json |
| --interfaceSuffix | DataHub interface suffix | .json |
| --mockRemoveSuffix | replace mockRemoveSuffix to interfaceSuffix | - |
| --interfaceSuffix | DataHub interface suffix, will replace mockSuffix | .json |
| --mockRemoveSuffix | remove mockRemoveSuffix, no repalce | - |
| --method | DataHub interface method | ALL |

## Examples
Expand Down Expand Up @@ -61,10 +61,10 @@ DataHub server 地址为:http://127.0.0.1:5678/

```bash
# post 请求
$ datahub-import-json test-project -d /Users/xxx/mock/post/ --interfaceSuffix .html --mockRemoveSuffix /data.json --method POST -s http://127.0.0.1:5678
$ datahub-import-json test-project -d /Users/xxx/mock/post/ --mockRemoveSuffix /data.json --method POST -s http://127.0.0.1:5678

# get 请求
$ datahub-import-json test-project -d /Users/xxx/mock/get/ --interfaceSuffix .html --mockRemoveSuffix /data.json --method GET -s http://127.0.0.1:5678
$ datahub-import-json test-project -d /Users/xxx/mock/get/ --mockRemoveSuffix /data.json --method GET -s http://127.0.0.1:5678
```

---
Expand Down
15 changes: 6 additions & 9 deletions lib/service.js
Expand Up @@ -24,18 +24,15 @@ async function addProject(options) {

// 添加 DataHub 接口
async function addInterface(filepath, projectUniqId, options) {
const relaceStr = options.mockRemoveSuffix
? options.mockRemoveSuffix
: options.mockSuffix;

let pathname = filepath.replace(options.mockDir + '/', '');
if (pathname.indexOf(options.interfaceSuffix) !== -1) {
// 如果后缀已经存在,则删除
pathname = pathname.replace(new RegExp(relaceStr + '$'), '');

// 只进行删除,不替换后缀
if (options.mockRemoveSuffix) {
pathname = pathname.replace(new RegExp(options.mockRemoveSuffix + '$'), '');
} else {
// 如果后缀不存在,则添加
// 进行替换
pathname = pathname.replace(
new RegExp(relaceStr + '$'),
new RegExp(options.mockSuffix + '$'),
options.interfaceSuffix
);
}
Expand Down
16 changes: 10 additions & 6 deletions lib/utils.js
Expand Up @@ -35,12 +35,16 @@ function handleJsonToJsSycn(mockPath, mockSuffix) {
mockPath.forEach(itemPath => {
const jsFilePath = itemPath.replace(new RegExp(mockSuffix + '$'), '.js');
if (fs.existsSync(jsFilePath)) {
const js = require(jsFilePath);
const json = require(itemPath);
if (typeof js === 'function') {
fs.writeFileSync(itemPath, JSON.stringify(js(json)));
} else if (typeof js === 'object') {
fs.writeFileSync(itemPath, JSON.stringify(js));
try {
const js = require(jsFilePath);
const json = require(itemPath);
if (typeof js === 'function' && json) {
fs.writeFileSync(itemPath, JSON.stringify(js(json)));
} else if (typeof js === 'object') {
fs.writeFileSync(itemPath, JSON.stringify(js));
}
} catch (e) {
console.log(e);
}
}
});
Expand Down

0 comments on commit 6c9f19a

Please sign in to comment.