js-code-detector 0.0.48 → 0.0.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/cjs/index.d.ts +1 -43
  2. package/dist/cjs/index.js +3 -25
  3. package/dist/cjs/services/DetectService.d.ts +3 -1
  4. package/dist/cjs/services/DetectService.js +30 -7
  5. package/dist/cjs/services/ProjectService.d.ts +1 -2
  6. package/dist/cjs/services/projectServiceClass/UmiProjectService.d.ts +4 -0
  7. package/dist/cjs/services/projectServiceClass/UmiProjectService.js +11 -7
  8. package/dist/cjs/services/projectServiceClass/VueProjectService.d.ts +31 -0
  9. package/dist/cjs/services/projectServiceClass/VueProjectService.js +191 -0
  10. package/dist/cjs/util/ast_util/AstFeatUtil.js +2 -4
  11. package/dist/cjs/util/ast_util/AstKit.d.ts +30 -0
  12. package/dist/cjs/util/ast_util/AstKit.js +574 -0
  13. package/dist/cjs/util/ast_util/AstUtil.d.ts +7 -7
  14. package/dist/cjs/util/ast_util/AstUtil.js +18 -14
  15. package/dist/cjs/util/ast_util/SHARED_CONSTANTS.d.ts +3 -0
  16. package/dist/cjs/util/ast_util/SHARED_CONSTANTS.js +35 -0
  17. package/dist/cjs/util/constants.d.ts +1 -0
  18. package/dist/cjs/util/constants.js +5 -2
  19. package/dist/cjs/util/{shared/gitUtil.d.ts → git_util/cloneRepoWithBranchAndDefault.d.ts} +1 -5
  20. package/dist/cjs/util/{shared/gitUtil.js → git_util/cloneRepoWithBranchAndDefault.js} +5 -25
  21. package/dist/cjs/util/git_util/execGitDiff.d.ts +4 -0
  22. package/dist/cjs/util/git_util/execGitDiff.js +39 -0
  23. package/dist/cjs/util/git_util/getGitRepositoryAndBranch.d.ts +4 -0
  24. package/dist/cjs/util/{report_util/getFileDepends.js → git_util/getGitRepositoryAndBranch.js} +15 -18
  25. package/dist/cjs/util/{parseGitLabDiffUril.js → git_util/parseGitLabDiffUril.js} +2 -2
  26. package/dist/cjs/util/{shared → project_umi_util}/umi4ProjectUtil.js +1 -1
  27. package/dist/cjs/util/{shared → project_umi_util}/vueProjectUtil.js +1 -1
  28. package/dist/cjs/util/report_util/createDependenceMap.js +3 -3
  29. package/dist/cjs/util/report_util/filterEffectedCode.d.ts +1 -1
  30. package/dist/cjs/util/report_util/filterEffectedExportMember.js +5 -1
  31. package/dist/cjs/util/report_util/generateGitDiffReport.d.ts +1 -1
  32. package/dist/cjs/util/report_util/generateGitDiffReport.js +2 -2
  33. package/dist/cjs/util/{ast_util/filePathResolver.d.ts → report_util/resolveImportPath.d.ts} +1 -1
  34. package/dist/cjs/util/{ast_util/filePathResolver.js → report_util/resolveImportPath.js} +5 -9
  35. package/dist/cjs/util/{createRandomStr.js → shared/createRandomStr.js} +1 -1
  36. package/dist/cjs/util/shared/getRepoSupportFlag.d.ts +1 -3
  37. package/dist/cjs/util/shared/getRepoSupportFlag.js +1 -18
  38. package/dist/cjs/util/shared/gitDiffTool.d.ts +0 -40
  39. package/dist/cjs/util/shared/gitDiffTool.js +7 -116
  40. package/package.json +5 -1
  41. package/dist/cjs/util/madge_util.d.ts +0 -12
  42. package/dist/cjs/util/madge_util.js +0 -120
  43. package/dist/cjs/util/report_util/getFileDepends.d.ts +0 -1
  44. /package/dist/cjs/util/{parseGitLabDiffUril.d.ts → git_util/parseGitLabDiffUril.d.ts} +0 -0
  45. /package/dist/cjs/util/{shared → project_umi_util}/umi4ProjectUtil.d.ts +0 -0
  46. /package/dist/cjs/util/{shared → project_umi_util}/vueProjectUtil.d.ts +0 -0
  47. /package/dist/cjs/util/{createRandomStr.d.ts → shared/createRandomStr.d.ts} +0 -0
@@ -1,9 +1,3 @@
1
- import { cloneGitRepoAndGetDiff } from "./util/shared/gitDiffTool";
2
- export declare const gitDiffFileName = "git_diff.txt";
3
- export declare function getGitRepositoryAndBranch(): Promise<{
4
- gitUrl: string;
5
- branchName: string;
6
- }>;
7
1
  export declare function sameCodeDetect(dirOfCwd?: string): Promise<void>;
8
2
  export declare function gitDiffDetect(): Promise<{
9
3
  dependenceJson: {
@@ -32,45 +26,9 @@ export declare function gitDiffDetect(): Promise<{
32
26
  filePath: string;
33
27
  }[];
34
28
  } | undefined>;
35
- export declare function getUpstreamDependenceJson(inputUrl: string, token: string, jsonKeys?: (keyof Awaited<ReturnType<typeof cloneGitRepoAndGetDiff>>)[]): Promise<{
36
- reports: {
37
- dangerIdentifiers: string[];
38
- topEffectedExportsAdded: string[];
39
- topEffectedExportsRemoved: string[];
40
- blockReports: {
41
- diff_txt: string[];
42
- infos: {
43
- causeBy: string;
44
- effectsUpstream: string[];
45
- occupations: string[];
46
- effectsDownstream: string[];
47
- }[];
48
- }[];
49
- type: "modify" | "add" | "delete";
50
- filesDependsOnMe: string[];
51
- undefinedIdentifiers: string[];
52
- filePath: string;
53
- }[];
54
- dependenceJson: {
55
- import2export: Record<string, string>;
56
- export2export: Record<string, string>;
57
- mapFilePathToExportAllSources: Record<string, string[]>;
58
- indirectExportMembers: Record<string, string>;
59
- };
60
- effectedCode: Record<string, any>;
61
- upstreamDependenceJson: {};
62
- repoType: string;
63
- message: string;
64
- } | {
65
- effectedCode: Record<string, any>;
66
- upstreamDependenceJson: {};
67
- repoType: string;
68
- message: any;
69
- reports?: undefined;
70
- dependenceJson?: undefined;
71
- }>;
72
29
  export { isRepoTypeSupported } from "./util/shared/getRepoSupportFlag";
73
30
  export declare function runDiffDetect(compareUrl?: string, token?: string): Promise<{
31
+ error: Error | null;
74
32
  repoType: string;
75
33
  effectedImportUsage: [string, string][];
76
34
  }>;
package/dist/cjs/index.js CHANGED
@@ -29,10 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // src/index.ts
30
30
  var src_exports = {};
31
31
  __export(src_exports, {
32
- getGitRepositoryAndBranch: () => getGitRepositoryAndBranch,
33
- getUpstreamDependenceJson: () => getUpstreamDependenceJson,
34
32
  gitDiffDetect: () => gitDiffDetect,
35
- gitDiffFileName: () => gitDiffFileName,
36
33
  isRepoTypeSupported: () => import_getRepoSupportFlag.isRepoTypeSupported,
37
34
  runDiffDetect: () => runDiffDetect,
38
35
  sameCodeDetect: () => sameCodeDetect
@@ -40,25 +37,13 @@ __export(src_exports, {
40
37
  module.exports = __toCommonJS(src_exports);
41
38
  var import_path = require("path");
42
39
  var import_fs = require("fs");
43
- var import_utils = require("@umijs/utils");
44
40
  var import_dayjs = __toESM(require("dayjs"));
45
41
  var import_readDirFiles = require("./util/shared/readDirFiles");
46
42
  var import_Core = __toESM(require("./util/ast_util/Core"));
47
- var import_parseGitLabDiffUril = require("./util/parseGitLabDiffUril");
48
43
  var import_gitDiffTool = require("./util/shared/gitDiffTool");
49
44
  var import_DetectService = require("./services/DetectService");
45
+ var import_getGitRepositoryAndBranch = __toESM(require("./util/git_util/getGitRepositoryAndBranch"));
50
46
  var import_getRepoSupportFlag = require("./util/shared/getRepoSupportFlag");
51
- var gitDiffFileName = "git_diff.txt";
52
- async function getGitRepositoryAndBranch() {
53
- const res = await import_utils.execa.execa("git remote get-url origin", { shell: true });
54
- import_utils.chalk.green(["仓库地址:", res.stdout]);
55
- const branch = await import_utils.execa.execa("git rev-parse --abbrev-ref HEAD", { shell: true });
56
- import_utils.chalk.green(["分支名称:", branch.stdout]);
57
- return {
58
- gitUrl: res.stdout,
59
- branchName: branch.stdout
60
- };
61
- }
62
47
  async function sameCodeDetect(dirOfCwd) {
63
48
  const filesAndContent = await (0, import_readDirFiles.readSrcFiles)(dirOfCwd);
64
49
  const { nodeContentGroupList, depthList } = import_Core.default.investigate(filesAndContent);
@@ -69,14 +54,10 @@ async function sameCodeDetect(dirOfCwd) {
69
54
  (0, import_fs.writeFileSync)((0, import_path.join)(process.cwd(), `${(0, import_dayjs.default)().format("YYYYMDD_HHmm")}_same_code.md`), md, { encoding: "utf-8", flag: "w" });
70
55
  }
71
56
  async function gitDiffDetect() {
72
- const { gitUrl, branchName } = await getGitRepositoryAndBranch();
57
+ const { gitUrl, branchName } = await (0, import_getGitRepositoryAndBranch.default)();
73
58
  const today = (0, import_dayjs.default)().format("YYYYMDD_HHmmss");
74
59
  return (0, import_gitDiffTool.gitDiffTool)({ gitRepoUrl: gitUrl, targetBranch: branchName, baseBranch: "master", tempDirPath: today, generateFile: void 0 });
75
60
  }
76
- async function getUpstreamDependenceJson(inputUrl, token, jsonKeys) {
77
- const gitInfo = (0, import_parseGitLabDiffUril.parseGitLabCompareUrl)(inputUrl);
78
- return (0, import_gitDiffTool.cloneGitRepoAndGetDiff)(gitInfo.gitRepoUrl, gitInfo.targetBranch, { token, jsonKeys });
79
- }
80
61
  async function runDiffDetect(compareUrl, token) {
81
62
  const option = {
82
63
  compareUrl,
@@ -85,7 +66,7 @@ async function runDiffDetect(compareUrl, token) {
85
66
  branchName: ""
86
67
  };
87
68
  if (!compareUrl || !token) {
88
- const { gitUrl, branchName } = await getGitRepositoryAndBranch();
69
+ const { gitUrl, branchName } = await (0, import_getGitRepositoryAndBranch.default)();
89
70
  option.gitRepoUrl = gitUrl;
90
71
  option.branchName = branchName;
91
72
  }
@@ -95,10 +76,7 @@ async function runDiffDetect(compareUrl, token) {
95
76
  }
96
77
  // Annotate the CommonJS export names for ESM import in node:
97
78
  0 && (module.exports = {
98
- getGitRepositoryAndBranch,
99
- getUpstreamDependenceJson,
100
79
  gitDiffDetect,
101
- gitDiffFileName,
102
80
  isRepoTypeSupported,
103
81
  runDiffDetect,
104
82
  sameCodeDetect
@@ -1,4 +1,4 @@
1
- import { parseGitLabCompareUrl } from "../util/parseGitLabDiffUril";
1
+ import { parseGitLabCompareUrl } from "../util/git_util/parseGitLabDiffUril";
2
2
  import { ProjectService } from "./ProjectService";
3
3
  export declare class DetectService {
4
4
  directoryInfo: {
@@ -27,8 +27,10 @@ export declare class DetectService {
27
27
  clean(): Promise<void>;
28
28
  mkdir(): Promise<void>;
29
29
  clone(): Promise<void>;
30
+ move(): Promise<void>;
30
31
  projectHandle(): Promise<void>;
31
32
  formatResult(): {
33
+ error: Error | null;
32
34
  repoType: string;
33
35
  effectedImportUsage: [string, string][];
34
36
  };
@@ -35,13 +35,15 @@ module.exports = __toCommonJS(DetectService_exports);
35
35
  var import_path = require("path");
36
36
  var import_await_to_js = __toESM(require("await-to-js"));
37
37
  var import_utils = require("@umijs/utils");
38
- var import_parseGitLabDiffUril = require("../util/parseGitLabDiffUril");
39
- var import_createRandomStr = __toESM(require("../util/createRandomStr"));
38
+ var import_parseGitLabDiffUril = require("../util/git_util/parseGitLabDiffUril");
39
+ var import_createRandomStr = __toESM(require("../util/shared/createRandomStr"));
40
40
  var import_constants = require("../util/constants");
41
- var import_gitUtil = require("../util/shared/gitUtil");
41
+ var import_execGitDiff = __toESM(require("../util/git_util/execGitDiff"));
42
42
  var import_getRepoSupportFlag = require("../util/shared/getRepoSupportFlag");
43
43
  var import_UmiProjectService = __toESM(require("./projectServiceClass/UmiProjectService"));
44
44
  var import_perf_hooks = require("perf_hooks");
45
+ var import_cloneRepoWithBranchAndDefault = __toESM(require("../util/git_util/cloneRepoWithBranchAndDefault"));
46
+ var import_VueProjectService = __toESM(require("./projectServiceClass/VueProjectService"));
45
47
  var DetectService = class {
46
48
  constructor(option) {
47
49
  this.directoryInfo = {
@@ -61,7 +63,7 @@ var DetectService = class {
61
63
  this.init(option);
62
64
  }
63
65
  async init(option) {
64
- const { compareUrl, token, gitRepoUrl, branchName } = option;
66
+ const { compareUrl, token = "", gitRepoUrl, branchName } = option;
65
67
  const tmpWorkDir = (0, import_createRandomStr.default)();
66
68
  this.directoryInfo = {
67
69
  tmpWorkDir,
@@ -72,6 +74,7 @@ var DetectService = class {
72
74
  this.gitInfo = {
73
75
  ...this.gitInfo,
74
76
  ...(0, import_parseGitLabDiffUril.parseGitLabCompareUrl)(compareUrl),
77
+ token,
75
78
  repoType: ""
76
79
  };
77
80
  }
@@ -97,6 +100,10 @@ var DetectService = class {
97
100
  import_utils.logger.info(`mkdir 耗时: ${duration}ms`);
98
101
  }
99
102
  await this.clone();
103
+ const token = this.gitInfo.token;
104
+ if (token) {
105
+ await this.move();
106
+ }
100
107
  import_perf_hooks.performance.mark("projectHandle");
101
108
  {
102
109
  const duration = import_perf_hooks.performance.measure("clone", "clone", "projectHandle").duration;
@@ -130,7 +137,10 @@ var DetectService = class {
130
137
  import_utils.logger.info("开始克隆仓库");
131
138
  const { tmpWorkDir } = this.directoryInfo;
132
139
  const { gitRepoUrl, token, baseBranch, targetBranch } = this.gitInfo;
140
+ import_utils.logger.info(`开始克隆仓库: ${gitRepoUrl}`);
141
+ import_utils.logger.info(`token: ${token}`);
133
142
  const repoUrl = token ? (0, import_parseGitLabDiffUril.getGitRepoUrlByToken)(gitRepoUrl, token || "") : gitRepoUrl;
143
+ import_utils.logger.info(`克隆仓库URL: ${repoUrl}`);
134
144
  const branchesAndTargetDirPaths = [
135
145
  {
136
146
  branch: baseBranch,
@@ -142,14 +152,23 @@ var DetectService = class {
142
152
  }
143
153
  ];
144
154
  await Promise.all(branchesAndTargetDirPaths.map(({ branch, targetDirPath: targetDirPath2 }) => {
145
- return (0, import_gitUtil.cloneRepoWithBranchAndDefault)(repoUrl, branch, targetDirPath2);
155
+ return (0, import_cloneRepoWithBranchAndDefault.default)(repoUrl, branch, targetDirPath2);
146
156
  }));
147
157
  const targetDirPath = (0, import_path.join)(tmpWorkDir, import_constants.TARGET);
148
- await (0, import_gitUtil.execGitDiff)(targetDirPath, baseBranch, targetBranch);
158
+ await (0, import_execGitDiff.default)(targetDirPath, baseBranch, targetBranch);
149
159
  const repoType = await (0, import_getRepoSupportFlag.getRepoType)((0, import_path.join)(targetDirPath, "package.json"));
150
160
  this.gitInfo.repoType = repoType;
151
161
  import_utils.logger.info("克隆仓库成功");
152
162
  }
163
+ async move() {
164
+ const repoType = this.gitInfo.repoType;
165
+ const tmpWorkDir = this.directoryInfo.tmpWorkDir;
166
+ const newTmpWorkDir = (0, import_path.join)(repoType, tmpWorkDir);
167
+ this.directoryInfo.tmpWorkDir = newTmpWorkDir;
168
+ this.directoryInfo.sourceBranchDir = (0, import_path.join)(newTmpWorkDir, import_constants.SOURCE);
169
+ this.directoryInfo.targetBranchDir = (0, import_path.join)(newTmpWorkDir, import_constants.TARGET);
170
+ await import_utils.execa.execa(`mv ${tmpWorkDir} ${newTmpWorkDir}`, { shell: true });
171
+ }
153
172
  async projectHandle() {
154
173
  var _a;
155
174
  const { repoType } = this.gitInfo;
@@ -159,13 +178,17 @@ var DetectService = class {
159
178
  if (repoType === "umi") {
160
179
  this.projectService = new import_UmiProjectService.default(this);
161
180
  }
181
+ if (repoType === "vue2") {
182
+ this.projectService = new import_VueProjectService.default(this);
183
+ }
162
184
  await ((_a = this.projectService) == null ? void 0 : _a.run());
163
185
  }
164
186
  formatResult() {
165
187
  var _a;
166
188
  const repoType = this.gitInfo.repoType;
167
- const { effectedImportUsage } = ((_a = this.projectService) == null ? void 0 : _a.outputResult) || { effectedImportUsage: [] };
189
+ const { effectedImportUsage, error } = ((_a = this.projectService) == null ? void 0 : _a.outputResult) || { effectedImportUsage: [], error: null };
168
190
  return {
191
+ error,
169
192
  repoType,
170
193
  effectedImportUsage
171
194
  };
@@ -1,16 +1,15 @@
1
1
  import { DetectService } from "./DetectService";
2
2
  import { formatGitDiffContent } from "../util/format_git_diff_content";
3
- import { IMadgeInstance } from "../util/report_util/getMadgeInstance";
4
3
  export interface ProjectService {
5
4
  detectService: DetectService;
6
5
  gitDiffDetail: ReturnType<typeof formatGitDiffContent>;
7
6
  helpInfo: {
8
7
  projectFileList: string[];
9
- madgeResult: IMadgeInstance | null;
10
8
  parsedAlias: Record<string, any>;
11
9
  };
12
10
  outputResult: {
13
11
  effectedImportUsage: [string, string][];
12
+ error: Error | null;
14
13
  };
15
14
  run(): void;
16
15
  }
@@ -1,11 +1,15 @@
1
1
  import { GitDiffDetail } from "../../util/format_git_diff_content";
2
2
  import { DetectService } from "../DetectService";
3
3
  import { ProjectService } from "../ProjectService";
4
+ import { IMadgeInstance } from "../../util/report_util/getMadgeInstance";
4
5
  export default class UmiProjectService implements ProjectService {
5
6
  detectService: DetectService;
6
7
  gitDiffDetail: GitDiffDetail[];
7
8
  helpInfo: ProjectService['helpInfo'];
8
9
  outputResult: ProjectService['outputResult'];
10
+ umiHelpInfo: {
11
+ madgeResult: IMadgeInstance | null;
12
+ };
9
13
  constructor(detectService: DetectService);
10
14
  run(): Promise<void>;
11
15
  parseGitDiffContent(): Promise<void>;
@@ -33,24 +33,27 @@ __export(UmiProjectService_exports, {
33
33
  });
34
34
  module.exports = __toCommonJS(UmiProjectService_exports);
35
35
  var import_format_git_diff_content = require("../../util/format_git_diff_content");
36
- var import_umi4ProjectUtil = require("../../util/shared/umi4ProjectUtil");
36
+ var import_umi4ProjectUtil = require("../../util/project_umi_util/umi4ProjectUtil");
37
37
  var import_fs = require("fs");
38
38
  var import_path = require("path");
39
- var import__ = require("../../index");
40
39
  var import_collectUpstreamFiles = __toESM(require("../../util/shared/collectUpstreamFiles"));
41
40
  var import_createDependenceMap = require("../../util/report_util/createDependenceMap");
42
41
  var import_filterEffectedExportMember = __toESM(require("../../util/report_util/filterEffectedExportMember"));
42
+ var import_constants = require("../../util/constants");
43
43
  var UmiProjectService = class {
44
44
  constructor(detectService) {
45
45
  this.detectService = detectService;
46
46
  this.gitDiffDetail = [];
47
47
  this.helpInfo = {
48
48
  projectFileList: [],
49
- madgeResult: null,
50
49
  parsedAlias: {}
51
50
  };
52
51
  this.outputResult = {
53
- effectedImportUsage: []
52
+ effectedImportUsage: [],
53
+ error: null
54
+ };
55
+ this.umiHelpInfo = {
56
+ madgeResult: null
54
57
  };
55
58
  this.detectService = detectService;
56
59
  }
@@ -58,18 +61,19 @@ var UmiProjectService = class {
58
61
  const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
59
62
  const { madgeResult, parsedAlias } = await (0, import_umi4ProjectUtil.umi4SetUp)({ targetDirPath, invokeType: "remote" });
60
63
  this.helpInfo.projectFileList = Object.keys(madgeResult.tree);
61
- this.helpInfo.madgeResult = madgeResult;
64
+ this.umiHelpInfo.madgeResult = madgeResult;
62
65
  this.helpInfo.parsedAlias = parsedAlias;
63
66
  await this.parseGitDiffContent();
64
67
  await this.collectEffectedFiles();
65
68
  }
66
69
  async parseGitDiffContent() {
67
70
  const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
68
- const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import__.gitDiffFileName), "utf-8");
71
+ const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import_constants.gitDiffFileName), "utf-8");
69
72
  this.gitDiffDetail = (0, import_format_git_diff_content.formatGitDiffContent)(diff_txt);
70
73
  }
71
74
  async collectEffectedFiles() {
72
- const { madgeResult, parsedAlias } = this.helpInfo;
75
+ const { parsedAlias } = this.helpInfo;
76
+ const { madgeResult } = this.umiHelpInfo;
73
77
  const projectFileList = this.helpInfo.projectFileList;
74
78
  const targetBranchDir = this.detectService.directoryInfo.targetBranchDir;
75
79
  const absPathPrefix = (0, import_path.join)(targetBranchDir, "/");
@@ -0,0 +1,31 @@
1
+ import { GitDiffDetail } from "../../util/format_git_diff_content";
2
+ import { ProjectService } from "../ProjectService";
3
+ import { DetectService } from "../DetectService";
4
+ import { IMadgeInstance } from "../../util/report_util/getMadgeInstance";
5
+ export default class VueProjectService implements ProjectService {
6
+ detectService: DetectService;
7
+ gitDiffDetail: GitDiffDetail[];
8
+ helpInfo: ProjectService['helpInfo'];
9
+ outputResult: ProjectService['outputResult'];
10
+ vueHelpInfo: {
11
+ madgeResult: IMadgeInstance | null;
12
+ webpackConfigPath: string;
13
+ webpackConfigTextLines: string[];
14
+ entryFiles: string[];
15
+ };
16
+ constructor(detectService: DetectService);
17
+ run(): Promise<void>;
18
+ readSrcDirFiles(): Promise<void>;
19
+ parseGitDiffContent(): Promise<void>;
20
+ handleWebpackConfigText(webpackConfigText: string): Promise<void>;
21
+ getWebpackConfigText(): Promise<void>;
22
+ createSimpleWebpackConfig(): {
23
+ resolve: {
24
+ alias: Record<string, any>;
25
+ };
26
+ };
27
+ getEntryFiles(): void;
28
+ getWebpackAlias(): void;
29
+ getMadgeResult(): Promise<void>;
30
+ collectEffectedFiles(): Promise<void>;
31
+ }
@@ -0,0 +1,191 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to2, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to2, key) && key !== except)
15
+ __defProp(to2, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to2;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/services/projectServiceClass/VueProjectService.ts
30
+ var VueProjectService_exports = {};
31
+ __export(VueProjectService_exports, {
32
+ default: () => VueProjectService
33
+ });
34
+ module.exports = __toCommonJS(VueProjectService_exports);
35
+ var import_format_git_diff_content = require("../../util/format_git_diff_content");
36
+ var import_fs = require("fs");
37
+ var import_path = require("path");
38
+ var import_constants = require("../../util/constants");
39
+ var import_utils = require("@umijs/utils");
40
+ var import_madge = __toESM(require("madge"));
41
+ var import_collectUpstreamFiles = __toESM(require("../../util/shared/collectUpstreamFiles"));
42
+ var import_createDependenceMap = require("../../util/report_util/createDependenceMap");
43
+ var import_filterEffectedExportMember = __toESM(require("../../util/report_util/filterEffectedExportMember"));
44
+ var import_await_to_js = __toESM(require("await-to-js"));
45
+ var VueProjectService = class {
46
+ constructor(detectService) {
47
+ this.detectService = detectService;
48
+ this.gitDiffDetail = [];
49
+ this.helpInfo = {
50
+ projectFileList: [],
51
+ parsedAlias: {}
52
+ };
53
+ this.outputResult = {
54
+ effectedImportUsage: [],
55
+ error: null
56
+ };
57
+ this.vueHelpInfo = {
58
+ madgeResult: null,
59
+ webpackConfigPath: "",
60
+ webpackConfigTextLines: [],
61
+ entryFiles: []
62
+ };
63
+ this.detectService = detectService;
64
+ }
65
+ async run() {
66
+ import_utils.logger.info("格式化 git diff 内容...");
67
+ await this.parseGitDiffContent();
68
+ import_utils.logger.info("获取 webpack 配置...");
69
+ await this.getWebpackConfigText();
70
+ await this.collectEffectedFiles();
71
+ }
72
+ async readSrcDirFiles() {
73
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
74
+ const projectFileList = (0, import_utils.readDirFiles)({ dir: (0, import_path.join)(targetDirPath, "src") });
75
+ }
76
+ async parseGitDiffContent() {
77
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
78
+ const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(targetDirPath, import_constants.gitDiffFileName), "utf-8");
79
+ this.gitDiffDetail = (0, import_format_git_diff_content.formatGitDiffContent)(diff_txt);
80
+ }
81
+ async handleWebpackConfigText(webpackConfigText) {
82
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
83
+ this.vueHelpInfo.webpackConfigTextLines = webpackConfigText.split("\n");
84
+ this.getWebpackAlias();
85
+ this.getEntryFiles();
86
+ const wb = this.createSimpleWebpackConfig();
87
+ (0, import_fs.writeFileSync)(this.vueHelpInfo.webpackConfigPath = (0, import_path.join)(targetDirPath, "webpack.config.js"), `module.exports = ${JSON.stringify(wb, null, 2)}`, "utf-8");
88
+ await this.getMadgeResult();
89
+ }
90
+ async getWebpackConfigText() {
91
+ const targetDirPath = this.detectService.directoryInfo.targetBranchDir;
92
+ let [err, data] = await (0, import_await_to_js.default)(import_utils.execa.execa(`cd ${targetDirPath} && npx vue-cli-service inspect`, { shell: true }));
93
+ if (err) {
94
+ await import_utils.execa.execa(`cd ${targetDirPath} && yarn`, { shell: true });
95
+ [err, data] = await (0, import_await_to_js.default)(import_utils.execa.execa(`cd ${targetDirPath} && npx vue-cli-service inspect`, { shell: true }));
96
+ }
97
+ this.outputResult.error = err;
98
+ const webpackConfigText = data == null ? void 0 : data.stdout;
99
+ if (webpackConfigText) {
100
+ await this.handleWebpackConfigText(webpackConfigText);
101
+ }
102
+ }
103
+ createSimpleWebpackConfig() {
104
+ return {
105
+ resolve: {
106
+ alias: this.helpInfo.parsedAlias
107
+ }
108
+ };
109
+ }
110
+ getEntryFiles() {
111
+ const lines = this.vueHelpInfo.webpackConfigTextLines;
112
+ let entryObject = {};
113
+ const entryLines = [];
114
+ let entryFind = false;
115
+ for (const line of lines) {
116
+ if (!entryFind && line.trim().startsWith("entry:")) {
117
+ entryLines.push(line.replace("entry:", ""));
118
+ entryFind = true;
119
+ } else if (entryFind && line.trim().startsWith("}")) {
120
+ entryLines.push(line.replace(/,/g, ""));
121
+ break;
122
+ } else if (entryFind) {
123
+ entryLines.push(line);
124
+ }
125
+ }
126
+ const entryLinesJoin = entryLines.join("\n");
127
+ entryObject = eval(`(${entryLinesJoin})`);
128
+ this.vueHelpInfo.entryFiles = Object.values({ ...entryObject }).flat();
129
+ }
130
+ getWebpackAlias() {
131
+ const lines = this.vueHelpInfo.webpackConfigTextLines;
132
+ const aliasLines = [];
133
+ let aliasFind = false;
134
+ for (const line of lines) {
135
+ if (!aliasFind && line.trim().startsWith("alias:")) {
136
+ aliasLines.push(line.replace("alias:", ""));
137
+ aliasFind = true;
138
+ } else if (aliasFind && line.trim().startsWith("}")) {
139
+ aliasLines.push(line.replace(/,/g, ""));
140
+ break;
141
+ } else if (aliasFind) {
142
+ aliasLines.push(line);
143
+ }
144
+ }
145
+ const userAlias = eval(`(${aliasLines.join("\n")})`);
146
+ this.helpInfo.parsedAlias = userAlias;
147
+ }
148
+ async getMadgeResult() {
149
+ const targetBranchDir = this.detectService.directoryInfo.targetBranchDir;
150
+ const entryFilePath = this.vueHelpInfo.entryFiles.map((file) => (0, import_path.join)(targetBranchDir, file));
151
+ const madgeConfig = {
152
+ baseDir: this.detectService.directoryInfo.targetBranchDir,
153
+ fileExtensions: ["js", "ts", "jsx", "tsx", "vue"],
154
+ webpackConfig: this.vueHelpInfo.webpackConfigPath
155
+ };
156
+ const res = await (0, import_madge.default)(entryFilePath, madgeConfig);
157
+ this.vueHelpInfo.madgeResult = res;
158
+ this.helpInfo.projectFileList = Object.keys(res.tree);
159
+ }
160
+ async collectEffectedFiles() {
161
+ const { parsedAlias } = this.helpInfo;
162
+ const { madgeResult } = this.vueHelpInfo;
163
+ const projectFileList = this.helpInfo.projectFileList;
164
+ const targetBranchDir = this.detectService.directoryInfo.targetBranchDir;
165
+ const absPathPrefix = (0, import_path.join)(targetBranchDir, "/");
166
+ const validModifiedFiles = this.gitDiffDetail.reduce((acc, item) => {
167
+ const { filePath } = item;
168
+ this.helpInfo.projectFileList.includes(filePath) && acc.push(filePath);
169
+ return acc;
170
+ }, []);
171
+ const possibleEffectedFiles = (0, import_collectUpstreamFiles.default)(madgeResult, validModifiedFiles);
172
+ const possibleEffectedFilesFullPath = possibleEffectedFiles.map((file) => (0, import_path.join)(absPathPrefix, file));
173
+ const { import2export, indirectExportMembers } = (0, import_createDependenceMap.createExportedNameToReferenceLocalSet)(possibleEffectedFilesFullPath, parsedAlias, absPathPrefix, projectFileList);
174
+ const gitDiffDetail = this.gitDiffDetail;
175
+ const validGitDiffDetail = gitDiffDetail.filter((item) => possibleEffectedFiles.includes(item.filePath));
176
+ const effectedExportNames = validGitDiffDetail.map((item) => {
177
+ const { filePath, newBranchLineScope, startLineOfNew } = item;
178
+ const exportedNames = (0, import_filterEffectedExportMember.default)((0, import_path.join)(absPathPrefix, filePath), absPathPrefix, Number(startLineOfNew), Number(startLineOfNew) + Number(newBranchLineScope));
179
+ return exportedNames.map((name) => [filePath, name].join("#"));
180
+ }).flat();
181
+ const effectedImportUsage = [...Object.entries(import2export), ...Object.entries(indirectExportMembers)].filter(([_, value]) => {
182
+ return effectedExportNames.includes(value);
183
+ });
184
+ this.outputResult.effectedImportUsage = effectedImportUsage;
185
+ const token = this.detectService.gitInfo.token;
186
+ if (!token) {
187
+ const pwd = (0, import_path.join)(this.detectService.directoryInfo.tmpWorkDir, "..");
188
+ (0, import_fs.writeFileSync)((0, import_path.join)(pwd, "effectedImportUsage.json"), JSON.stringify(effectedImportUsage, null, 2));
189
+ }
190
+ }
191
+ };
@@ -37,6 +37,7 @@ var import_sha1 = __toESM(require("crypto-js/sha1"));
37
37
  var import_enc_base64 = __toESM(require("crypto-js/enc-base64"));
38
38
  var import_featureLevel = require("../shared/featureLevel");
39
39
  var import_astNodeFeatureExtractorMap = require("../shared/astNodeFeatureExtractorMap");
40
+ var import_SHARED_CONSTANTS = require("./SHARED_CONSTANTS");
40
41
  var AstFeatUtil = class {
41
42
  // 单节点判断
42
43
  static isValidNodeCollect(astNode) {
@@ -220,10 +221,7 @@ var AstFeatUtil = class {
220
221
  }
221
222
  };
222
223
  AstFeatUtil.skipHashCreateTypes = ["Program", "File", "ExportDeclaration"];
223
- AstFeatUtil.invalidNodeKey = [
224
- "comments",
225
- "tokens"
226
- ];
224
+ AstFeatUtil.invalidNodeKey = import_SHARED_CONSTANTS.INVALID_NODE_KEY;
227
225
  // 忽略的 节点类型
228
226
  AstFeatUtil.AstNodeTypeConf = /* @__PURE__ */ new Map([
229
227
  ["ImportDeclaration", {
@@ -0,0 +1,30 @@
1
+ import { AstNode } from "./AstUtil";
2
+ export declare class AstUtil {
3
+ static invalidNodeKey: string[];
4
+ static EXPORT_DECLARATION_TYPES: readonly ["FunctionDeclaration", "TSEnumDeclaration", "TSInterfaceDeclaration", "TSTypeAliasDeclaration", "ClassDeclaration"];
5
+ static windowProperties: string[];
6
+ static intrinsicElements: string[];
7
+ static standardAttributes: string[];
8
+ static FUNCTION_TYPES: string[];
9
+ static getNodePath(node: AstNode): string;
10
+ static getShortNodeMsg(node: AstNode, hideParentProperty?: boolean): string;
11
+ static getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet: Map<number, Set<AstNode>>, lineNumberStart: number, lineNumberEnd: number, loose?: boolean): AstNode[];
12
+ private static isValidNodeCollect;
13
+ private static isValidArrayNodeCollect;
14
+ private static collectEffectId;
15
+ private static findOrCreateImportedMember;
16
+ private static findOrCreateExportedMember;
17
+ private static updateImportedAndExportedMember;
18
+ private static deepSearchParamsIdentifier;
19
+ private static collectDependenceIdsOfExportMembers;
20
+ private static collectExpressionIdentifiersShallow;
21
+ private static _collectExpressionIdentifiersShallow;
22
+ private static collectDependenceIds;
23
+ private static updateLoc;
24
+ private static collectVariableDeclarationIdentifiers;
25
+ private static collectDeclarationIdentifiers;
26
+ private static collectImportDeclarationIdentifiers;
27
+ private static collectHoldingIds;
28
+ static deepFirstTravel(node: AstNode, filePath: string, mapUuidToNode: Map<string, AstNode>, mapFileLineToNodeSet: Map<number, Set<AstNode>>, mapPathToNodeSet: Map<string, Set<AstNode>>, stopTravelCallback?: (node: AstNode) => boolean | void): AstNode | undefined;
29
+ private static _deepFirstTravel;
30
+ }