Skip to content

Commit 298c583

Browse files
committed
fix: Fix a bug in the projects with installed dependencies
1 parent 9cc86f4 commit 298c583

File tree

14 files changed

+431
-414
lines changed

14 files changed

+431
-414
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"rules": {
1313
"no-explicit-any": 0,
1414
"no-non-null-assertion": 0,
15-
"no-useless-escape": 0
15+
"no-useless-escape": 0,
16+
"@typescript-eslint/no-var-requires": 0,
17+
"no-empty": 0
1618
}
1719
}

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ You can also write configuration file named `.coderflyrc`, mainly to simplify al
5151
// ...
5252
}
5353
```
54+
> The results are written to the impact_report.json file in the directory where the command was executed
5455
5556
![command line](./docs/pics/command_line.png)
5657

@@ -116,10 +117,19 @@ interface ImpactReason {
116117
}
117118
```
118119

120+
### matchVueVersion
121+
122+
Since the use of `vue-template-compiler` must be consistent with the `vue` version, otherwise an error will be reported, you must keep them both consistent before using `coderfly`. You can either manually install the corresponding version of `vue-template-compiler` in your project yourself, or you can use the API to do this in your code. Note that this operation needs to be called before using the other APIs.
123+
119124
## Example
120125

121126
```js
122-
import { diff, getAllFiles, getFuncTree, getImpacts } from "coderfly";
127+
// if necessary
128+
const { matchVueVersion } = require('coderfly/dist/match_version');
129+
130+
matchVueVersion();
131+
132+
const { diff, getAllFiles, getFuncTree, getImpacts } = require('coderfly');
123133

124134
// diff
125135
const functionDiffInfo = diff();

bin/coderfly.js

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
#!/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);
2+
const fs = require('fs');
3+
const path = require('path');
4+
const { program } = require('commander');
5+
const ora = require('ora');
6+
const { diff, getAllFiles, getFuncTree, getImpacts } = require('../dist/index.js');
107
const pkg = require('../package.json');
8+
const { CONFIG_FILENAME, TREE_FILE, REPORT_FILE } = require('../dist/const.js');
9+
const { parseAliasFromConfig, lookFileOrFolderUp } = require('../dist/utils/handle_config');
1110

12-
const CONFIG_FILENAME = '.coderflyrc';
13-
const TREE_FILE = path.resolve(process.cwd(), './file_tree.json');
14-
const REPORT_FILE = path.resolve(process.cwd(), './impact_report.json');
1511
const newsBoy = ora();
1612

1713
program
@@ -108,41 +104,4 @@ function parseAliasFromOptions (alias) {
108104
}
109105

110106
return result;
111-
}
112-
113-
function parseAliasFromConfig (config) {
114-
Object.keys(config).forEach(alias => {
115-
config[alias] = path.resolve(process.cwd(), config[alias]);
116-
});
117-
118-
return config;
119-
}
120-
121-
function lookFileOrFolderUp (target, baseDir) {
122-
const cwd = process.cwd();
123-
let oldPath = '';
124-
let newPath;
125-
126-
if (baseDir) {
127-
if (path.isAbsolute(baseDir)) {
128-
newPath = baseDir;
129-
} else {
130-
newPath = path.resolve(cwd, baseDir);
131-
}
132-
} else {
133-
newPath = cwd;
134-
}
135-
136-
while (oldPath !== newPath) {
137-
oldPath = newPath;
138-
const files = fs.readdirSync(newPath);
139-
for (const file of files) {
140-
if (file === target) {
141-
return newPath;
142-
}
143-
}
144-
newPath = path.dirname(oldPath);
145-
}
146-
return '';
147-
};
148-
107+
}

docs/README_CN.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
}
5151
```
5252

53+
> 分析结果会写入在执行命令的目录下的 impact_report.json 文件中
54+
5355
![command line](./pics/command_line.png)
5456

5557
**使用 API**
@@ -115,10 +117,20 @@ interface ImpactReason {
115117
}
116118
```
117119

120+
### matchVueVersion
121+
122+
由于 `vue-template-compiler` 的使用必须保持和 `vue` 版本一致,否则会报错,因此在使用 `coderfly` 前必须将二者保持一致。你可以自己手动在项目中安装对应的 `vue-template-compiler` 版本,也可以使用该 API 在代码中完成此操作。注意此操作需要在使用其他 API 之前调用
123+
124+
118125
## 示例
119126

120127
```js
121-
import { diff, getAllFiles, getFuncTree, getImpacts } from "coderfly";
128+
// 如果有必要
129+
const { matchVueVersion } = require('coderfly/dist/match_version');
130+
131+
matchVueVersion();
132+
133+
const { diff, getAllFiles, getFuncTree, getImpacts } = require('coderfly');
122134

123135
// diff
124136
const functionDiffInfo = diff();

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "coderfly",
33
"description": "Find function-level association impacts of code changes",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"author": "CocaColf <cocacolf@gmail.com>",
66
"main": "dist/index.js",
7-
"type": "module",
87
"bin": {
98
"coderfly": "bin/coderfly.js"
109
},
@@ -35,11 +34,10 @@
3534
"@types/lodash-es": "^4.17.6",
3635
"commander": "^9.1.0",
3736
"enhanced-resolve": "^5.9.2",
38-
"execa": "^6.1.0",
37+
"execa": "5.1.1",
3938
"lodash-es": "^4.17.21",
4039
"n-readlines": "^1.0.1",
41-
"ora": "^6.1.0",
42-
"parse-git-diff": "^0.0.6",
40+
"ora": "5.4.1",
4341
"recast": "^0.20.5",
4442
"vue-template-compiler": "^2.6.14"
4543
}

