Skip to content

Commit 3c96427

Browse files
committed
feat(command line): Support command line usage
1 parent 0d1401b commit 3c96427

File tree

6 files changed

+289
-12
lines changed

6 files changed

+289
-12
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,21 @@ This project is still under development and has not been published to the npm ye
3030

3131
## Usage
3232

33-
**Using the API**
33+
**Using the command line**
3434

35-
see the [API](#api) or [Example](#example).
35+
`coderfly check <folder path>`
3636

37-
**Using the command line**
37+
Options:
3838

39-
> Not yet finished
39+
- alias: Set path alias, alias and path should be linked with a **colon**. eg: `coderfly check ./src -alias src:./src static:./public`
4040

41-
- [ ] Using command line: `cci check <folder path>`
41+
- t or tree: Export the file tree to a file, the file defaults to `file_tree.json`. eg: `coderfly check ./src -t`
4242

43+
![command line](./docs/pics/command_line.png)
44+
45+
**Using the API**
46+
47+
see the [API](#api) or [Example](#example).
4348
## API
4449

4550
### diff

bin/coderfly.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
import { program } from 'commander';
6+
import { createRequire } from "module";
7+
import ora from 'ora';
8+
import { diff, getAllFiles, getFuncTree, getImpacts } from '../dist/index.js';
9+
const require = createRequire(import.meta.url);
10+
const pkg = require('../package.json');
11+
12+
const TREE_FILE = path.resolve(process.cwd(), './file_tree.json');
13+
const REPORT_FILE = path.resolve(process.cwd(), './impact_report.json');
14+
const newsBoy = ora();
15+
16+
program
17+
.version(pkg.version)
18+
.usage('<command>')
19+
.description('Find function-level association impacts of code changes');
20+
21+
program
22+
.command('check <srcPath>')
23+
.option('-alias, --alias <alias:path...>', 'set path alias')
24+
.option('-t, --tree', 'export the file tree to a file')
25+
.description('check association impacts of code changes')
26+
.action((srcPath, options) => {
27+
let alias = {};
28+
29+
if (options.alias) {
30+
alias = parseAlias(options.alias);
31+
}
32+
33+
const functionDiffInfo = diff();
34+
newsBoy.succeed(' Function diff completed ');
35+
36+
const files = getAllFiles(path.resolve(process.cwd(), srcPath));
37+
38+
const tree = getFuncTree(files, {
39+
alias
40+
});
41+
newsBoy.succeed(' File tree build completed ');
42+
if (options.tree) {
43+
fs.writeFileSync(TREE_FILE, JSON.stringify(tree, null, 4));
44+
newsBoy.info(`You can check file tree from ${TREE_FILE}`);
45+
}
46+
47+
let allFunctions = [];
48+
functionDiffInfo.forEach(item => {
49+
const file = path.resolve(process.cwd(), item.file);
50+
51+
item.total.forEach(fn => {
52+
allFunctions.push({
53+
filePath: file,
54+
name: fn,
55+
paths: [[fn, file]]
56+
});
57+
});
58+
});
59+
60+
let impactReport = [];
61+
62+
allFunctions.forEach(item => {
63+
let impact = getImpacts(tree, item);
64+
impactReport.push(impact);
65+
});
66+
newsBoy.succeed(' Association analysis completed ');
67+
68+
fs.writeFileSync(REPORT_FILE, JSON.stringify(impactReport, null, 4));
69+
70+
newsBoy.info(`Job done! You can check the result from ${REPORT_FILE}`);
71+
});
72+
73+
program.parse(process.argv);
74+
75+
function parseAlias (alias) {
76+
let result = {};
77+
if (typeof alias === 'string') {
78+
alias = [alias];
79+
}
80+
81+
for (let item of alias) {
82+
if (!/\S+:\S+/g.test(item)) {
83+
continue;
84+
}
85+
86+
const splitRes = item.split(':');
87+
88+
// because it's key:value, so compare with 2
89+
if (splitRes.length !== 2) {
90+
continue;
91+
}
92+
93+
result[splitRes[0]] = path.resolve(process.cwd(), splitRes[1]);
94+
}
95+
96+
return result;
97+
}
98+

docs/README_CN.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@
2828

2929
## 使用
3030

31-
**使用 API**
31+
**使用命令行**
3232

33-
可以参考 [API](#api) 或者[示例代码](#示例)
33+
`coderfly check <folder path>`
3434

35-
**使用命令行**
35+
参数:
36+
37+
- `alias`: 设置路径别名, 别名和路径要以 **:** 连接。示例: `coderfly check ./src -alias src:./src static:./public`
3638

37-
> 当前还未实现
39+
- `t` 或者 `tree`: 将文件树以文件形式输出,默认为 `file_tree.json`。 示例: `coderfly check ./src -t`
3840

39-
- [ ] 使用命令行 `cci check <folder path>`
41+
![command line](./pics/command_line.png)
42+
43+
**使用 API**
44+
45+
可以参考 [API](#api) 或者[示例代码](#示例)
4046

4147
## API
4248

docs/pics/command_line.png

14.9 KB
Loading

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"main": "index.js",
55
"license": "MIT",
66
"type": "module",
7+
"bin": {
8+
"coderfly": "bin/coderfly.js"
9+
},
10+
"files": [
11+
"bin",
12+
"dist"
13+
],
714
"scripts": {
815
"build": "rm -rf dist/ && tsc && find ./dist -type f|xargs dos2unix",
916
"lint": "eslint . --ext .ts",
@@ -20,10 +27,12 @@
2027
"dependencies": {
2128
"@babel/parser": "^7.17.8",
2229
"@types/lodash-es": "^4.17.6",
30+
"commander": "^9.1.0",
2331
"enhanced-resolve": "^5.9.2",
2432
"execa": "^6.1.0",
2533
"lodash-es": "^4.17.21",
2634
"n-readlines": "^1.0.1",
35+
"ora": "^6.1.0",
2736
"parse-git-diff": "^0.0.6",
2837
"recast": "^0.20.5",
2938
"vue-template-compiler": "^2.6.14"

0 commit comments

Comments
 (0)