Skip to content

Commit

Permalink
scripts[patch]: Revert change to delete folder func (langchain-ai#6607)
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored and CarterMorris committed Nov 10, 2024
1 parent 94aa109 commit a28c6d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
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

0 comments on commit a28c6d4

Please sign in to comment.