Skip to content

Commit 2fb82b4

Browse files
committed
feat: 支持scan方法
1 parent c19205d commit 2fb82b4

File tree

25 files changed

+308
-13
lines changed

25 files changed

+308
-13
lines changed

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,36 @@ astraea.json(
8484
}
8585
)
8686
```
87+
88+
89+
### scan
90+
91+
`astraea.scan` based on `astraea.json`, it will scan all `.js` files of `inputDir`.
92+
93+
[Demo](./example/scan)
94+
95+
[Test](./test/scan.js)
96+
#### options
97+
- `inputDir`, .js dir
98+
- `outputDir`, output directory
99+
- `opt`,
100+
- `blackList`, filter `path`
101+
- `optional`, boolean, all params are optinaol
102+
- `suffix`, file format, eg: `ts` or `d.ts`
103+
```js
104+
const astraea = require('../index.js');
105+
const path = require('path');
106+
107+
astraea.scan({
108+
inputDir: path.resolve(__dirname, './scanData'),
109+
outputDir: path.resolve(__dirname, '../example/scan'),
110+
request: {
111+
optional: true,
112+
},
113+
response: {},
114+
});
115+
```
116+
87117
## How to use?
88118
this is a simple example, you need to rework if use in prod!
89119
```js
@@ -177,4 +207,5 @@ export interface IDeleteRoutes {
177207
'/store/order/:orderId': DeleteStoreOrderOrderId;
178208
'/user/:username': DeleteUserUsername;
179209
}
180-
```
210+
```
211+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface RootObject {
2+
groupId?: number;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface RootObject {}

example/scan/requests/routes.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { RootObject as GetApiV1AccountsToken } from './GET/api/v1/accounts/token';
2+
import { RootObject as GetApiV1AccountsId } from './GET/api/v1/accounts/:id';
3+
4+
export interface IGetRoutes {
5+
'/api/v1/accounts/token': GetApiV1AccountsToken;
6+
'/api/v1/accounts/:id': GetApiV1AccountsId;
7+
}
8+
9+
export interface IPostRoutes {
10+
11+
}
12+
13+
export interface IPutRoutes {
14+
15+
}
16+
17+
export interface IDeleteRoutes {
18+
19+
}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface RootObject {
2+
name: string;
3+
id: number;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface RootObject {
2+
token: string;
3+
}

example/scan/responses/routes.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { RootObject as GetApiV1AccountsToken } from './GET/api/v1/accounts/token';
2+
import { RootObject as GetApiV1AccountsId } from './GET/api/v1/accounts/:id';
3+
4+
export interface IGetRoutes {
5+
'/api/v1/accounts/token': GetApiV1AccountsToken;
6+
'/api/v1/accounts/:id': GetApiV1AccountsId;
7+
}
8+
9+
export interface IPostRoutes {
10+
11+
}
12+
13+
export interface IPutRoutes {
14+
15+
}
16+
17+
export interface IDeleteRoutes {
18+
19+
}
20+

lib/src/adapters/scan.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { IOpt } from './json';
2+
export interface IOptions {
3+
inputDir: string;
4+
outputDir: string;
5+
request?: IOpt;
6+
response?: IOpt;
7+
}
8+
declare const scan: (options: IOptions) => void;
9+
export default scan;

lib/src/adapters/scan.js

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/adapters/scan.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare const _default: {
22
json: (json: import("./adapters/json").IJson, dir: string, opt?: import("./adapters/json").IOpt | undefined) => void;
33
swagger: (swagger: any, dir: string, opt?: import("./adapters/swagger").IOpt | undefined) => Promise<void>;
4+
scan: (options: import("./adapters/scan").IOptions) => void;
45
};
56
export default _default;

lib/src/index.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/utils/getAllFiles.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const getAllFiles: (dir: string, suffix: string) => string[];

lib/src/utils/getAllFiles.js

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/utils/getAllFiles.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+22-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
"homepage": "https://github.com/ShanaMaid/Astraea#readme",
3737
"dependencies": {
3838
"@types/fs-extra": "^5.0.5",
39-
"@types/node": "^11.9.5",
39+
"@types/node": "^11.15.14",
4040
"@types/underscore": "^1.8.9",
4141
"fs-extra": "^7.0.1",
4242
"swagmock": "^1.0.0",
43-
"tslib": "^1.9.3",
43+
"tslib": "2.0.0",
4444
"typescript": "^3.3.3333",
4545
"underscore": "^1.9.1"
4646
},

src/adapters/scan.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as path from 'path';
2+
import {
3+
IOpt,
4+
IApi,
5+
} from './json';
6+
import parseJSON from './json';
7+
import { getAllFiles } from '../utils/getAllFiles';
8+
9+
export interface IOptions {
10+
inputDir: string;
11+
outputDir: string;
12+
request?: IOpt;
13+
response?: IOpt;
14+
}
15+
16+
type Method = 'request' | 'response';
17+
18+
const transform = (method: Method, data: IApi) => {
19+
const json: {[i: string]: any} = {
20+
get: {},
21+
post: {},
22+
put: {},
23+
delete: {},
24+
};
25+
// eslint-disable-next-line no-restricted-syntax
26+
const paths = Object.keys(data);
27+
// eslint-disable-next-line
28+
for (const api of paths) {
29+
console.log(`scan api - ${method} -> ${api}`);
30+
const result = api.split(' ');
31+
const target = data[api][method];
32+
json[result[0].toLowerCase()][result[1]] = target;
33+
}
34+
return json;
35+
};
36+
37+
const scan = (options: IOptions) => {
38+
const {
39+
inputDir,
40+
outputDir,
41+
request: requestOpt,
42+
response: responseOpt,
43+
} = options;
44+
const jsFiles = getAllFiles(inputDir, 'js');
45+
const files: IApi = jsFiles.reduce((pre, cur) => {
46+
return { ...pre, ...require(cur) };
47+
}, jsFiles[0] ? require(jsFiles[0]) : {});
48+
const requestJSON = transform('request', files);
49+
const responseJSON = transform('response', files);
50+
/**
51+
* 生成请求参数
52+
*/
53+
parseJSON(requestJSON, path.resolve(outputDir, './requests'), requestOpt);
54+
55+
/**
56+
* 生成响应参数
57+
*/
58+
parseJSON(responseJSON, path.resolve(outputDir, './responses'), responseOpt);
59+
60+
console.log(`scan success! total: ${Object.keys(files).length} API`);
61+
62+
};
63+
64+
export default scan;

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import json from './adapters/json';
22
import swagger from './adapters/swagger';
3+
import scan from './adapters/scan';
4+
35

46
export default {
57
json,
6-
swagger
8+
swagger,
9+
scan,
710
};

src/utils/getAllFiles.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
export const getAllFiles = (dir: string, suffix: string) => {
5+
const paths: string[] = [];
6+
const files = fs.readdirSync(dir);
7+
// eslint-disable-next-line no-restricted-syntax
8+
for (const file of files) {
9+
const filePath = path.resolve(dir, file);
10+
const stat = fs.lstatSync(filePath);
11+
if (stat.isDirectory()) {
12+
paths.push(...getAllFiles(filePath, suffix));
13+
} else if (file.match(new RegExp(`.${suffix}$`))) {
14+
paths.push(filePath);
15+
}
16+
}
17+
return paths;
18+
};

test/scan.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const astraea = require('../index.js');
2+
const path = require('path');
3+
4+
astraea.scan({
5+
inputDir: path.resolve(__dirname, './scanData'),
6+
outputDir: path.resolve(__dirname, '../example/scan'),
7+
request: {
8+
optional: true,
9+
},
10+
response: {},
11+
});

0 commit comments

Comments
 (0)