UNPKG

@kubb/core

Version:

Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.

157 lines (149 loc) 3.74 kB
import fs3 from 'fs-extra'; import { switcher } from 'js-runtime'; import { resolve, relative, normalize } from 'path'; // src/fs/clean.ts async function clean(path) { return fs3.remove(path); } var reader = switcher( { node: async (path) => { return fs3.readFile(path, { encoding: "utf8" }); }, bun: async (path) => { const file = Bun.file(path); return file.text(); } }, "node" ); var syncReader = switcher( { node: (path) => { return fs3.readFileSync(path, { encoding: "utf8" }); }, bun: () => { throw new Error("Bun cannot read sync"); } }, "node" ); async function read(path) { return reader(path); } function readSync(path) { return syncReader(path); } var writer = switcher( { node: async (path, data, { sanity }) => { try { const oldContent = await fs3.readFile(resolve(path), { encoding: "utf-8" }); if (oldContent?.toString() === data?.toString()) { return; } } catch (_err) { } await fs3.outputFile(resolve(path), data, { encoding: "utf-8" }); if (sanity) { const savedData = await fs3.readFile(resolve(path), { encoding: "utf-8" }); if (savedData?.toString() !== data?.toString()) { throw new Error(`Sanity check failed for ${path} Data[${data.length}]: ${data} Saved[${savedData.length}]: ${savedData} `); } return savedData; } return data; }, bun: async (path, data, { sanity }) => { try { await Bun.write(resolve(path), data); if (sanity) { const file = Bun.file(resolve(path)); const savedData = await file.text(); if (savedData?.toString() !== data?.toString()) { throw new Error(`Sanity check failed for ${path} Data[${path.length}]: ${path} Saved[${savedData.length}]: ${savedData} `); } return savedData; } return data; } catch (e) { console.error(e); } } }, "node" ); async function write(path, data, options = {}) { if (data.trim() === "") { return void 0; } return writer(path, data.trim(), options); } var reader2 = switcher( { node: async (path) => { return fs3.pathExists(path); }, bun: async (path) => { const file = Bun.file(path); return file.exists(); } }, "node" ); switcher( { node: (path) => { return fs3.pathExistsSync(path); }, bun: () => { throw new Error("Bun cannot read sync"); } }, "node" ); async function exists(path) { return reader2(path); } function slash(path, platform = "linux") { const isWindowsPath = /^\\\\\?\\/.test(path); const normalizedPath = normalize(path); if (["linux", "mac"].includes(platform) && !isWindowsPath) { return normalizedPath.replaceAll(/\\/g, "/").replace("../", ""); } return normalizedPath.replaceAll(/\\/g, "/").replace("../", ""); } function getRelativePath(rootDir, filePath, platform = "linux") { if (!rootDir || !filePath) { throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ""} ${filePath || ""}`); } const relativePath = relative(rootDir, filePath); const slashedPath = slash(relativePath, platform); if (slashedPath.startsWith("../")) { return slashedPath; } return `./${slashedPath}`; } // src/fs/types.ts var types_exports = {}; // src/fs/index.ts function trimExtName(text) { return text.replace(/\.[^/.]+$/, ""); } export { clean, exists, getRelativePath, read, readSync, trimExtName, types_exports, write }; //# sourceMappingURL=chunk-YRPOID7E.js.map //# sourceMappingURL=chunk-YRPOID7E.js.map