Skip to content

Commit 9c7bd9b

Browse files
committed
feat: apply one-step process api
1 parent e2feb32 commit 9c7bd9b

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ module.exports = {
6161
see the [API](#api) or [Example](#example).
6262
## API
6363

64+
### coderfly
65+
66+
The API for the complete process is included, use this for a one-step process if you don't need control the process yourself.
67+
68+
**Params**
69+
70+
- srcPath: string. It's source code folder path
6471
### diff
6572

6673
Get the changes of the function by the changes of the file.
@@ -124,6 +131,21 @@ Since the use of `vue-template-compiler` must be consistent with the `vue` versi
124131

125132
## Example
126133

134+
**Easy to use**
135+
136+
```js
137+
// if necessary
138+
const { matchVueVersion } = require('coderfly/dist/match_version');
139+
140+
matchVueVersion();
141+
142+
const { coderfly } = require('coderfly');
143+
144+
coderfly('./src');
145+
```
146+
147+
148+
**If you want you control the process yourself**
127149
```js
128150
// if necessary
129151
const { matchVueVersion } = require('coderfly/dist/match_version');

docs/README_CN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ module.exports = {
6161

6262
## API
6363

64+
### coderfly
65+
66+
API集大成者,如果不需要自己字定义过程,用这个一步到位即可.
67+
68+
**Params**
69+
70+
- srcPath: string. It's source code folder path
71+
72+
**Params**
73+
74+
- srcPath: string,源码文件夹路径
6475
### diff
6576

6677
根据本地文件变动,输出函数修改情况。

src/const.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const MUSTACHE_TAG_REG = /\{\{((?:.|\n)+?)\}\}/g;
99

1010
export const TEXT_NODE_TYPES = [2, 3];
1111

12+
export const CODERFLY_FOLDER = path.resolve(process.cwd(), '.coderfly');
1213
export const CONFIG_FILENAME = '.coderflyrc.js';
13-
export const TREE_FILE = path.resolve(process.cwd(), './file_tree.json');
14-
export const REPORT_FILE = path.resolve(process.cwd(), './impact_report.json');
14+
export const MATCHED_VUE = path.resolve(CODERFLY_FOLDER, 'matched_vue');
15+
export const TREE_FILE = path.resolve(CODERFLY_FOLDER, 'file_tree.json');
16+
export const REPORT_FILE = path.resolve(CODERFLY_FOLDER, 'impact_report.json');

src/index.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
1+
import path from 'path';
2+
import fs from 'fs';
13
import { diff } from './utils/function_change/index.js';
2-
import { getAllFiles, getFuncTree } from './utils/handle_file_utils.js';
4+
import { confirmFolderExist, getAllFiles, getFuncTree } from './utils/handle_file_utils.js';
35
import { getImpacts, findWhoCallMe } from './impact.js';
46
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 = 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+
}
557

658
export {
759
getAllFiles,
@@ -10,4 +62,5 @@ export {
1062
getImpacts,
1163
findWhoCallMe,
1264
diff,
65+
coderfly,
1366
};

src/utils/handle_file_utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { visit } from 'recast';
44
import { parse } from '@babel/parser';
55
import { parseComponent, compile } from 'vue-template-compiler';
66
import lineByLine from 'n-readlines';
7-
import { ALLOW_EXT, IS_TOP_SCOPE, TS_DECLARATION_EXT, UN_KNOWN } from '../const.js';
7+
import { ALLOW_EXT, CODERFLY_FOLDER, IS_TOP_SCOPE, TS_DECLARATION_EXT, UN_KNOWN } from '../const.js';
88
import {
99
AllFuncsInfo,
1010
FileAstInfo,
@@ -462,11 +462,17 @@ function isAllowExt (filePath: string) {
462462
return ALLOW_EXT.includes(path.extname(filePath)) && filePath.indexOf(TS_DECLARATION_EXT) === -1;
463463
}
464464

465+
function confirmFolderExist () {
466+
if (!fs.existsSync(CODERFLY_FOLDER)) {
467+
fs.mkdirSync(CODERFLY_FOLDER);
468+
}
469+
}
465470

466471
export {
467472
getFileInfo,
468473
getVueScriptRealStartLine,
469474
getFuncTree,
470475
getAllFiles,
471476
isAllowExt,
477+
confirmFolderExist,
472478
};

0 commit comments

Comments
 (0)