Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts[patch]: Revert change to delete folder func #6607

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/langchain-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/scripts",
"version": "0.1.0",
"version": "0.1.1",
"description": "Shared scripts for LangChain.js",
"type": "module",
"engines": {
Expand Down
79 changes: 35 additions & 44 deletions libs/langchain-scripts/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Command } from "commander";
import { rollup } from "@rollup/wasm-node";
import path from "node:path";
import { glob } from "glob";
import { setTimeout } from "node:timers/promises";
import { ExportsMapValue, ImportData, LangChainConfig } from "../types.js";

async function asyncSpawn(command: string, args: string[]) {
Expand All @@ -29,55 +28,47 @@ async function asyncSpawn(command: string, args: string[]) {
});
}

const deleteFolderRecursive = async function (
inputPath: string,
retries = 3,
delay = 100
) {
for (let attempt = 0; attempt < retries; attempt += 1) {
try {
// Verify the path exists
if (
await fs.promises
.access(inputPath)
.then(() => true)
.catch(() => false)
) {
const pathStat = await fs.promises.lstat(inputPath);
// If it's a file, delete it and return
if (pathStat.isFile()) {
await fs.promises.unlink(inputPath);
} else if (pathStat.isDirectory()) {
// List contents of directory
const directoryContents = await fs.promises.readdir(inputPath);
if (directoryContents.length) {
for await (const item of directoryContents) {
await deleteFolderRecursive(
path.join(inputPath, item),
retries,
delay
);
const deleteFolderRecursive = async function (inputPath: string) {
try {
// Verify the path exists
if (
await fs.promises
.access(inputPath)
.then(() => true)
.catch(() => false)
) {
const pathStat = await fs.promises.lstat(inputPath);
// If it's a file, delete it and return
if (pathStat.isFile()) {
await fs.promises.unlink(inputPath);
} else if (pathStat.isDirectory()) {
// List contents of directory
const directoryContents = await fs.promises.readdir(inputPath);
if (directoryContents.length) {
for await (const item of directoryContents) {
const itemStat = await fs.promises.lstat(
path.join(inputPath, item)
);
if (itemStat.isFile()) {
// Delete file
await fs.promises.unlink(path.join(inputPath, item));
} else if (itemStat.isDirectory()) {
await deleteFolderRecursive(path.join(inputPath, item));
}
}
// If the directory is empty or all contents have been deleted, delete it
} else if (directoryContents.length === 0) {
// If the directory is empty, delete it
await fs.promises.rmdir(inputPath);
}
}
// If we reach here, the operation was successful
return;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.code === "ENOENT") {
// File or directory doesn't exist, consider it deleted
return;
}
if (attempt === retries - 1) {
// If this was the last attempt, throw the error
throw error;
}
// Wait before the next attempt
await setTimeout(delay);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.code !== "ENOENT") {
// If the error is not "file or directory doesn't exist", rethrow it
throw error;
}
// Otherwise, ignore the error (file or directory already doesn't exist)
}
};

Expand Down
Loading