UNPKG

@sidekick-coder/db

Version:

Cli Tool to manipulate data from diferent sources

265 lines (259 loc) 7.25 kB
import minimist from 'minimist'; import * as valibot from 'valibot'; import path, { 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 * as inquirer from '@inquirer/prompts'; import { merge, omit } from 'lodash-es'; // src/utils/parseOptions.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; }; } var schema = 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()) ) ); function createPathNode() { return { resolve: (...args) => path.resolve(...args), join: (...args) => path.join(...args), dirname: (args) => path.dirname(args), basename: (args) => path.basename(args) }; } // src/core/validator/valibot.ts var stringList = valibot.pipe( valibot.any(), valibot.transform((value) => { if (typeof value === "string") { return value.split(","); } if (Array.isArray(value)) { return value; } }), valibot.array(valibot.string()) ); function array2(s) { return valibot.pipe( v2.union([v2.array(s), s]), valibot.transform((value) => Array.isArray(value) ? value : [value]), valibot.array(s) ); } function path3(dirname2, path4 = createPathNode()) { return valibot.pipe( valibot.string(), valibot.transform((value) => path4.resolve(dirname2, value)) ); } function uint8() { return valibot.pipe( valibot.any(), valibot.check((value) => value instanceof Uint8Array), valibot.transform((value) => value) ); } var prompts = { password: (options) => valibot.optionalAsync(valibot.string(), () => { return inquirer.password({ message: "Enter password", ...options }); }) }; var extras = { array: array2, vars: schema, stringList, path: path3, uint8, number: valibot.pipe( valibot.any(), valibot.transform(Number), valibot.check((n) => !isNaN(n)), valibot.number() ) }; var vWithExtras = { ...valibot, extras, prompts }; var v2 = vWithExtras; // src/core/validator/validate.ts function validate(cb, payload) { const schema2 = typeof cb === "function" ? cb(v2) : cb; const { output, issues, success } = v2.safeParse(schema2, payload); if (!success) { const flatten = v2.flatten(issues); const messages = []; if (flatten.root) { messages.push(...flatten.root); } if (flatten.nested) { Object.entries(flatten.nested).forEach((entry) => { const [key, value] = entry; messages.push(...value.map((v3) => `${key}: ${v3}`)); }); } const message = messages.length ? messages.join(", ") : "Validation failed"; const error = new Error(message); error.name = "ValidationError"; Object.assign(error, { messages }); throw error; } return output; } validate.async = async function(cb, payload) { let schema2; if (typeof cb === "function") { schema2 = cb(v2); } else { schema2 = cb; } const { output, issues, success } = await v2.safeParseAsync(schema2, payload); if (!success) { const error = new Error("Validation failed"); const flatten = v2.flatten(issues); const details = { ...flatten.root, ...flatten.nested }; Object.assign(error, { details }); throw error; } return output; }; function parseOptions(args, options) { var _a, _b, _c; const flags = minimist(args, { string: ["where", "view", "data", "render", "id"], alias: { where: "w", view: "v", data: "d", render: "r", render_options: "ro", sortBy: ["sort-by"], sortDesc: ["sort-desc"] } }); const result = validate( (v3) => { var _a2, _b2; return v3.objectWithRest( { view: v3.optional(v3.string(), (_b2 = (_a2 = options == null ? void 0 : options.databaseDefinition) == null ? void 0 : _a2.view) == null ? void 0 : _b2.default), render: v3.optional(v3.string()), data: v3.optional(v3.extras.vars), where: v3.optional(v3.extras.vars), include: v3.optional(v3.extras.stringList), exclude: v3.optional(v3.extras.stringList), render_options: v3.optional(v3.extras.vars, {}), sortBy: v3.optional(v3.extras.stringList), sortDesc: v3.optional(v3.extras.array(v3.boolean())) }, v3.any() ); }, flags ); if (result.view && (options == null ? void 0 : options.databaseDefinition)) { const view = (_c = (_b = (_a = options.databaseDefinition) == null ? void 0 : _a.view) == null ? void 0 : _b.sources) == null ? void 0 : _c.find((v3) => v3.name === result.view); merge(result, omit(view, "name")); } return result; } export { parseOptions };