deeplyzer 0.2.0 → 0.6.0
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/README.md +43 -2
- package/analysis/deep/assess/assessAgent.js +24 -0
- package/analysis/deep/env/environmentAgent.js +125 -0
- package/analysis/deep/generate/ganerateAgent.js +30 -0
- package/analysis/deep/orchestratorAgent.js +94 -0
- package/analysis/deep/plan/planAgent.js +26 -0
- package/analysis/static/staticAnalyzer.js +86 -0
- package/engine.js +87 -0
- package/globals.js +9 -0
- package/img/logo.png +0 -0
- package/img/workflow.png +0 -0
- package/index.js +6 -0
- package/llm/llm.js +58 -0
- package/llm/providers/claude.js +65 -0
- package/llm/providers/gemini.js +66 -0
- package/llm/providers/openai.js +91 -0
- package/open-packages.json +11 -0
- package/package.json +8 -3
- package/prompts/deep.assess +85 -0
- package/prompts/deep.env.assess +11 -0
- package/prompts/deep.env.runtime +45 -0
- package/prompts/deep.generate.output +31 -0
- package/prompts/deep.plans +153 -0
- package/prompts/graph.build_nodes +577 -0
- package/prompts/graph.environment +154 -0
- package/prompts/graph.resolve_edges +44 -0
- package/prompts/graph.source_exclusions +47 -0
- package/prompts/static.analyse +58 -0
- package/services/environmentService.js +70 -0
- package/services/graphService.js +292 -0
- package/services/workflowService.js +168 -0
- package/session/sessionManager.js +130 -0
- package/utils/execUtil.js +153 -0
- package/utils/fileUtil.js +239 -0
- package/utils/idUtil.js +17 -0
- package/utils/logger.js +93 -0
- package/utils/promptsUtil.js +29 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const globals = require('../globals');
|
|
4
|
+
const IdUtil = require('../utils/idUtil');
|
|
5
|
+
|
|
6
|
+
class WorkflowService {
|
|
7
|
+
|
|
8
|
+
static baseDir = path.join(process.cwd(), ".deeplyzer");
|
|
9
|
+
static workflowFile = path.join(WorkflowService.baseDir, "workflows.json");
|
|
10
|
+
|
|
11
|
+
build() {
|
|
12
|
+
const functionNodeIds = new Set();
|
|
13
|
+
|
|
14
|
+
for (const graph of globals.graphs) {
|
|
15
|
+
const nodes = graph.graph?.nodes || [];
|
|
16
|
+
|
|
17
|
+
for (const node of nodes) {
|
|
18
|
+
if (node?.is_function === true) {
|
|
19
|
+
functionNodeIds.add(node.id);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const workflowMap = new Map();
|
|
25
|
+
|
|
26
|
+
// Fallback: entire graph becomes a single workflow
|
|
27
|
+
if (functionNodeIds.size === 0) {
|
|
28
|
+
const allNodes = globals.graphs.flatMap(
|
|
29
|
+
graph => graph.graph?.nodes || []
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if (allNodes.length > 0) {
|
|
33
|
+
const rootNode = allNodes[0];
|
|
34
|
+
|
|
35
|
+
workflowMap.set(rootNode.id, {
|
|
36
|
+
id: IdUtil.generate(),
|
|
37
|
+
node: rootNode.id,
|
|
38
|
+
childNodes: allNodes
|
|
39
|
+
.slice(1)
|
|
40
|
+
.map(node => node.id)
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
for (const nodeId of functionNodeIds) {
|
|
45
|
+
workflowMap.set(nodeId, {
|
|
46
|
+
id: IdUtil.generate(),
|
|
47
|
+
node: nodeId,
|
|
48
|
+
childNodes: []
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const graph of graphs) {
|
|
53
|
+
const edges = graph.graph?.edges || [];
|
|
54
|
+
|
|
55
|
+
for (const edge of edges) {
|
|
56
|
+
const { node1, node2 } = edge;
|
|
57
|
+
|
|
58
|
+
if (!workflowMap.has(node1) || !workflowMap.has(node2)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
this._addConnection(workflowMap, node1, node2);
|
|
63
|
+
this._addConnection(workflowMap, node2, node1);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const workflows = [...workflowMap.values()];
|
|
69
|
+
|
|
70
|
+
this._saveWorkflows(workflows);
|
|
71
|
+
|
|
72
|
+
return workflows;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
loadWorkflows() {
|
|
76
|
+
if (!fs.existsSync(WorkflowService.workflowFile)) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(
|
|
82
|
+
fs.readFileSync(WorkflowService.workflowFile, "utf8")
|
|
83
|
+
);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new Error(`Failed to load workflows: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_saveWorkflows(workflows) {
|
|
90
|
+
fs.mkdirSync(WorkflowService.baseDir, { recursive: true });
|
|
91
|
+
|
|
92
|
+
fs.writeFileSync(
|
|
93
|
+
WorkflowService.workflowFile,
|
|
94
|
+
JSON.stringify(workflows, null, 4),
|
|
95
|
+
"utf8"
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_addConnection(workflowMap, source, target) {
|
|
100
|
+
const childNodes = workflowMap.get(source).childNodes;
|
|
101
|
+
|
|
102
|
+
if (!childNodes.includes(target)) {
|
|
103
|
+
childNodes.push(target);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fillSources() {
|
|
108
|
+
for (const workflow of globals.workflows || []) {
|
|
109
|
+
|
|
110
|
+
const nodeIds = new Set([
|
|
111
|
+
workflow.node,
|
|
112
|
+
...(workflow.childNodes || [])
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
// Group source lines by file path
|
|
116
|
+
const fileLines = new Map();
|
|
117
|
+
|
|
118
|
+
for (const graph of globals.graphs || []) {
|
|
119
|
+
|
|
120
|
+
if (!graph.file?.path) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const nodes = (graph.graph?.nodes || []).filter(
|
|
125
|
+
node => nodeIds.has(node.id)
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (!nodes.length) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const filePath = graph.file.path;
|
|
133
|
+
|
|
134
|
+
if (!fileLines.has(filePath)) {
|
|
135
|
+
fileLines.set(filePath, new Set());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const lines = fileLines.get(filePath);
|
|
139
|
+
|
|
140
|
+
for (const node of nodes) {
|
|
141
|
+
for (let line = node.startLine; line <= node.endLine; line++) {
|
|
142
|
+
lines.add(line);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Extract unique lines in ascending order, grouped by file
|
|
148
|
+
const sources = [];
|
|
149
|
+
|
|
150
|
+
for (const [filePath, lineNumbers] of fileLines) {
|
|
151
|
+
|
|
152
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
153
|
+
const lines = content.split(/\r?\n/);
|
|
154
|
+
|
|
155
|
+
const code = [...lineNumbers]
|
|
156
|
+
.sort((a, b) => a - b)
|
|
157
|
+
.map(line => lines[line - 1])
|
|
158
|
+
.join("\n");
|
|
159
|
+
|
|
160
|
+
sources.push(code);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
workflow.src = sources.join("\n\n");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = WorkflowService;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
class SessionManager {
|
|
6
|
+
|
|
7
|
+
static STATE = {
|
|
8
|
+
NONE: "none",
|
|
9
|
+
IN_PROGRESS: "in_progress",
|
|
10
|
+
COMPLETED: "completed",
|
|
11
|
+
ABORTED: "aborted"
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
static STAGES = {
|
|
15
|
+
NONE: "none",
|
|
16
|
+
BUILD_NODES: "build_nodes",
|
|
17
|
+
CONNECT_EDGES: "connect_edges",
|
|
18
|
+
BUILD_WORKFLOWS: "build_workflows",
|
|
19
|
+
DEFINE_ENVIRONMENTS: "define_environments",
|
|
20
|
+
STATIC_ANALYSIS: "static_analysis",
|
|
21
|
+
DEEP_ANALYSIS: "deep_analysis",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static session = null;
|
|
25
|
+
|
|
26
|
+
static baseDir = path.join(process.cwd(), ".deeplyzer");
|
|
27
|
+
static sessionFile = path.join(SessionManager.baseDir, "session.json");
|
|
28
|
+
|
|
29
|
+
static _ensureDirectory() {
|
|
30
|
+
fs.mkdirSync(this.baseDir, { recursive: true });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static loadSession() {
|
|
34
|
+
this._ensureDirectory();
|
|
35
|
+
|
|
36
|
+
if (!fs.existsSync(this.sessionFile)) {
|
|
37
|
+
this.session = {
|
|
38
|
+
state: this.STATE.IN_PROGRESS,
|
|
39
|
+
currentStage: this.STAGES.NONE
|
|
40
|
+
};
|
|
41
|
+
this._writeSession();
|
|
42
|
+
return this.session;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
this.session = JSON.parse(
|
|
47
|
+
fs.readFileSync(this.sessionFile, "utf8")
|
|
48
|
+
);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw new Error(`Failed to load session: ${error.message}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static updateState(state) {
|
|
55
|
+
if (!this.session) {
|
|
56
|
+
this.loadSession();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.session.state = state;
|
|
60
|
+
|
|
61
|
+
this._writeSession();
|
|
62
|
+
|
|
63
|
+
return this.session;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
static updateStage(stage) {
|
|
68
|
+
if (!this.session) {
|
|
69
|
+
this.loadSession();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.session.currentStage = stage;
|
|
73
|
+
|
|
74
|
+
this._writeSession();
|
|
75
|
+
|
|
76
|
+
return this.session;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static clearSession() {
|
|
80
|
+
this.session = {
|
|
81
|
+
state: this.STATE.NONE,
|
|
82
|
+
currentStage: this.STAGES.NONE
|
|
83
|
+
};
|
|
84
|
+
this._writeSession();
|
|
85
|
+
return this.session;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static resetAnalysisSession() {
|
|
89
|
+
this.session = {
|
|
90
|
+
state: this.STATE.IN_PROGRESS,
|
|
91
|
+
currentStage: this.STAGES.DEFINE_ENVIRONMENTS
|
|
92
|
+
};
|
|
93
|
+
this._writeSession();
|
|
94
|
+
return this.session;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static checkStage(stage) {
|
|
98
|
+
if (!this.session) {
|
|
99
|
+
this.loadSession();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const stages = Object.values(this.STAGES);
|
|
103
|
+
|
|
104
|
+
const stageIndex = stages.indexOf(stage);
|
|
105
|
+
|
|
106
|
+
if (stageIndex === -1) {
|
|
107
|
+
throw new Error(`Invalid stage: ${stage}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const lastCompletedIndex = stages.indexOf(this.session.currentStage);
|
|
111
|
+
|
|
112
|
+
return stageIndex > lastCompletedIndex;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static _writeSession() {
|
|
116
|
+
this._ensureDirectory();
|
|
117
|
+
|
|
118
|
+
fs.writeFileSync(
|
|
119
|
+
this.sessionFile,
|
|
120
|
+
JSON.stringify(this.session, null, 4),
|
|
121
|
+
"utf8"
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static getStageList() {
|
|
126
|
+
return Object.values(this.STAGES);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = SessionManager;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const { exec } = require("child_process");
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const { createRequire } = require("module");
|
|
6
|
+
const { pathToFileURL } = require("url");
|
|
7
|
+
|
|
8
|
+
class ExecUtil {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.baseDir = path.join(process.cwd(), ".deeplyzer");
|
|
11
|
+
this.execAsync = util.promisify(exec);
|
|
12
|
+
|
|
13
|
+
this.packageRequire = createRequire(
|
|
14
|
+
path.join(this.baseDir, "package.json")
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
this.platform = process.platform;
|
|
18
|
+
this.shell = this._getShell();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async exec(command, id) {
|
|
22
|
+
let cwd = this.baseDir
|
|
23
|
+
if(id) {
|
|
24
|
+
cwd = path.join(this.baseDir, "src", id);
|
|
25
|
+
|
|
26
|
+
fs.mkdirSync(cwd, { recursive: true });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const { stdout, stderr } = await this.execAsync(command, {
|
|
31
|
+
cwd,
|
|
32
|
+
shell: this.shell,
|
|
33
|
+
windowsHide: true,
|
|
34
|
+
maxBuffer: 10 * 1024 * 1024
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (stdout) process.stdout.write(stdout);
|
|
38
|
+
if (stderr) process.stderr.write(stderr);
|
|
39
|
+
|
|
40
|
+
return { success: true, stdout, stderr };
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error(`[COMMAND ERROR] ${error.message}`);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
stdout: error.stdout || "",
|
|
47
|
+
stderr: error.stderr || "",
|
|
48
|
+
error: error.message
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async installPackage(packageName) {
|
|
54
|
+
try {
|
|
55
|
+
fs.mkdirSync(this.baseDir, { recursive: true });
|
|
56
|
+
|
|
57
|
+
const { stdout, stderr } = await this.execAsync(
|
|
58
|
+
`npm install ${packageName} --no-save`,
|
|
59
|
+
{
|
|
60
|
+
cwd: this.baseDir,
|
|
61
|
+
shell: this.shell,
|
|
62
|
+
windowsHide: true,
|
|
63
|
+
maxBuffer: 10 * 1024 * 1024
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (stdout) process.stdout.write(stdout);
|
|
68
|
+
if (stderr) process.stderr.write(stderr);
|
|
69
|
+
|
|
70
|
+
return { success: true };
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return {
|
|
73
|
+
success: false,
|
|
74
|
+
error: "Package installation failed:" + error.message
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async loadPackage(packageName) {
|
|
80
|
+
try {
|
|
81
|
+
// Resolve from .deeplyzer/node_modules
|
|
82
|
+
const modulePath = this.packageRequire.resolve(packageName);
|
|
83
|
+
|
|
84
|
+
// Dynamically load CommonJS or ES modules
|
|
85
|
+
const module = await import(
|
|
86
|
+
pathToFileURL(modulePath).href
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
success: true,
|
|
91
|
+
module: module.default || module
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
error: "Package loading failed: " + error.message
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async createInstance(packageName, className, ...args) {
|
|
103
|
+
try {
|
|
104
|
+
const modulePath = this.packageRequire.resolve(packageName);
|
|
105
|
+
|
|
106
|
+
const importedModule = await import(
|
|
107
|
+
require("url").pathToFileURL(modulePath).href
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Support named exports and default exports
|
|
111
|
+
const exports = importedModule.default
|
|
112
|
+
? { ...importedModule, ...importedModule.default }
|
|
113
|
+
: importedModule;
|
|
114
|
+
|
|
115
|
+
const TargetClass = exports[className];
|
|
116
|
+
|
|
117
|
+
if (typeof TargetClass !== "function") {
|
|
118
|
+
throw new Error(`Class not found: ${className}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const instance = new TargetClass(...args);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
success: true,
|
|
125
|
+
instance
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return {
|
|
130
|
+
success: false,
|
|
131
|
+
error: "Instance creation failed: " + error.message
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_getShell() {
|
|
137
|
+
switch (this.platform) {
|
|
138
|
+
case "win32":
|
|
139
|
+
return "powershell.exe";
|
|
140
|
+
|
|
141
|
+
case "darwin":
|
|
142
|
+
return "/bin/zsh";
|
|
143
|
+
|
|
144
|
+
case "linux":
|
|
145
|
+
return "/bin/bash";
|
|
146
|
+
|
|
147
|
+
default:
|
|
148
|
+
throw new Error(`Unsupported operating system: ${this.platform}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = new ExecUtil();
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
class FileUtil {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.baseDir = path.join(process.cwd(), ".deeplyzer");
|
|
8
|
+
this._ensureDirectory();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
saveCodeFiles(files, outputDir) {
|
|
12
|
+
const root = path.resolve(this.baseDir, "src", outputDir);
|
|
13
|
+
const srcDir = path.resolve(this.baseDir, "src");
|
|
14
|
+
|
|
15
|
+
if (!root.startsWith(srcDir + path.sep)) {
|
|
16
|
+
throw new Error(`Invalid output directory: ${outputDir}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fs.mkdirSync(root, { recursive: true });
|
|
20
|
+
|
|
21
|
+
if (!Array.isArray(files)) {
|
|
22
|
+
files = [files];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const savedFiles = [];
|
|
26
|
+
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
if (!file || typeof file.filename !== "string" || typeof file.content !== "string") {
|
|
29
|
+
throw new Error("Invalid file. Expected filename and content strings.");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const filename = path.basename(file.filename);
|
|
33
|
+
|
|
34
|
+
if (filename !== file.filename || !filename.endsWith(".py")) {
|
|
35
|
+
throw new Error(`Invalid filename: ${file.filename}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let destination = path.resolve(root, filename);
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
fs.writeFileSync(destination, file.content, {
|
|
42
|
+
encoding: "utf8",
|
|
43
|
+
flag: "wx"
|
|
44
|
+
});
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (error.code === "EEXIST") {
|
|
47
|
+
throw new Error(`File already exists: ${destination}`);
|
|
48
|
+
}
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
savedFiles.push(destination);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return savedFiles;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
loadJSON(filename, defaultValue = null) {
|
|
59
|
+
try {
|
|
60
|
+
const filePath = this._getFilePath(filename);
|
|
61
|
+
|
|
62
|
+
if (!fs.existsSync(filePath)) {
|
|
63
|
+
return defaultValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
67
|
+
} catch (error) {
|
|
68
|
+
throw new Error(`Failed to load ${filename}: ${error.message}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
saveJSON(filename, data) {
|
|
73
|
+
try {
|
|
74
|
+
const filePath = this._getFilePath(filename);
|
|
75
|
+
|
|
76
|
+
fs.mkdirSync(path.dirname(filePath), {
|
|
77
|
+
recursive: true
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
fs.writeFileSync(
|
|
81
|
+
filePath,
|
|
82
|
+
JSON.stringify(data, null, 2),
|
|
83
|
+
"utf8"
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return filePath;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw new Error(`Failed to save ${filename}: ${error.message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
readPaths(paths, exclusions = []) {
|
|
93
|
+
const results = [];
|
|
94
|
+
|
|
95
|
+
for (const inputPath of paths) {
|
|
96
|
+
const resolvedPath = path.resolve(inputPath);
|
|
97
|
+
const relativePath = path.relative(process.cwd(), resolvedPath);
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
if (!fs.existsSync(resolvedPath)) continue;
|
|
101
|
+
|
|
102
|
+
const stat = fs.statSync(resolvedPath);
|
|
103
|
+
const isDirectory = stat.isDirectory();
|
|
104
|
+
|
|
105
|
+
if (this._matchesExclusion(relativePath, isDirectory, exclusions)) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (stat.isFile()) {
|
|
110
|
+
results.push({
|
|
111
|
+
filename: path.basename(resolvedPath),
|
|
112
|
+
path: resolvedPath,
|
|
113
|
+
content: fs.readFileSync(resolvedPath, "utf8")
|
|
114
|
+
});
|
|
115
|
+
} else if (isDirectory) {
|
|
116
|
+
const children = this.readPaths(
|
|
117
|
+
fs.readdirSync(resolvedPath).map(name =>
|
|
118
|
+
path.join(resolvedPath, name)
|
|
119
|
+
),
|
|
120
|
+
exclusions
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
results.push(...children);
|
|
124
|
+
}
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error(`[FileUtil] ${inputPath}: ${error.message}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return results;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
removeAllFiles(dir) {
|
|
134
|
+
const dirPath = path.join(this.baseDir, dir);
|
|
135
|
+
|
|
136
|
+
if (!fs.existsSync(dirPath)) return;
|
|
137
|
+
|
|
138
|
+
for (const entry of fs.readdirSync(dirPath, { withFileTypes: true })) {
|
|
139
|
+
const entryPath = path.join(dirPath, entry.name);
|
|
140
|
+
|
|
141
|
+
if (entry.isDirectory()) {
|
|
142
|
+
this.removeAllFiles(path.join(dir, entry.name));
|
|
143
|
+
} else {
|
|
144
|
+
fs.rmSync(entryPath, { force: true });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
_globToRegex(pattern) {
|
|
150
|
+
let regex = "";
|
|
151
|
+
|
|
152
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
153
|
+
const char = pattern[i];
|
|
154
|
+
const next = pattern[i + 1];
|
|
155
|
+
|
|
156
|
+
if (char === "*" && next === "*") {
|
|
157
|
+
regex += ".*";
|
|
158
|
+
i++;
|
|
159
|
+
} else if (char === "*") {
|
|
160
|
+
regex += "[^/]*";
|
|
161
|
+
} else if (char === "?") {
|
|
162
|
+
regex += "[^/]";
|
|
163
|
+
} else {
|
|
164
|
+
regex += char.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return new RegExp(`^${regex}$`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
_matchesExclusion(relativePath, isDirectory, exclusions = []) {
|
|
172
|
+
const normalizedPath = relativePath
|
|
173
|
+
.split(path.sep)
|
|
174
|
+
.join("/");
|
|
175
|
+
|
|
176
|
+
const segments = normalizedPath.split("/");
|
|
177
|
+
let excluded = false;
|
|
178
|
+
|
|
179
|
+
for (let pattern of exclusions) {
|
|
180
|
+
if (!pattern || pattern.startsWith("#")) continue;
|
|
181
|
+
|
|
182
|
+
let negate = pattern.startsWith("!");
|
|
183
|
+
|
|
184
|
+
if (negate) {
|
|
185
|
+
pattern = pattern.substring(1);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
pattern = pattern
|
|
189
|
+
.replace(/\\/g, "/")
|
|
190
|
+
.replace(/^\/+/, "");
|
|
191
|
+
|
|
192
|
+
const directoryOnly = pattern.endsWith("/");
|
|
193
|
+
pattern = pattern.replace(/\/+$/, "");
|
|
194
|
+
|
|
195
|
+
if (!pattern) continue;
|
|
196
|
+
|
|
197
|
+
const hasSlash = pattern.includes("/");
|
|
198
|
+
const regex = this._globToRegex(pattern);
|
|
199
|
+
|
|
200
|
+
let matched = false;
|
|
201
|
+
|
|
202
|
+
for (let i = 0; i < segments.length; i++) {
|
|
203
|
+
const candidate = segments.slice(0, i + 1).join("/");
|
|
204
|
+
const isLast = i === segments.length - 1;
|
|
205
|
+
const candidateIsDirectory = !isLast || isDirectory;
|
|
206
|
+
|
|
207
|
+
if (directoryOnly && !candidateIsDirectory) continue;
|
|
208
|
+
|
|
209
|
+
if (hasSlash) {
|
|
210
|
+
if (regex.test(candidate)) {
|
|
211
|
+
matched = true;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
if (regex.test(segments[i])) {
|
|
216
|
+
matched = true;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (matched) {
|
|
223
|
+
excluded = !negate;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return excluded;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
_ensureDirectory() {
|
|
231
|
+
fs.mkdirSync(this.baseDir, { recursive: true });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
_getFilePath(filename) {
|
|
235
|
+
return path.resolve(this.baseDir, filename);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
module.exports = FileUtil;
|
package/utils/idUtil.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
|
|
3
|
+
class IdUtil {
|
|
4
|
+
static generate(length = 6) {
|
|
5
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
6
|
+
let id = "";
|
|
7
|
+
|
|
8
|
+
for (let i = 0; i < length; i++) {
|
|
9
|
+
const index = crypto.randomInt(0, chars.length);
|
|
10
|
+
id += chars[index];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return id;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = IdUtil;
|