Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jul 23, 2024
1 parent 59ec7ce commit 97f9c34
Showing 1 changed file with 37 additions and 44 deletions.
81 changes: 37 additions & 44 deletions libs/langchain-scripts/src/build_v2.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { spawn } from "node:child_process";
import ts from "typescript";
import fs from "node:fs";
import { rimraf } from "rimraf";
import { Command } from "commander";
import { rollup } from "@rollup/wasm-node";
import path from "node:path";
import { rm } from "node:fs/promises";
import { ExportsMapValue, ImportData, LangChainConfig } from "./types.js";
import { fileURLToPath } from 'url';

async function asyncSpawn(command: string, args: string[]) {
return new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -613,57 +612,42 @@ export async function buildWithTSup() {
pre,
} = processOptions();

let config: LangChainConfig | undefined;
try {
// Required for cross-platform compatibility.
const importPath = fileURLToPath(new URL('langchain.config.js', `file://${process.cwd()}/`));
const configFile = await import(importPath);
config = configFile.config;
} catch (e: any) {
console.error(e.message);
console.error("Error trying to load with fileURLToPath");
}

try {
if (!config) {
console.log("----- trying to load via new URL() -----")
// Required for cross-platform compatibility.
let importPath = new URL('langchain.config.js', import.meta.url).pathname;
if (importPath.endsWith("/dist/langchain.config.js")) {
importPath = importPath.replace("/dist/langchain.config.js", "/langchain.config.js");
}
const configFile = await import(importPath);
config = configFile.config;
} else {
console.log("----- CONFIG LOADED -----")
}

} catch (e: any) {
console.error(e.message);
console.error("Error trying to load with new URL");
}

if (!config) {
throw new Error("No config found");
} else {
console.log("--------- CONFIG SUCCESSFULLY LOADED ---------")
// Required for cross-platform compatibility.
let importPath = new URL("langchain.config.js", import.meta.url).pathname;
if (importPath.endsWith("/dist/langchain.config.js")) {
importPath = importPath.replace(
"/dist/langchain.config.js",
"/langchain.config.js"
);
}

const { config }: { config: LangChainConfig } = await import(importPath);

// Clean & generate build files
if (pre && shouldGenMaps) {
await Promise.all([
rimraf("dist"),
rimraf(".turbo"),
rm("dist", { recursive: true, force: true }).catch((e) => {
console.error("Error removing dist (pre && shouldGenMaps)");
throw e;
}),
rm(".turbo", { recursive: true, force: true }).catch((e) => {
console.error("Error removing .turbo (pre && shouldGenMaps)");
throw e;
}),
cleanGeneratedFiles(config),
createImportMapFile(config),
generateImportConstants(config),
generateImportTypes(config),
]);
} else if (pre && !shouldGenMaps) {
await Promise.all([
rimraf("dist"),
rimraf(".turbo"),
rm("dist", { recursive: true, force: true }).catch((e) => {
console.error("Error removing dist (pre && !shouldGenMaps)");
throw e;
}),
rm(".turbo", { recursive: true, force: true }).catch((e) => {
console.error("Error removing .turbo (pre && !shouldGenMaps)");
throw e;
}),
cleanGeneratedFiles(config),
]);
}
Expand All @@ -681,9 +665,18 @@ export async function buildWithTSup() {
// move CJS to dist
await Promise.all([
updatePackageJson(config),
rimraf("dist-cjs"),
rimraf("dist/tests"),
rimraf("dist/**/tests"),
rm("dist-cjs", { recursive: true, force: true }).catch((e) => {
console.error("Error removing dist-cjs");
throw e;
}),
rm("dist/tests", { recursive: true, force: true }).catch((e) => {
console.error("Error removing dist/tests");
throw e;
}),
rm("dist/**/tests", { recursive: true, force: true }).catch((e) => {
console.error("Error removing dist/**/tests");
throw e;
}),
]);
}

Expand Down

0 comments on commit 97f9c34

Please sign in to comment.