Add combining and extract scripts

This commit is contained in:
Tat Dat Duong
2025-07-15 17:09:10 +02:00
parent 1fbc623d57
commit b225b603b8
2 changed files with 97 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
import * as path from "node:path";
import * as fs from "node:fs/promises";
import * as url from "node:url";
const mdPath = url.fileURLToPath(
new URL("./add_translation_js_ref_updated.md", import.meta.url)
);
const extractedDir = url.fileURLToPath(
new URL(
"../../../oap-langgraphjs-tools-agent/src/add_transaction_js",
import.meta.url
)
);
const files = (await fs.readdir(extractedDir, { withFileTypes: true })).sort(
(a, b) => {
const aInt = Number.parseInt(a.name.split(".")[0], 10);
const bInt = Number.parseInt(b.name.split(".")[0], 10);
return aInt - bInt;
}
);
let count = 0;
let lines = [];
for (let file of files) {
if (file.isDirectory() || !file.name.endsWith(".mts")) continue;
count += 1;
const content = await fs.readFile(path.resolve(extractedDir, file.name), {
encoding: "utf-8",
});
lines = lines.concat(
content
.split("\n")
.reduce((acc, line) => {
if (line.trimStart().startsWith("// ```")) acc.push([]);
acc.at(-1)?.push(line);
return acc;
}, [])
.map((i) => {
const tag = i[0].trimStart().slice("// ```".length);
return ["```" + tag, ...i.slice(1), "```"].join("\n");
})
);
}
await fs.writeFile(mdPath, lines.join("\n\n"));
+46
View File
@@ -0,0 +1,46 @@
import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as url from "node:url";
const mdPath = url.fileURLToPath(
new URL("./add_translation_js_ref.md", import.meta.url)
);
const extractedDir = url.fileURLToPath(
new URL(
"../../../oap-langgraphjs-tools-agent/src/add_transaction_js",
import.meta.url
)
);
await fs.mkdir(extractedDir, { recursive: true });
const md = (await fs.readFile(mdPath, { encoding: "utf-8" })).split("\n");
const chunks = [];
let current = [];
for (let line of md) {
if (line.trimStart().startsWith("```")) {
if (current.length > 0) {
chunks.push(current.join("\n"));
current = [];
} else {
current.push("// " + line.trimStart());
}
} else if (current.length > 0) {
current.push(line);
}
}
if (current.length > 0) {
chunks.push(current.join("\n"));
}
for (let i = 0; i < chunks.length; i += 1) {
await fs.writeFile(path.resolve(extractedDir, `${i}.mts`), chunks[i], {
encoding: "utf-8",
});
}
console.log("finished");