@sidekick-coder/db
Version:
Cli Tool to manipulate data from diferent sources
220 lines (215 loc) • 6.65 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { parse as parse$1, stringify as stringify$1 } from 'yaml';
// src/utils/filesystem.ts
// src/utils/tryCatch.ts
async function tryCatch(tryer) {
try {
const result = await tryer();
return [result, null];
} catch (error) {
return [null, error];
}
}
tryCatch.sync = function(tryer) {
try {
const result = tryer();
return [result, null];
} catch (error) {
return [null, error];
}
};
var parse = parse$1;
var stringify = stringify$1;
var YAML = {
parse,
stringify
};
// src/utils/filesystem.ts
var locks = /* @__PURE__ */ new Set();
function awaitLock(path2, timeout = 1e3) {
return new Promise((resolve2, reject) => {
const interval = setInterval(() => {
if (!locks.has(path2)) {
clearInterval(interval);
resolve2();
}
}, 100);
setTimeout(() => {
clearInterval(interval);
reject(new Error("Timeout"));
}, timeout);
});
}
async function fileExists(path2) {
const [, error] = await tryCatch(() => fs.promises.access(path2));
return error ? false : true;
}
async function readFile(path2) {
const [content, error] = await tryCatch(() => fs.promises.readFile(path2));
if (error) {
return null;
}
return new Uint8Array(content);
}
readFile.text = async function(filepath, defaultValue = "") {
const content = await readFile(filepath);
if (!content) {
return defaultValue;
}
return new TextDecoder().decode(content);
};
readFile.json = async function(path2, options) {
const content = await readFile.text(path2);
if (!content) {
return (options == null ? void 0 : options.default) || null;
}
const [json, error] = await tryCatch(() => JSON.parse(content, options == null ? void 0 : options.reviver));
return error ? (options == null ? void 0 : options.default) || null : json;
};
readFile.yaml = async function(path2, options) {
const content = await readFile.text(path2);
if (!content) {
return (options == null ? void 0 : options.default) || null;
}
const [yml, error] = await tryCatch(() => YAML.parse(content, options == null ? void 0 : options.reviver));
return error ? (options == null ? void 0 : options.default) || null : yml;
};
function readFileSync(path2) {
const [content, error] = tryCatch.sync(() => fs.readFileSync(path2));
if (error) {
return null;
}
return new Uint8Array(content);
}
readFileSync.text = function(filepath, defaultValue = "") {
const content = readFileSync(filepath);
if (!content) {
return defaultValue;
}
return new TextDecoder().decode(content);
};
readFileSync.json = function(path2, options) {
const content = readFileSync.text(path2);
if (!content) {
return (options == null ? void 0 : options.default) || null;
}
const [json, error] = tryCatch.sync(() => JSON.parse(content, options == null ? void 0 : options.reviver));
return error ? (options == null ? void 0 : options.default) || null : json;
};
readFileSync.yaml = function(path2, options) {
const content = readFileSync.text(path2);
if (!content) {
return (options == null ? void 0 : options.default) || null;
}
const [yml, error] = tryCatch.sync(() => YAML.parse(content, options == null ? void 0 : options.parseOptions));
return error ? (options == null ? void 0 : options.default) || null : yml;
};
async function readDir(path2, options) {
const [files, error] = await tryCatch(
() => fs.promises.readdir(path2, {
withFileTypes: true
})
);
if (error) {
return [];
}
let result = files;
if (options == null ? void 0 : options.onlyFiles) {
result = files.filter((file) => file.isFile());
}
if (options == null ? void 0 : options.onlyDirectories) {
result = files.filter((file) => file.isDirectory());
}
return result.map((file) => file.name);
}
function readDirSync(path2, options) {
const [files, error] = tryCatch.sync(
() => fs.readdirSync(path2, {
withFileTypes: true
})
);
if (error) {
return [];
}
let result = files;
if (options == null ? void 0 : options.onlyFiles) {
result = files.filter((file) => file.isFile());
}
if (options == null ? void 0 : options.onlyDirectories) {
result = files.filter((file) => file.isDirectory());
}
return result.map((file) => file.name);
}
function copy(source, destination, options) {
return fs.promises.cp(source, destination, options);
}
async function mkdir(filepath, options) {
if (await fileExists(filepath)) return;
if (options == null ? void 0 : options.recursive) {
const parent = path.dirname(filepath);
await mkdir(parent, options);
}
await fs.promises.mkdir(filepath, options);
}
async function writeFile(filename, content, options) {
if (locks.has(filename)) {
await awaitLock(filename);
}
locks.add(filename);
if (options == null ? void 0 : options.recursive) {
const parent = path.dirname(filename);
await mkdir(parent, { recursive: true });
}
const [, error] = await tryCatch(() => fs.promises.writeFile(filename, content));
locks.delete(filename);
if (error) {
throw error;
}
}
writeFile.text = async function(filename, content, options) {
await writeFile(filename, new TextEncoder().encode(content), options);
};
writeFile.json = async function(filename, content, options) {
await writeFile.text(filename, JSON.stringify(content, null, 2), options);
};
function writeFileSync(filename, content, options) {
if (options == null ? void 0 : options.recursive) {
const parent = path.dirname(filename);
fs.mkdirSync(parent, { recursive: true });
}
fs.writeFileSync(filename, content);
}
writeFileSync.text = function(filename, content, options) {
writeFileSync(filename, new TextEncoder().encode(content), options);
};
writeFileSync.json = function(filename, content, options) {
writeFileSync.text(filename, JSON.stringify(content, null, 2), options);
};
function resolve(url, ...args) {
const __dirname = path.dirname(fileURLToPath(url));
return path.resolve(__dirname, ...args);
}
function removeFile(path2) {
return fs.promises.rm(path2);
}
function removeFileSync(path2) {
return fs.rmSync(path2);
}
var filesystem = {
path,
resolve,
exists: fileExists,
read: readFile,
readSync: readFileSync,
readdir: readDir,
readdirSync: readDirSync,
copy,
write: writeFile,
writeSync: writeFileSync,
mkdir,
remove: removeFile,
removeSync: removeFileSync
};
export { copy, fileExists, filesystem, mkdir, readDir, readDirSync, readFile, readFileSync, removeFile, removeFileSync, resolve, writeFile, writeFileSync };