Skip to content

Commit

Permalink
feat: handle js file
Browse files Browse the repository at this point in the history
  • Loading branch information
zivyangll committed Sep 20, 2019
1 parent ffe2b9b commit 04ff2c8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ $ npm i datahub-import-json -g
## Usage

```bash
$ datahub-import-json test-project -d /Users/xxx/mock -s http://127.0.0.1:5678
$ datahub-import-json test-project -d /Users/xxx/mock/get --method GET
$ datahub-import-json test-project -d /Users/xxx/mock/post --method POST
```

## Configuration
Expand All @@ -31,6 +32,40 @@ $ datahub-import-json projectName
| -d | mock data directory path | - |
| --mockSuffix | set mock file suffix | .json |
| --interfaceSuffix | DataHub interface suffix | .json |
| --mockRemoveSuffix | replace mockRemoveSuffix to interfaceSuffix | - |
| --method | DataHub interface method | ALL |

## Examples

### Import JSON interface

假设项目名为:test-project(建议与仓库名称保持一致)
mock 数据文件件路径为:/Users/xxx/mock
DataHub server 地址为:http://127.0.0.1:5678/
想导入的 Mock 文件后缀为:.json
导入后的 Mock 接口后缀为:.html

```bash
$ datahub-import-json test-project -d /Users/xxx/mock --interfaceSuffix .html -s http://127.0.0.1:5678
```

### Import js interface

假设项目名为:test-project(建议与仓库名称保持一致)
mock 数据文件件路径为:/Users/xxx/mock
DataHub server 地址为:http://127.0.0.1:5678/
想导入的 Mock 文件路径为:
/Users/xxx/mock/post/member/xxx.html/data.json
/Users/xxx/mock/post/member/xxx.html/data.js
导入后的 Mock 接口路径为:/member/xxx.html

```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

# 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
```

---

Expand Down
9 changes: 8 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ cli
.option('-s, --server <server>', 'DataHub server address', {
default: 'http://127.0.0.1:5678',
})
.option('--mockSuffix <mockSuffix>', 'set mock file suffix', {
.option('--mockSuffix <mockSuffix>', 'filter mock file with mockSuffix', {
default: '.json',
})
.option('--interfaceSuffix <interfaceSuffix>', 'DataHub interface suffix', {
default: '.json',
})
.option(
'--mockRemoveSuffix <mockRemoveSuffix>',
'replace mockRemoveSuffix to interfaceSuffix'
)
.option('--method <method>', 'DataHub interface method', {
default: 'ALL',
})
.action(async (projectName, options) => {
console.log('==============options============');
console.log(options);
Expand Down
26 changes: 18 additions & 8 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@ async function addProject(options) {

// 添加 DataHub 接口
async function addInterface(filepath, projectUniqId, options) {
const pathname = options.interfaceSuffix
? filepath.replace(
new RegExp(options.mockSuffix + '$'),
options.interfaceSuffix
)
: filepath;
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 + '$'), '');
} else {
// 如果后缀不存在,则添加
pathname = pathname.replace(
new RegExp(relaceStr + '$'),
options.interfaceSuffix
);
}

const interfaceData = await fetch(`${options.server}/api/interface`, {
method: 'POST',
headers: {
Expand All @@ -38,8 +48,8 @@ async function addInterface(filepath, projectUniqId, options) {
},
body: JSON.stringify({
description: '.',
method: 'ALL',
pathname: pathname.replace(options.mockDir + '/', ''),
method: options.method,
pathname,
projectUniqId,
}),
})
Expand Down
27 changes: 24 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,47 @@ function getMockFilePath(options) {
const mockDir = path.join(options.mockDir);
const mockSuffix = options.mockSuffix;
const mockPath = [];

// 获取所有 JSON 文件
readDirSync(mockDir, mockPath, mockSuffix);

// 处理同级目录下的同名 JS 文件,将拿到的数据覆盖对应 JSON 文件
handleJsonToJsSycn(mockPath, mockSuffix);

return mockPath;
}

// 获取目录下所有 mock 文件地址
function readDirSync(dir, mockPath, mockSuffix) {
const pa = fs.readdirSync(dir);
pa.forEach(ele => {
const childrenPath = path.join(dir, ele);
pa.forEach(itemPath => {
const childrenPath = path.join(dir, itemPath);
const info = fs.statSync(childrenPath);
if (info.isDirectory()) {
readDirSync(childrenPath, mockPath, mockSuffix);
} else {
if (new RegExp(mockSuffix + '$').test(ele)) {
if (new RegExp(mockSuffix + '$').test(itemPath)) {
mockPath.push(childrenPath);
}
}
});
}

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));
}
}
});
}

module.exports = {
getMockFilePath,
};

0 comments on commit 04ff2c8

Please sign in to comment.