// Generated by ReScript, PLEASE EDIT WITH CARE import * as Nodefs from "node:fs"; import * as Nodeos from "node:os"; import * as Nodeurl from "node:url"; import * as Nodepath from "node:path"; import * as ArrayUtils from "./ArrayUtils.res.js"; import * as SpawnAsync from "./SpawnAsync.res.js"; import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.mjs"; import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.mjs"; import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.mjs"; import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.mjs"; import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.mjs"; import * as Promises from "node:fs/promises"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.mjs"; import * as BinsJs from "../../../cli/common/bins.js"; let rescript_tools_exe = BinsJs.rescript_tools_exe; let nodeVersion = Stdlib_Option.getOrThrow(Stdlib_Int.fromString(Stdlib_Option.getOrThrow(process.version.replace("v", "").split(".")[0], "Failed to find major version of Node")), "Failed to convert node version to Int"); let ignoreRuntimeTests = [ [ 24, ["Stdlib_RegExp.escape"] ], [ 24, [ "Stdlib_DataView.getFloat16", "Stdlib_DataView.setFloat16" ] ], [ 26, ["Stdlib_Date.toTemporalInstant"] ] ]; let ignoreRuntimeTestModules = [[ 26, "Stdlib_Temporal" ]]; function getOutput(buffer) { return buffer.map(e => e.toString()).join(""); } async function extractDocFromFile(file) { let match = await SpawnAsync.run(rescript_tools_exe, [ "extract-codeblocks", file, "--transform-assert-equal" ], undefined); try { return JSON.parse(getOutput(match.stdout)); } catch (raw_e) { let e = Primitive_exceptions.internalToException(raw_e); if (e.RE_EXN_ID === "JsExn") { console.error(e._1); let msg = `Failed to extract code blocks from ${file}`; throw new Error(`Panic! ${msg}`); } throw e; } } let batchSize = Nodeos.cpus().length; let docPaths = [ Nodepath.join("packages", "@rescript", "runtime"), Nodepath.join("packages", "@rescript", "belt", "src") ]; async function extractExamples() { let docFiles = docPaths.flatMap(docPath => { let files = Nodefs.readdirSync(docPath); return files.filter(f => { if (f.startsWith("Js") || f.startsWith("RescriptTools")) { return false; } else if (f.endsWith(".resi")) { return true; } else if (f.endsWith(".res")) { return !files.includes(f + "i"); } else { return false; } }).map(f => Nodepath.join(docPath, f)); }); console.log(`Extracting examples from ${docFiles.length.toString()} runtime and Belt files...`); let examples = []; await ArrayUtils.forEachAsyncInBatches(docFiles, batchSize, async file => { let doc = await extractDocFromFile(file); examples.push(...doc.filter(d => d.code.includes("assertEqual("))); }); examples.sort((a, b) => Primitive_string.compare(a.id, b.id)); return examples; } async function main() { let examples = await extractExamples(); let dict = {}; examples.forEach(cur => { let modulePath = cur.id.split("."); let id = modulePath.slice(0, modulePath.length - 1 | 0).join("."); let p = dict[id]; let previous = p !== undefined ? p : []; dict[id] = [cur].concat(previous); }); let output = []; Stdlib_Dict.forEachWithKey(dict, (examples, key) => { examples.sort((a, b) => Primitive_string.compare(a.name, b.name)); let codeExamples = Stdlib_Array.filterMap(examples, example => { let ignoreExample = ignoreRuntimeTests.some(param => { if (nodeVersion < param[0]) { return param[1].includes(example.id); } else { return false; } }) || ignoreRuntimeTestModules.some(param => { if (nodeVersion < param[0]) { return example.id.startsWith(param[1]); } else { return false; } }); let match = ignoreExample ? (console.warn(`Ignoring ${example.id} tests. Not supported by Node ${nodeVersion.toString()}`), [ "testSkip", "testAsyncSkip" ]) : [ "test", "testAsync" ]; let code = example.code; if (code.length === 0) { return; } else if (code.includes("await")) { return `${match[1]}("${example.name}", async () => { module Test = { ${code} } () })`; } else { return `${match[0]}("${example.name}", () => { module Test = { ${code} } () })`; } }); if (codeExamples.length === 0) { return; } let content = `describe("${key}", () => { ${codeExamples.join("\n")} })`; output.push(content); }); let dirname = Nodepath.dirname(Nodeurl.fileURLToPath(import.meta.url)); let filepath = Nodepath.join(dirname, "generated_mocha_test.res"); let fileContent = `open Mocha @@warning("-32-34-60-37-109-3-44") ${output.join("\n")}`; return await Promises.writeFile(filepath, fileContent); } await main(); /* rescript_tools_exe Not a pure module */