UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

80 lines (79 loc) 2.68 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import * as nodeFs from "fs"; const basename = /* @__PURE__ */ __name((path, ext) => { const parts = path.split(/[\\/]/); const base = parts[parts.length - 1]; const index = ext ? base.lastIndexOf("." + ext) : -1; return index === -1 ? base : base.substring(0, index); }, "basename"); const dirname = /* @__PURE__ */ __name((path) => { const parts = path.split(/[\\/]/); if (parts.length === 1) { return "."; } return parts.slice(0, parts.length - 1).join("/"); }, "dirname"); const copyFolderToMemFs = /* @__PURE__ */ __name((fs, source, target, recursive, filter) => { const dir = nodeFs.opendirSync(source); let entry; while ((entry = dir.readSync()) !== null) { if (entry.name.startsWith(".")) { continue; } const sourcePath = source + "/" + entry.name; const targetPath = target + "/" + entry.name; if (entry.isDirectory()) { if (!recursive) { continue; } fs.mkdirSync(targetPath); copyFolderToMemFs(fs, sourcePath, targetPath, true, filter); } else { if (filter && !filter.test(entry.name)) { continue; } fs.writeFileSync(targetPath, nodeFs.readFileSync(sourcePath)); } } dir.closeSync(); }, "copyFolderToMemFs"); const copyFolderFromMemFs = /* @__PURE__ */ __name((fs, source, target, recursive, filter) => { const entries = fs.readdirSync(source, { withFileTypes: true, encoding: "utf-8" }); for (const entry of entries) { const name = entry.name.toString(); if (name.toString().startsWith(".")) { continue; } const sourcePath = source + "/" + name; const targetPath = target + "/" + name; if (entry.isDirectory()) { if (!recursive) { continue; } nodeFs.mkdirSync(targetPath); copyFolderFromMemFs(fs, sourcePath, targetPath, recursive, filter); } else { if (filter && !filter.test(name)) { continue; } nodeFs.writeFileSync(targetPath, fs.readFileSync(sourcePath)); } } }, "copyFolderFromMemFs"); const generateRandomFilename = /* @__PURE__ */ __name((prefix, length = 8) => { const validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"; let randomChars = ""; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * validChars.length); randomChars += validChars[randomIndex]; } return `${prefix}${randomChars}`; }, "generateRandomFilename"); export { basename, copyFolderFromMemFs, copyFolderToMemFs, dirname, generateRandomFilename };