src/const.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import path from "path";
2+
13
export const ALLOW_EXT = ['.vue', '.js', '.ts'];
24
export const UN_KNOWN = 'unknown';
35
export const IS_TOP_SCOPE = '[is_top_scope]';
46

57
export const MUSTACHE_TAG_REG = /\{\{((?:.|\n)+?)\}\}/g;
68

79
export const TEXT_NODE_TYPES = [2, 3];
10+
11+
export const CONFIG_FILENAME = '.coderflyrc';
12+
export const TREE_FILE = path.resolve(process.cwd(), './file_tree.json');
13+
export const REPORT_FILE = path.resolve(process.cwd(), './impact_report.json');

src/match_version.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { commandSync } from "execa";
2+
import path from "path";
3+
import { lookFileOrFolderUp } from "./utils/handle_config.js";
4+
5+
export function matchVueVersion () {
6+
let vueVersion = 'latest';
7+
let vueTemplateCompilerVersion = '';
8+
9+
try {
10+
vueVersion = require('vue').version;
11+
const vtcDir = require.resolve('vue-template-compiler');
12+
if (vtcDir) {
13+
vueTemplateCompilerVersion = require(`${path.dirname(vtcDir)}/package.json`).version;
14+
}
15+
} catch (e) {}
16+
17+
if (vueVersion && vueTemplateCompilerVersion && vueTemplateCompilerVersion === vueVersion) {
18+
return;
19+
}
20+
21+
// use yarn or npm
22+
const yarnLockDir = lookFileOrFolderUp('yarn.lock', process.cwd());
23+
if (yarnLockDir) {
24+
try { commandSync('yarn remove vue-template-compiler') } catch {}
25+
commandSync(`yarn add vue-template-compiler@${vueVersion}`);
26+
} else {
27+
try { commandSync('npm uninstall vue-template-compiler') } catch {}
28+
commandSync(`npm install vue-template-compiler@${vueVersion}`);
29+
}
30+
}

src/utils/function_change/diff.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { execaCommandSync } from 'execa';
3+
import { commandSync } from 'execa';
44
import lineByLine from 'n-readlines';
55
import { parse, visit } from 'recast';
6-
import { createRequire } from 'module';
76
import { parseComponent } from 'vue-template-compiler';
87
import { DiffFunctionInfo, FunctionInfo } from '../../type';
98

@@ -23,7 +22,7 @@ export function getFunctionDiffInfo (filePath: string, commitSha?: string) {
2322
// treat it as a new file
2423
functionInfoBefore = {};
2524
} else {
26-
const beforeCtx = execaCommandSync(`git cat-file blob ${blobId}`).stdout;
25+
const beforeCtx = commandSync(`git cat-file blob ${blobId}`).stdout;
2726
const tempFile = `./temp_${path.posix.basename(filePath)}`;
2827
fs.writeFileSync(tempFile, beforeCtx);
2928
functionInfoBefore = getFunctionBlock(tempFile);
@@ -70,7 +69,6 @@ function getFunctionBlock (filePath: string) {
7069
}
7170

7271
try {
73-
const require = createRequire(import.meta.url);
7472
ast = parse(code, {
7573
parser: require('recast/parsers/babel'),
7674
});
@@ -214,7 +212,7 @@ function getFileCtxBeforeChange (filePath: string, commitSha?: string) {
214212
commitSha = latestCommitSha();
215213
}
216214

217-
const gitBlobs = execaCommandSync(`git ls-tree -r ${commitSha}`).stdout;
215+
const gitBlobs = commandSync(`git ls-tree -r ${commitSha}`).stdout;
218216
const blobArr = gitBlobs.split('\n');
219217

220218
for (const blobItem of blobArr) {
@@ -226,5 +224,5 @@ function getFileCtxBeforeChange (filePath: string, commitSha?: string) {
226224
}
227225

228226
function latestCommitSha () {
229-
return execaCommandSync('git rev-parse HEAD').stdout;
227+
return commandSync('git rev-parse HEAD').stdout;
230228
}

src/utils/function_change/file_change.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { execaCommandSync } from 'execa';
1+
import { commandSync } from 'execa';
22
import { FileChange } from '../../type';
33

44
const COMMANDS = ['git diff --name-status', 'git diff --name-status --staged'];
55

66
const formatList = (str: string, type: string) => {
77
const arr = str.split('\n').filter(item => {
8-
const regex = new RegExp(`[${type}].*`)
8+
const regex = new RegExp(`[${type}].*`);
99
if (regex.test(item)) {
1010
return item !== undefined;
1111
}
@@ -23,7 +23,7 @@ export function getFileChange () {
2323
const typeList = ['M', 'D', 'A'];
2424

2525
for (const command of COMMANDS) {
26-
const changeInfo = execaCommandSync(command).stdout;
26+
const changeInfo = commandSync(command).stdout;
2727

2828
typeList.forEach(type => {
2929
const formatResult = formatList(changeInfo, type);

src/utils/function_change/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from "path";
2+
import { ALLOW_EXT } from "../../const.js";
13
import { DiffInfo } from "../../type";
24
import { getFunctionDiffInfo } from "./diff.js";
35
import { getFileChange } from "./file_change.js";
@@ -11,6 +13,8 @@ export function diff () {
1113
const files = changedList[changeType];
1214

1315
for (const file of files) {
16+
if (!ALLOW_EXT.includes(path.extname(file))) continue;
17+
1418
result.push({
1519
file,
1620
...getFunctionDiffInfo(file)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy