js-code-detector 0.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-present John
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # code-detector
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/code-detector.svg?style=flat)](https://npmjs.com/package/code-detector)
4
+ [![NPM downloads](http://img.shields.io/npm/dm/code-detector.svg?style=flat)](https://npmjs.com/package/code-detector)
5
+
6
+ ## Install
7
+
8
+ ```bash
9
+ $ yarn install
10
+ ```
11
+
12
+ ```bash
13
+ $ npm run dev
14
+ $ npm run build
15
+ ```
16
+
17
+ ## Options
18
+
19
+ TODO
20
+
21
+ ## LICENSE
22
+
23
+ MIT
package/bin/detect.js ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+
3
+ async function main() {
4
+ const { gitUrl, branchName } = await require('../dist/cjs/index').getGitRepositoryAndBranch();
5
+ require('../dist/cjs/index').writeGitDiffTxt(gitUrl, branchName);
6
+ }
7
+ main();
@@ -0,0 +1,6 @@
1
+ export declare function umiPluginCallback(api: any): Promise<void>;
2
+ export declare function writeGitDiffTxt(gitUrl: string, branchName: string): Promise<string>;
3
+ export declare function getGitRepositoryAndBranch(): Promise<{
4
+ gitUrl: string;
5
+ branchName: string;
6
+ }>;
@@ -0,0 +1,110 @@
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 = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
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/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ getGitRepositoryAndBranch: () => getGitRepositoryAndBranch,
33
+ umiPluginCallback: () => umiPluginCallback,
34
+ writeGitDiffTxt: () => writeGitDiffTxt
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+ var import_fs = require("fs");
38
+ var import_path = require("path");
39
+ var import_format_git_diff_content = require("./util/format_git_diff_content");
40
+ var import_madge_util = __toESM(require("./util/madge_util"));
41
+ var import_fs2 = require("fs");
42
+ var import_report_util = require("./util/report_util");
43
+ var import_utils = require("@umijs/utils");
44
+ var import_dayjs = __toESM(require("dayjs"));
45
+ var jsonName = "git_diff_report.json";
46
+ async function umiPluginCallback(api) {
47
+ const diff_txt = (0, import_fs.readFileSync)((0, import_path.join)(api.cwd, "git_diff.txt"), "utf-8");
48
+ const gitDiffDetail = (0, import_format_git_diff_content.formatGitDiffContent)(diff_txt);
49
+ const madgeUtil = await (0, import_madge_util.default)(api);
50
+ if (!madgeUtil) {
51
+ return;
52
+ }
53
+ const { madgeInstance: { tree }, usingFiles } = madgeUtil;
54
+ const cwd = api.cwd;
55
+ const absPathPrefix = cwd + "/";
56
+ const usingFileNoPrefix = usingFiles.map((item) => item.filePath.replace(absPathPrefix, ""));
57
+ const groupGitDiffLines = gitDiffDetail.filter((item) => usingFileNoPrefix.includes(item.filePath));
58
+ const reports = (0, import_report_util.createDetectReport)({ groupGitDiffLines, tree, absPathPrefix });
59
+ (0, import_fs2.writeFileSync)((0, import_path.join)(api.cwd, jsonName), JSON.stringify(reports, null, 2), { encoding: "utf-8", flag: "w" });
60
+ }
61
+ var shellFileContent = `#!/bin/sh
62
+ time=$(date "+%Y%-m%d")
63
+ mkdir -p $time
64
+ cd $time
65
+
66
+ git clone $1 target
67
+ cd target
68
+ yarn install
69
+
70
+ git fetch origin $2:$2
71
+ git checkout $2
72
+
73
+ git diff master..$2 --unified=0 --output=git_diff.txt
74
+ cd ..
75
+ git clone $1 source
76
+ `;
77
+ var pluginFileContent = `import * as cb from "../../dist/cjs/index.js"
78
+
79
+ export default async (api) => {
80
+ const buildCallback = () => cb.umiPluginCallback(api);
81
+ api.onBuildComplete(buildCallback);
82
+ }`;
83
+ async function writeGitDiffTxt(gitUrl, branchName) {
84
+ const today = (0, import_dayjs.default)().format("YYYYMDD");
85
+ (0, import_fs2.writeFileSync)((0, import_path.join)(process.cwd(), "detect.sh"), shellFileContent, { encoding: "utf-8", flag: "w" });
86
+ import_utils.execa.execa("chmod +x detect.sh", { shell: "/bin/bash" });
87
+ const res0 = await import_utils.execa.execa("sh detect.sh", [gitUrl, branchName], { shell: "/bin/bash" });
88
+ import_utils.chalk.green(["临时文件夹建立,源代码clone完成"]);
89
+ (0, import_fs2.writeFileSync)((0, import_path.join)(process.cwd(), today, "target", "plugin.ts"), pluginFileContent, { encoding: "utf-8", flag: "w" });
90
+ import_utils.chalk.green(["临时文件写入完成"]);
91
+ await import_utils.execa.execa(`cd ${today}/target && npm run build`, { shell: "/bin/bash" });
92
+ import_utils.chalk.green(["临时入口文件生成"]);
93
+ return (0, import_fs.readFileSync)((0, import_path.join)(process.cwd(), today, "target", jsonName), "utf-8");
94
+ }
95
+ async function getGitRepositoryAndBranch() {
96
+ const res = await import_utils.execa.execa("git remote get-url origin", { shell: "/bin/bash" });
97
+ import_utils.chalk.green(["仓库地址:", res.stdout]);
98
+ const branch = await import_utils.execa.execa("git rev-parse --abbrev-ref HEAD", { shell: "/bin/bash" });
99
+ import_utils.chalk.green(["分支名称:", branch.stdout]);
100
+ return {
101
+ gitUrl: res.stdout,
102
+ branchName: branch.stdout
103
+ };
104
+ }
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ getGitRepositoryAndBranch,
108
+ umiPluginCallback,
109
+ writeGitDiffTxt
110
+ });
@@ -0,0 +1,15 @@
1
+ #!/bin/sh
2
+ time=$(date "+%Y%-m%d%H%M%S")
3
+ mkdir -p $time
4
+ cd $time
5
+
6
+ git clone $1 target
7
+ cd target
8
+ yarn install
9
+
10
+ git fetch origin $2:$2
11
+ git checkout $2
12
+
13
+ git diff master..$2 --unified=0 --output=git_diff.txt
14
+ cd ..
15
+ git clone $1 source
File without changes
File without changes
@@ -0,0 +1,100 @@
1
+ export interface AstNode {
2
+ type: string;
3
+ name?: string;
4
+ start?: number | null;
5
+ end?: number | null;
6
+ loc?: Record<'start' | 'end', {
7
+ line: number;
8
+ column: number;
9
+ }> | null;
10
+ range?: [number, number];
11
+ extra?: Record<string, unknown>;
12
+ _util: {
13
+ startLine: number;
14
+ endLine: number;
15
+ startColumn: number;
16
+ endColumn: number;
17
+ filePath: string;
18
+ parent: AstNode | null;
19
+ parentProperty: string;
20
+ indexOfProperty: number | null;
21
+ ancestors: AstNode[];
22
+ nodeCollection: AstNode[];
23
+ children: AstNode[];
24
+ uuid: string;
25
+ variableScope: AstNode[];
26
+ dependenceIds: Set<AstNode>;
27
+ holdingIds: Set<AstNode>;
28
+ holdingIdNameMap: Map<string, Set<AstNode>>;
29
+ inject: Set<AstNode>;
30
+ provide: Set<AstNode>;
31
+ effectIds: Set<AstNode>;
32
+ crossScope: Set<AstNode>;
33
+ };
34
+ }
35
+ export default class AstUtil {
36
+ static invalidNodeKey: string[];
37
+ static getNodePath(node: AstNode): string;
38
+ static deepFirstTravel(node: AstNode, filePath: string, mapUuidToNode: Map<string, AstNode>, mapFileLineToNodeSet: Map<number, Set<AstNode>>): AstNode | undefined;
39
+ static _deepFirstTravel(node: AstNode, visitedNodeSet: Set<typeof node>, extra: {
40
+ filePath: string;
41
+ depth: number;
42
+ mapUuidToNode: Map<string, AstNode>;
43
+ mapFileLineToNodeSet: Map<number, Set<AstNode>>;
44
+ }): AstNode;
45
+ static collectInjectProvide(ast: AstNode): void;
46
+ static collectHoldingIds(node: AstNode & {
47
+ body: AstNode | null;
48
+ }): void;
49
+ static isVarInit(node: AstNode): boolean;
50
+ static isReturnArgument(node: AstNode): boolean;
51
+ static collectDependenceIds(node: AstNode): void;
52
+ static markDependenceIdAsCrossScope(node: AstNode, scopeId: AstNode): void;
53
+ static isUseStateVarDec(node: AstNode): node is (AstNode & {
54
+ type: "VariableDeclarator";
55
+ id: {
56
+ type: "ArrayPattern";
57
+ elements: (null | AstNode)[];
58
+ };
59
+ init: {
60
+ type: "CallExpression";
61
+ callee: AstNode;
62
+ arguments: [AstNode] | [];
63
+ };
64
+ });
65
+ static isUseMemoVarDec(node: AstNode): boolean;
66
+ static isUseCallbackVarDec(node: AstNode): boolean;
67
+ static collectEffectId(node: AstNode): void;
68
+ static collectEffectIdOfVarDec(node: AstNode): void;
69
+ static collectEffectIdOfUseState(node: AstNode): void;
70
+ static collectEffectIdOfUseMemo(node: AstNode): void;
71
+ static collectEffectIdOfUseCallback(node: AstNode): void;
72
+ static collectEffectIdOfAssign(node: AstNode): void;
73
+ static collectEffectIdOfUnaryUpdate(node: AstNode): void;
74
+ static isBodyArray(node: AstNode): node is ({
75
+ body: AstNode[];
76
+ } & AstNode);
77
+ static findExportIdentifiers(node: any, callback: (identifier: AstNode) => void): void;
78
+ static expressionTypeIsIdentifier(exp: AstNode | null): exp is AstNode & {
79
+ type: "Identifier";
80
+ };
81
+ static deepFindIdOfExpression(exp: AstNode | null, callback: (identifier: AstNode) => void): void;
82
+ static _deepFindIdOfExpression(exp: AstNode | null, callback: (identifier: AstNode) => void): void;
83
+ static getRootIdentifierOfMemberExpression(memExp: AstNode): AstNode;
84
+ static findIdOfImport(node: AstNode, callback: (identifier: AstNode) => void): void;
85
+ static findIdOfFunctionOrVariableOrClass(node: AstNode, callback: (identifier: AstNode) => void): void;
86
+ static findIdOfVariable(node: AstNode, callback: (identifier: AstNode) => void): void;
87
+ private static _deepFindIdentifier;
88
+ static updateLoc(astNode: AstNode, extra: {
89
+ mapUuidToNode: Map<string, AstNode>;
90
+ mapFileLineToNodeSet: Map<number, Set<AstNode>>;
91
+ }): void;
92
+ static isValidArrayNodeCollect(astNode: any): astNode is AstNode[];
93
+ static isValidNodeCollect(astNode: AstNode): astNode is AstNode;
94
+ static windowProperties: string[];
95
+ static isPropertyOfGlobal(node: AstNode): boolean;
96
+ static getTopScopeNodesByLineNumberRange(mapFileLineToNodeSet: Map<number, Set<AstNode>>, lineNumberStart: number, lineNumberEnd: number): AstNode[];
97
+ static getTopScopeNodesByLineNumber(mapFileLineToNodeSet: Map<number, Set<AstNode>>, lineNumber: number): AstNode[];
98
+ static isSubNodeOfVariableDeclarator(node: AstNode): boolean;
99
+ static createScopeContent(node: AstNode): string;
100
+ }