Skip to content

Commit b68ef66

Browse files
committed
bug: 修复若干bug
1 parent 2ac5c02 commit b68ef66

File tree

7 files changed

+249
-23
lines changed

7 files changed

+249
-23
lines changed

bin/coderfly.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
const fs = require('fs');
2+
const fs = require('fs-extra');
33
const path = require('path');
44
const { program } = require('commander');
55
const ora = require('ora');
@@ -20,7 +20,7 @@ program
2020
.option('-alias, --alias <alias:path...>', 'set path alias')
2121
.option('-t, --tree', 'export the file tree to a file')
2222
.description('check association impacts of code changes')
23-
.action((srcPath, options) => {
23+
.action(async (srcPath, options) => {
2424
let alias = {};
2525

2626
if (options.alias) {
@@ -43,13 +43,12 @@ program
4343
newsBoy.succeed(' Function diff completed ');
4444

4545
const files = getAllFiles(path.resolve(process.cwd(), srcPath));
46-
47-
const tree = getFuncTree(files, {
46+
const tree = await getFuncTree([{files, options:{
4847
alias
49-
});
48+
}}]);
5049
newsBoy.succeed(' File tree build completed ');
5150
if (options.tree) {
52-
fs.writeFileSync(TREE_FILE, JSON.stringify(tree, null, 4));
51+
fs.outputJSON(TREE_FILE, tree, {spaces: '/t'});
5352
newsBoy.info(` You can check file tree from ${TREE_FILE} `);
5453
}
5554

@@ -74,7 +73,7 @@ program
7473
});
7574
newsBoy.succeed(' Association analysis completed ');
7675

77-
fs.writeFileSync(REPORT_FILE, JSON.stringify(impactReport, null, 4));
76+
fs.outputJsonSync(REPORT_FILE, impactReport, {spaces: '\t'});
7877

7978
newsBoy.info(` Job done! You can check the result from ${REPORT_FILE} `);
8079
});

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "coderfly",
2+
"name": "@semptian/coderfly",
33
"description": "Find function-level association impacts of code changes",
44
"version": "0.1.1",
55
"author": "CocaColf <cocacolf@gmail.com>",
@@ -17,7 +17,7 @@
1717
"url": "https://github.com/CocaColf/coderfly"
1818
},
1919
"scripts": {
20-
"build": "rm -rf dist/ && tsc && node ./copy_files.js && find ./dist -type f|xargs dos2unix",
20+
"build": "rimraf dist && tsc && node ./copy_files.js",
2121
"lint": "eslint . --ext .ts",
2222
"lint:fix": "eslint . --ext .ts --fix"
2323
},
@@ -27,6 +27,7 @@
2727
"@typescript-eslint/eslint-plugin": "^5.19.0",
2828
"@typescript-eslint/parser": "^5.19.0",
2929
"eslint": "^8.13.0",
30+
"rimraf": "^5.0.5",
3031
"typescript": "^4.6.3"
3132
},
3233
"dependencies": {
@@ -35,10 +36,12 @@
3536
"@babel/plugin-proposal-object-rest-spread": "^7.17.3",
3637
"@babel/preset-env": "^7.16.11",
3738
"@babel/preset-typescript": "^7.16.7",
39+
"@types/fs-extra": "^11.0.4",
3840
"commander": "^9.1.0",
3941
"de-indent": "^1.0.2",
4042
"enhanced-resolve": "^5.9.2",
4143
"execa": "5.1.1",
44+
"fs-extra": "^11.2.0",
4245
"he": "^1.2.0",
4346
"n-readlines": "^1.0.1",
4447
"nanoid": "^3.3.4",

src/coderfly.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import fs from 'fs';
2+
import fs from 'fs-extra';
33
import { diff } from './utils/function_change/index.js';
44
import { confirmFolderExist, getAllFiles, getFuncTree } from './utils/handle_file_utils.js';
55
import { getImpacts } from './impact.js';
@@ -39,7 +39,7 @@ export async function coderfly (srcPath: string, monorepo=false) {
3939
impactReport.push(impact);
4040
});
4141

42-
fs.writeFileSync(REPORT_FILE, JSON.stringify(impactReport, null, 4));
42+
fs.outputJsonSync(REPORT_FILE, impactReport, {spaces: '\t'});
4343
}
4444

4545
async function getSingleTree (srcPath: string) {
@@ -58,15 +58,13 @@ async function getSingleTree (srcPath: string) {
5858

5959
const files = getAllFiles(path.resolve(process.cwd(), srcPath));
6060

61-
const tree = await getFuncTree([{
61+
return getFuncTree([{
6262
srcPath,
6363
files,
6464
options: {
6565
alias
6666
},
6767
}]);
68-
69-
return tree;
7068
}
7169

7270
async function getMonorepoTree (srcPath: string) {
@@ -101,8 +99,7 @@ async function getMonorepoTree (srcPath: string) {
10199
});
102100
}
103101

104-
const tree = await getFuncTree(treeParams);
102+
return getFuncTree(treeParams);
105103

106-
return tree;
107104
}
108105

src/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface GetFileInfoWorkerData {
123123
}
124124

125125
export interface FuncTreeParam {
126-
srcPath: string;
126+
srcPath?: string;
127127
files: string[];
128128
options?: GetTreeOptions;
129129
}

src/utils/handle_file_utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from 'fs';
1+
import * as fs from 'fs-extra';
22
import * as path from 'path';
33
import { visit } from 'recast';
44
import { parse } from '@babel/parser';
@@ -17,6 +17,7 @@ import {
1717
} from '../type';
1818
import { getTemplateInfo } from './parse_template_ast.js';
1919
import { getFileInfoWorker } from '../worker/run_worker.js';
20+
import { cloneDeep } from './help.js';
2021

2122
const { create } = require('enhanced-resolve');
2223

@@ -81,7 +82,6 @@ async function getFuncTree (params: FuncTreeParam[]): Promise<FileInfoTree> {
8182
handledMixinPath.push(mixinPath);
8283

8384
const mixinInfo = tree[mixinPath];
84-
8585
// if meet a same function name: component first
8686
for (const functionName in mixinInfo['allFuncsInfo']) {
8787
if (!tree[file]['allFuncsInfo'][functionName]) {
@@ -126,8 +126,16 @@ async function getFuncTree (params: FuncTreeParam[]): Promise<FileInfoTree> {
126126
}
127127
});
128128
}
129-
130-
fs.writeFileSync(TREE_FILE, JSON.stringify(tree, null, 4));
129+
// 修改output tree中的filepath加上行号,方便再文件中直接跳转
130+
const _tree = cloneDeep(tree)
131+
Object.keys(_tree).forEach(k => {
132+
const fileInfo = tree[k]
133+
Object.keys(fileInfo.allFuncsInfo).forEach(fk => {
134+
const fnInfo = fileInfo.allFuncsInfo[fk]
135+
fnInfo.filePath = fnInfo.filePath + ':' + fnInfo.position?.replace('L', '')
136+
})
137+
})
138+
fs.outputJSON(TREE_FILE, tree, {spaces: '\t'});
131139

132140
return tree;
133141
}

src/utils/help.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function cloneDeep (object: object): object {
2+
return JSON.parse(JSON.stringify(object))
3+
}
4+
5+
export {
6+
cloneDeep
7+
}

0 commit comments

Comments
 (0)