Skip to content

Commit 6690287

Browse files
committed
implement python support, create some helper classes
1 parent 1cbcf2f commit 6690287

File tree

13 files changed

+403
-245
lines changed

13 files changed

+403
-245
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ node_modules
22
dist
33
.env
44
binaries
5-
napi_dist
5+
napi_dist
6+
__pycache__
7+
.ruff_cache

package-lock.json

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"prompts": "^2.4.2",
8383
"tree-sitter": "^0.21.1",
8484
"tree-sitter-javascript": "^0.23.0",
85+
"tree-sitter-python": "^0.23.4",
8586
"tree-sitter-typescript": "^0.23.0",
8687
"uuid": "^10.0.0",
8788
"yargs": "^17.7.2",

packages/cli/src/api/scan.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { z } from "zod";
2-
import {
3-
getDependencyTree,
4-
getEndpontsFromTree,
5-
} from "../helper/dependencyTree";
2+
import DependencyTreeManager from "../helper/dependencyTree";
63
import { scanSchema } from "./helpers/validation";
74

85
export function scan(payload: z.infer<typeof scanSchema>) {
9-
const tree = getDependencyTree(payload.entrypointPath);
10-
11-
const endpoints = getEndpontsFromTree(tree);
12-
6+
const dependencyTreeManager = new DependencyTreeManager(
7+
payload.entrypointPath,
8+
);
9+
const endpoints = dependencyTreeManager.getEndponts();
1310
return { endpoints };
1411
}

