Skip to content

Commit 2ac5c02

Browse files
committed
feat: support Monorepo
1 parent 5445ad0 commit 2ac5c02

File tree

4 files changed

+123
-55
lines changed

4 files changed

+123
-55
lines changed

src/coderfly.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import { diff } from './utils/function_change/index.js';
4+
import { confirmFolderExist, getAllFiles, getFuncTree } from './utils/handle_file_utils.js';
5+
import { getImpacts } from './impact.js';
6+
import { CONFIG_FILENAME, IGNORE_DIRS, REPORT_FILE } from './const.js';
7+
import { lookFileOrFolderUp } from './utils/handle_config.js';
8+
import { FileInfoTree, FuncTreeParam, ImpactReason } from './type.js';
9+
10+
export async function coderfly (srcPath: string, monorepo=false) {
11+
confirmFolderExist();
12+
13+
let tree: FileInfoTree = {};
14+
if (!monorepo) {
15+
tree = await getSingleTree(srcPath);
16+
} else {
17+
tree = await getMonorepoTree(srcPath);
18+
}
19+
20+
const functionDiffInfo = diff();
21+
22+
const allFunctions: ImpactReason[] = [];
23+
functionDiffInfo.forEach(item => {
24+
const file = path.resolve(process.cwd(), item.file);
25+
26+
item.total.forEach(fn => {
27+
allFunctions.push({
28+
filePath: file,
29+
name: fn,
30+
paths: [[fn, file]]
31+
});
32+
});
33+
});
34+
35+
const impactReport: any[] = [];
36+
37+
allFunctions.forEach(item => {
38+
const impact = getImpacts(tree, item);
39+
impactReport.push(impact);
40+
});
41+
42+
fs.writeFileSync(REPORT_FILE, JSON.stringify(impactReport, null, 4));
43+
}
44+
45+
async function getSingleTree (srcPath: string) {
46+
let alias = {};
47+
const configFolder = lookFileOrFolderUp(CONFIG_FILENAME, path.resolve(process.cwd(), srcPath));
48+
49+
if (configFolder) {
50+
const configFile = path.resolve(configFolder, CONFIG_FILENAME);
51+
52+
try {
53+
alias = require(configFile);
54+
} catch (error){
55+
// do nothing
56+
}
57+
}
58+
59+
const files = getAllFiles(path.resolve(process.cwd(), srcPath));
60+
61+
const tree = await getFuncTree([{
62+
srcPath,
63+
files,
64+
options: {
65+
alias
66+
},
67+
}]);
68+
69+
return tree;
70+
}
71+
72+
async function getMonorepoTree (srcPath: string) {
73+
const rootDir = path.resolve(process.cwd(), srcPath);
74+
const subPackages = fs.readdirSync(rootDir);
75+
76+
const treeParams: FuncTreeParam[] = [];
77+
78+
for (const subPackage of subPackages) {
79+
if (IGNORE_DIRS.includes(subPackage)) continue;
80+
81+
const subPackagePath = path.resolve(srcPath, subPackage);
82+
const configFile = path.resolve(subPackagePath, CONFIG_FILENAME);
83+
let alias = {};
84+
85+
if (fs.existsSync(configFile)) {
86+
try {
87+
alias = require(configFile);
88+
} catch (error) {
89+
console.log(`get configure failed.File path ${path.resolve(srcPath, subPackage)}`);
90+
}
91+
}
92+
93+
const files = getAllFiles(subPackagePath);
94+
95+
treeParams.push({
96+
srcPath: subPackagePath,
97+
files,
98+
options: {
99+
alias
100+
},
101+
});
102+
}
103+
104+
const tree = await getFuncTree(treeParams);
105+
106+
return tree;
107+
}
108+

src/index.ts

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,8 @@
1-
import path from 'path';
2-
import fs from 'fs';
31
import { diff } from './utils/function_change/index.js';
4-
import { confirmFolderExist, getAllFiles, getFuncTree } from './utils/handle_file_utils.js';
2+
import { getAllFiles, getFuncTree } from './utils/handle_file_utils.js';
53
import { getImpacts, findWhoCallMe } from './impact.js';
64
import { getTemplateInfo } from './utils/parse_template_ast.js';
7-
import { CONFIG_FILENAME, REPORT_FILE } from './const.js';
8-
import { lookFileOrFolderUp } from './utils/handle_config.js';
9-
import { ImpactReason } from './type.js';
10-
11-
async function coderfly (srcPath: string) {
12-
let alias = {};
13-
const configFolder = lookFileOrFolderUp(CONFIG_FILENAME, path.resolve(process.cwd(), srcPath));
14-
15-
if (configFolder) {
16-
const configFile = path.resolve(configFolder, CONFIG_FILENAME);
17-
18-
try {
19-
alias = require(configFile);
20-
} catch (error){
21-
// do nothing
22-
}
23-
}
24-
25-
confirmFolderExist();
26-
27-
const functionDiffInfo = diff();
28-
29-
const files = getAllFiles(path.resolve(process.cwd(), srcPath));
30-
31-
const tree = await getFuncTree(files, {
32-
alias
33-
});
34-
35-
const allFunctions: ImpactReason[] = [];
36-
functionDiffInfo.forEach(item => {
37-
const file = path.resolve(process.cwd(), item.file);
38-
39-
item.total.forEach(fn => {
40-
allFunctions.push({
41-
filePath: file,
42-
name: fn,
43-
paths: [[fn, file]]
44-
});
45-
});
46-
});
47-
48-
const impactReport: any[] = [];
49-
50-
allFunctions.forEach(item => {
51-
const impact = getImpacts(tree, item);
52-
impactReport.push(impact);
53-
});
54-
55-
fs.writeFileSync(REPORT_FILE, JSON.stringify(impactReport, null, 4));
56-
}
5+
import { coderfly } from './coderfly.js';
576

587
export {
598
getAllFiles,

src/type.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,10 @@ export interface GetFileInfoWorkerData {
120120
};
121121
tree: FileInfoTree;
122122
options?: GetTreeOptions;
123+
}
124+
125+
export interface FuncTreeParam {
126+
srcPath: string;
127+
files: string[];
128+
options?: GetTreeOptions;
123129
}

src/utils/handle_file_utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
FileAstInfo,
1111
FileInfo,
1212
FileInfoTree,
13+
FuncTreeParam,
1314
GetTreeOptions,
1415
NameAndPath,
1516
TemplateKeyInfo
@@ -48,8 +49,12 @@ function getAllFiles (folderPath: string): string[] {
4849
return fileList;
4950
}
5051

51-
async function getFuncTree (files: string[], options?: GetTreeOptions): Promise<FileInfoTree> {
52-
const tree = await getFileInfoWorker(files, options);
52+
async function getFuncTree (params: FuncTreeParam[]): Promise<FileInfoTree> {
53+
const tree: FileInfoTree = {};
54+
for (const item of params) {
55+
const curTree = await getFileInfoWorker(item.files, item.options);
56+
Object.assign(tree, curTree);
57+
}
5358

5459
for (const file in tree) {
5560
const fileInfo = tree[file];

0 commit comments

Comments
 (0)