UNPKG

@sidekick-coder/db

Version:

Cli Tool to manipulate data from diferent sources

239 lines (231 loc) 7.39 kB
'use strict'; var fs = require('fs'); var path = require('path'); var url = require('url'); var yaml = require('yaml'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var fs__default = /*#__PURE__*/_interopDefault(fs); var path__default = /*#__PURE__*/_interopDefault(path); // 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 = yaml.parse; var stringify = yaml.stringify; 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__default.default.promises.access(path2)); return error ? false : true; } async function readFile(path2) { const [content, error] = await tryCatch(() => fs__default.default.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__default.default.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__default.default.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__default.default.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__default.default.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__default.default.dirname(filepath); await mkdir(parent, options); } await fs__default.default.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__default.default.dirname(filename); await mkdir(parent, { recursive: true }); } const [, error] = await tryCatch(() => fs__default.default.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__default.default.dirname(filename); fs__default.default.mkdirSync(parent, { recursive: true }); } fs__default.default.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$1, ...args) { const __dirname = path__default.default.dirname(url.fileURLToPath(url$1)); return path__default.default.resolve(__dirname, ...args); } function removeFile(path2) { return fs__default.default.promises.rm(path2); } function removeFileSync(path2) { return fs__default.default.rmSync(path2); } var filesystem = { path: path__default.default, resolve, exists: fileExists, read: readFile, readSync: readFileSync, readdir: readDir, readdirSync: readDirSync, copy, write: writeFile, writeSync: writeFileSync, mkdir, remove: removeFile, removeSync: removeFileSync }; exports.copy = copy; exports.fileExists = fileExists; exports.filesystem = filesystem; exports.mkdir = mkdir; exports.readDir = readDir; exports.readDirSync = readDirSync; exports.readFile = readFile; exports.readFileSync = readFileSync; exports.removeFile = removeFile; exports.removeFileSync = removeFileSync; exports.resolve = resolve; exports.writeFile = writeFile; exports.writeFileSync = writeFileSync;