UNPKG

@sidekick-coder/db

Version:

Cli Tool to manipulate data from diferent sources

161 lines (154 loc) 4.21 kB
import * as valibot from 'valibot'; import { dirname, resolve } from 'path'; import fs from 'fs'; import 'url'; import { parse as parse$1, stringify as stringify$1 } from 'yaml'; import qs from 'qs'; import '@inquirer/prompts'; // src/core/validator/valibot.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 }; function readFileSync(path4) { const [content, error] = tryCatch.sync(() => fs.readFileSync(path4)); 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(path4, options) { const content = readFileSync.text(path4); 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(path4, options) { const content = readFileSync.text(path4); 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; }; var filesystem = { readSync: readFileSync}; function createReviver(folder) { return (_, value) => { if (typeof value == "string" && value.startsWith("./")) { return resolve(dirname(folder), value); } return value; }; } valibot.optional( valibot.pipe( valibot.any(), valibot.transform((value) => { if (typeof value == "object") { return value; } if (/\.yml$/.test(value)) { const file = value.replace(/^@/, ""); const folder = dirname(file); return filesystem.readSync.yaml(value.replace(/^@/, ""), { reviver: createReviver(folder) }); } if (typeof value == "string" && value.includes("=")) { const result = qs.parse(value, { allowEmptyArrays: true }); return result; } if (typeof value == "string" && value.startsWith("{")) { return JSON.parse(value); } if (typeof value == "string" && value.startsWith("[")) { return JSON.parse(value); } return value; }), valibot.record(valibot.string(), valibot.any()) ) ); // src/core/validator/valibot.ts valibot.pipe( valibot.any(), valibot.transform((value) => { if (typeof value === "string") { return value.split(","); } if (Array.isArray(value)) { return value; } }), valibot.array(valibot.string()) ); ({ number: valibot.pipe( valibot.any(), valibot.transform(Number), valibot.check((n) => !isNaN(n)), valibot.number() ) }); // src/core/idStrategy/createIncremental.ts function pad(value, size) { return value.toString().padStart(size, "0"); } // src/providers/file/__tests__/factories.ts function createFactories(options) { const { filesystem: filesystem2, parser, root } = options; const resolve3 = filesystem2.path.resolve; function makeItem(payload = {}) { const id = pad(filesystem2.readdirSync(root).length, 2); const filename = resolve3(root, `${id}.${parser.ext}`); const raw = parser.stringify(payload); filesystem2.writeSync.text(filename, raw); return { id, filename, raw, ...parser.parse(raw) }; } function makeManyItems(count = 5, payload) { const items = []; for (let i = 0; i < count; i++) { items.push(makeItem(payload)); } return items; } return { makeItem, makeManyItems }; } export { createFactories };