packages/cli/src/api/split.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import fs from "fs";
22
import path from "path";
3-
import {
4-
getDependencyTree,
5-
getEndpontsFromTree,
6-
getGroupsFromEndpoints,
7-
} from "../helper/dependencyTree";
3+
import DependencyTreeManager from "../helper/dependencyTree";
84
import { cleanupOutputDir, createOutputDir } from "../helper/file";
9-
import { createSplit } from "../helper/split";
5+
import SplitRunner from "../helper/split";
106
import { splitSchema } from "./helpers/validation";
117
import { z } from "zod";
128
import { GroupMap } from "../helper/types";
@@ -16,25 +12,38 @@ export function split(payload: z.infer<typeof splitSchema>) {
1612
const groupMap: GroupMap = {};
1713

1814
// Get the dependency tree
19-
const tree = getDependencyTree(payload.entrypointPath);
15+
const dependencyTreeManager = new DependencyTreeManager(
16+
payload.entrypointPath,
17+
);
2018

2119
const outputDir = payload.outputDir || path.dirname(payload.entrypointPath);
2220

2321
// Clean up and prepare the output directory
2422
cleanupOutputDir(outputDir);
2523
createOutputDir(outputDir);
2624

27-
// Iterate over the tree and get endpoints
28-
const endpoints = getEndpontsFromTree(tree);
29-
30-
// Get groups from the endpoints
31-
const groups = getGroupsFromEndpoints(endpoints);
25+
// Get groups from the dependency tree
26+
const groups = dependencyTreeManager.getGroups();
3227

3328
// Process each group for splitting
3429
groups.forEach((group, index) => {
35-
// Clone the tree to avoid mutation of the original tree
36-
const treeClone = structuredClone(tree);
37-
createSplit(treeClone, group, outputDir, payload.entrypointPath, index);
30+
console.log(JSON.stringify(dependencyTreeManager.dependencyTree, null, 2));
31+
32+
const splitRunner = new SplitRunner(dependencyTreeManager, group);
33+
const files = splitRunner.run();
34+
35+
const targetDir = path.dirname(payload.entrypointPath);
36+
const annotationDirectory = path.join(outputDir, index.toString());
37+
38+
files.forEach((file) => {
39+
const relativeFileNamePath = path.relative(targetDir, file.path);
40+
const destinationPath = path.join(
41+
annotationDirectory,
42+
relativeFileNamePath,
43+
);
44+
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
45+
fs.writeFileSync(destinationPath, file.sourceCode, "utf8");
46+
});
3847
});
3948

4049
// Store the processed annotations in the output directory

packages/cli/src/api/sync.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import {
55
parseNanoApiAnnotation,
66
updateCommentFromAnnotation,
77
} from "../helper/annotations";
8-
import {
9-
getDependencyTree,
10-
getEndpontsFromTree,
11-
} from "../helper/dependencyTree";
8+
import DependencyTreeManager from "../helper/dependencyTree";
129
import Parser from "tree-sitter";
1310
import { getParserLanguageFromFile } from "../helper/treeSitter";
1411
import { replaceIndexesFromSourceCode } from "../helper/cleanup";
1512
import { getJavascriptAnnotationNodes } from "../helper/languages/javascript/annotations";
1613
import { getTypescriptAnnotationNodes } from "../helper/languages/typescript/annotations";
1714

1815
export function sync(payload: z.infer<typeof syncSchema>) {
19-
const tree = getDependencyTree(payload.entrypointPath);
16+
const dependencyTreeManager = new DependencyTreeManager(
17+
payload.entrypointPath,
18+
);
2019

21-
const endpoints = getEndpontsFromTree(tree);
20+
const endpoints = dependencyTreeManager.getEndponts();
2221

2322
const updatedEndpoints = endpoints.map((endpoint) => {
2423
const updatedEndpoint = payload.endpoints.find(

packages/cli/src/commands/annotate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from "fs";
2-
import { getDependencyTree } from "../helper/dependencyTree";
2+
import DependencyTreeManager from "../helper/dependencyTree";
33
import OpenAI from "openai";
44
import { DependencyTree } from "../helper/types";
55

@@ -8,7 +8,7 @@ export default async function annotateOpenAICommandHandler(
88
openAIApiKey: string, // OpenAI API key
99
) {
1010
console.info("Annotating program...");
11-
const tree = getDependencyTree(entrypoint);
11+
const dependencyTreeManager = new DependencyTreeManager(entrypoint);
1212

1313
const openAIConfig = {
1414
apiKey: openAIApiKey,
@@ -24,7 +24,7 @@ export default async function annotateOpenAICommandHandler(
2424
messages.push({
2525
role: "user",
2626
content: `The entrypoint of my application is: ${entrypoint}.
27-
Here is the dependency tree of the entrypoint of: ${JSON.stringify(tree)}`,
27+
Here is the dependency tree of the entrypoint of: ${JSON.stringify(dependencyTreeManager.dependencyTree)}`,
2828
});
2929

3030
messages.push({
@@ -50,7 +50,7 @@ export default async function annotateOpenAICommandHandler(
5050
iterateOverTree(child);
5151
});
5252
}
53-
iterateOverTree(tree);
53+
iterateOverTree(dependencyTreeManager.dependencyTree);
5454

5555
messages.push({
5656
role: "user",

packages/cli/src/commands/split.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import path from "path";
22
import fs from "fs";
3-
import {
4-
getDependencyTree,
5-
getEndpontsFromTree,
6-
getGroupsFromEndpoints,
7-
} from "../helper/dependencyTree";
3+
import DependencyTreeManager from "../helper/dependencyTree";
84
import { cleanupOutputDir, createOutputDir } from "../helper/file";
9-
import { createSplit } from "../helper/split";
5+
import SplitRunner from "../helper/split";
106
import { GroupMap } from "../helper/types";
117

128
export default function splitCommandHandler(
@@ -15,22 +11,31 @@ export default function splitCommandHandler(
1511
) {
1612
const groupMap: GroupMap = {};
1713

18-
const tree = getDependencyTree(entrypointPath);
14+
const dependencyTreeManager = new DependencyTreeManager(entrypointPath);
1915

2016
cleanupOutputDir(outputDir);
2117
createOutputDir(outputDir);
2218

23-
// Iterate over the tree and get endpoints
24-
const endpoints = getEndpontsFromTree(tree);
25-
26-
// Get groups from the endpoints
27-
const groups = getGroupsFromEndpoints(endpoints);
19+
// Get groups from the dependency tree
20+
const groups = dependencyTreeManager.getGroups();
2821

2922
// Process each group for splitting
3023
groups.forEach((group, index) => {
31-
// Clone the tree to avoid mutation of the original tree
32-
const treeClone = structuredClone(tree);
33-
createSplit(treeClone, group, outputDir, entrypointPath, index);
24+
const splitRunner = new SplitRunner(dependencyTreeManager, group);
25+
const files = splitRunner.run();
26+
27+
const targetDir = path.dirname(entrypointPath);
28+
const annotationDirectory = path.join(outputDir, index.toString());
29+
30+
files.forEach((file) => {
31+
const relativeFileNamePath = path.relative(targetDir, file.path);
32+
const destinationPath = path.join(
33+
annotationDirectory,
34+
relativeFileNamePath,
35+
);
36+
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
37+
fs.writeFileSync(destinationPath, file.sourceCode, "utf8");
38+
});
3439
});
3540

3641
// Store the processed annotations in the output directory

0 commit comments

Comments
 (0)