Skip to content

Commit

Permalink
Fix worker script issues and await in api
Browse files Browse the repository at this point in the history
  • Loading branch information
erbesharat committed Dec 9, 2024
1 parent 4f09e12 commit 27c8b6e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 61 deletions.
10 changes: 5 additions & 5 deletions packages/cli/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { json, Router } from "express";
import { z } from "zod";
import { napiConfigSchema } from "../config";
import { scanSchema, splitSchema, syncSchema } from "./helpers/validation";
import { scan } from "./scan";
import { split } from "./split";
import { sync } from "./sync";
import { scanSchema, splitSchema, syncSchema } from "./helpers/validation";
import { napiConfigSchema } from "../config";
import { z } from "zod";

export function getApi(napiConfig: z.infer<typeof napiConfigSchema>) {
const api = Router();
Expand Down Expand Up @@ -42,13 +42,13 @@ export function getApi(napiConfig: z.infer<typeof napiConfigSchema>) {
res.status(200).json({ success: true });
});

api.post("/api/split", (req, res) => {
api.post("/api/split", async (req, res) => {
const result = splitSchema.safeParse(req.body);
if (!result.success) {
res.status(400).json(result.error.issues);
return;
}
const splitResult = split(result.data);
const splitResult = await split(result.data);
res.status(200).json(splitResult);
});

Expand Down
46 changes: 21 additions & 25 deletions packages/cli/src/api/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { cleanupOutputDir, createOutputDir } from "../helper/file";
import SplitRunner from "../splitRunner/splitRunner";
import { splitSchema } from "./helpers/validation";

export function split(payload: z.infer<typeof splitSchema>) {
export async function split(payload: z.infer<typeof splitSchema>) {
console.time("split command");
const groupMap: Record<number, Group> = {};

// Get the dependency tree
const dependencyTreeManager = new DependencyTreeManager(
payload.entrypointPath,
payload.entrypointPath

Check failure on line 16 in packages/cli/src/api/split.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);

const outputDir = payload.outputDir || path.dirname(payload.entrypointPath);
Expand All @@ -26,30 +26,26 @@ export function split(payload: z.infer<typeof splitSchema>) {
const groups = dependencyTreeManager.getGroups();

// Process each group for splitting
groups.forEach((group, index) => {
const splitRunner = new SplitRunner(dependencyTreeManager, group);
const files = splitRunner.run();

const targetDir = path.dirname(payload.entrypointPath);
const annotationDirectory = path.join(outputDir, index.toString());

files
.then((files) => {
files.forEach((file) => {
const relativeFileNamePath = path.relative(targetDir, file.path);
const destinationPath = path.join(
annotationDirectory,
relativeFileNamePath,
);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(destinationPath, file.sourceCode, "utf8");
});
})
.catch((error) => {
console.error(error);
throw error;
await Promise.all(
groups.map(async (group, index) => {
const splitRunner = new SplitRunner(dependencyTreeManager, group);
const files = await splitRunner.run();

const targetDir = path.dirname(payload.entrypointPath);
const annotationDirectory = path.join(outputDir, index.toString());

files.forEach((file) => {
const relativeFileNamePath = path.relative(targetDir, file.path);
const destinationPath = path.join(
annotationDirectory,
relativeFileNamePath

Check failure on line 41 in packages/cli/src/api/split.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(destinationPath, file.sourceCode, "utf8");
});
});
return files;
})

Check failure on line 47 in packages/cli/src/api/split.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);

// Store the processed annotations in the output directory
groups.forEach((group, index) => {
Expand Down
45 changes: 20 additions & 25 deletions packages/cli/src/commands/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Group } from "../dependencyManager/types";
import { cleanupOutputDir, createOutputDir } from "../helper/file";
import SplitRunner from "../splitRunner/splitRunner";

export default function splitCommandHandler(
export default async function splitCommandHandler(
entrypointPath: string, // Path to the entrypoint file
outputDir: string, // Path to the output directory
outputDir: string // Path to the output directory

Check failure on line 10 in packages/cli/src/commands/split.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
) {
const groupMap: Record<number, Group> = {};

Expand All @@ -20,30 +20,25 @@ export default function splitCommandHandler(
const groups = dependencyTreeManager.getGroups();

// Process each group for splitting
groups.forEach((group, index) => {
const splitRunner = new SplitRunner(dependencyTreeManager, group);
const files = splitRunner.run();

const targetDir = path.dirname(entrypointPath);
const annotationDirectory = path.join(outputDir, index.toString());

files
.then((files) => {
files.forEach((file) => {
const relativeFileNamePath = path.relative(targetDir, file.path);
const destinationPath = path.join(
annotationDirectory,
relativeFileNamePath,
);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(destinationPath, file.sourceCode, "utf8");
});
})
.catch((error) => {
console.error(error);
throw error;
await Promise.all(
groups.map(async (group, index) => {
const splitRunner = new SplitRunner(dependencyTreeManager, group);
const files = await splitRunner.run();

const targetDir = path.dirname(entrypointPath);
const annotationDirectory = path.join(outputDir, index.toString());

files.forEach((file) => {
const relativeFileNamePath = path.relative(targetDir, file.path);
const destinationPath = path.join(
annotationDirectory,
relativeFileNamePath

Check failure on line 35 in packages/cli/src/commands/split.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
fs.writeFileSync(destinationPath, file.sourceCode, "utf8");
});
});
})

Check failure on line 40 in packages/cli/src/commands/split.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);

// Store the processed annotations in the output directory
groups.forEach((group, index) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/splitRunner/splitRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SplitRunner {
console.info(`Splitting group: ${this.group.name}`);
console.time("Total Splitting Time");

const worker = new Worker(path.resolve(__dirname, "worker.js"), {
const worker = new Worker(path.resolve(__dirname, "worker"), {
workerData: {
entrypointPath: this.dependencyTreeManager.dependencyTree.path,
group: this.group,
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/splitRunner/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function removeAnnotationFromOtherGroups() {

const updatedSourceCode = languagePlugin.removeAnnotationFromOtherGroups(
file.sourceCode,
group,
group

Check failure on line 28 in packages/cli/src/splitRunner/worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
return { ...file, sourceCode: updatedSourceCode };
});
Expand All @@ -51,7 +51,7 @@ function removeInvalidImportsAndUsages(exportMap: Map<string, DepExport[]>) {
const updatedSourceCode = languagePlugin.cleanupInvalidImports(
file.path,
file.sourceCode,
exportMap,
exportMap

Check failure on line 54 in packages/cli/src/splitRunner/worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);

return { ...file, sourceCode: updatedSourceCode };
Expand All @@ -64,7 +64,7 @@ function removeUnusedImports() {

const updatedSourceCode = languagePlugin.cleanupUnusedImports(
file.path,
file.sourceCode,
file.sourceCode

Check failure on line 67 in packages/cli/src/splitRunner/worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);

return { ...file, sourceCode: updatedSourceCode };
Expand Down Expand Up @@ -135,7 +135,7 @@ function removeErrors() {

const query = new Parser.Query(
languagePlugin.parser.getLanguage(),
"(ERROR) @error",
"(ERROR) @error"

Check failure on line 138 in packages/cli/src/splitRunner/worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
const errorCaptures = query.captures(tree.rootNode);
errorCaptures.forEach((capture) => {
Expand All @@ -147,7 +147,7 @@ function removeErrors() {

const updatedSourceCode = removeIndexesFromSourceCode(
file.sourceCode,
indexesToRemove,
indexesToRemove
);

return { ...file, sourceCode: updatedSourceCode };
Expand Down

0 comments on commit 27c8b6e

Please sign in to comment.