@kubb/core
Version:
Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.
170 lines (159 loc) • 4.26 kB
JavaScript
;
var fs3 = require('fs-extra');
var jsRuntime = require('js-runtime');
var path = require('path');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var fs3__default = /*#__PURE__*/_interopDefault(fs3);
// src/fs/clean.ts
async function clean(path) {
return fs3__default.default.remove(path);
}
var reader = jsRuntime.switcher(
{
node: async (path) => {
return fs3__default.default.readFile(path, { encoding: "utf8" });
},
bun: async (path) => {
const file = Bun.file(path);
return file.text();
}
},
"node"
);
var syncReader = jsRuntime.switcher(
{
node: (path) => {
return fs3__default.default.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 = jsRuntime.switcher(
{
node: async (path$1, data, { sanity }) => {
try {
const oldContent = await fs3__default.default.readFile(path.resolve(path$1), {
encoding: "utf-8"
});
if (oldContent?.toString() === data?.toString()) {
return;
}
} catch (_err) {
}
await fs3__default.default.outputFile(path.resolve(path$1), data, { encoding: "utf-8" });
if (sanity) {
const savedData = await fs3__default.default.readFile(path.resolve(path$1), {
encoding: "utf-8"
});
if (savedData?.toString() !== data?.toString()) {
throw new Error(`Sanity check failed for ${path$1}
Data[${data.length}]:
${data}
Saved[${savedData.length}]:
${savedData}
`);
}
return savedData;
}
return data;
},
bun: async (path$1, data, { sanity }) => {
try {
await Bun.write(path.resolve(path$1), data);
if (sanity) {
const file = Bun.file(path.resolve(path$1));
const savedData = await file.text();
if (savedData?.toString() !== data?.toString()) {
throw new Error(`Sanity check failed for ${path$1}
Data[${path$1.length}]:
${path$1}
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 = jsRuntime.switcher(
{
node: async (path) => {
return fs3__default.default.pathExists(path);
},
bun: async (path) => {
const file = Bun.file(path);
return file.exists();
}
},
"node"
);
jsRuntime.switcher(
{
node: (path) => {
return fs3__default.default.pathExistsSync(path);
},
bun: () => {
throw new Error("Bun cannot read sync");
}
},
"node"
);
async function exists(path) {
return reader2(path);
}
function slash(path$1, platform = "linux") {
const isWindowsPath = /^\\\\\?\\/.test(path$1);
const normalizedPath = path.normalize(path$1);
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 = path.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(/\.[^/.]+$/, "");
}
exports.clean = clean;
exports.exists = exists;
exports.getRelativePath = getRelativePath;
exports.read = read;
exports.readSync = readSync;
exports.trimExtName = trimExtName;
exports.types_exports = types_exports;
exports.write = write;
//# sourceMappingURL=chunk-E4XLCCPK.cjs.map
//# sourceMappingURL=chunk-E4XLCCPK.cjs.map