@sidekick-coder/db
Version:
Cli Tool to manipulate data from diferent sources
213 lines (204 loc) • 6.04 kB
JavaScript
;
var path2 = require('path');
var valibot = require('valibot');
var fs = require('fs');
require('url');
var yaml = require('yaml');
var qs = require('qs');
var inquirer = require('@inquirer/prompts');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var path2__default = /*#__PURE__*/_interopDefault(path2);
var valibot__namespace = /*#__PURE__*/_interopNamespace(valibot);
var fs__default = /*#__PURE__*/_interopDefault(fs);
var qs__default = /*#__PURE__*/_interopDefault(qs);
var inquirer__namespace = /*#__PURE__*/_interopNamespace(inquirer);
// src/core/filesystem/createPathNode.ts
function createPathNode() {
return {
resolve: (...args) => path2__default.default.resolve(...args),
join: (...args) => path2__default.default.join(...args),
dirname: (args) => path2__default.default.dirname(args),
basename: (args) => path2__default.default.basename(args)
};
}
// 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
};
function readFileSync(path4) {
const [content, error] = tryCatch.sync(() => fs__default.default.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 path2.resolve(path2.dirname(folder), value);
}
return value;
};
}
var schema = valibot__namespace.optional(
valibot__namespace.pipe(
valibot__namespace.any(),
valibot__namespace.transform((value) => {
if (typeof value == "object") {
return value;
}
if (/\.yml$/.test(value)) {
const file = value.replace(/^@/, "");
const folder = path2.dirname(file);
return filesystem.readSync.yaml(value.replace(/^@/, ""), {
reviver: createReviver(folder)
});
}
if (typeof value == "string" && value.includes("=")) {
const result = qs__default.default.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__namespace.record(valibot__namespace.string(), valibot__namespace.any())
)
);
var stringList = valibot__namespace.pipe(
valibot__namespace.any(),
valibot__namespace.transform((value) => {
if (typeof value === "string") {
return value.split(",");
}
if (Array.isArray(value)) {
return value;
}
}),
valibot__namespace.array(valibot__namespace.string())
);
function array2(s) {
return valibot__namespace.pipe(
v2.union([v2.array(s), s]),
valibot__namespace.transform((value) => Array.isArray(value) ? value : [value]),
valibot__namespace.array(s)
);
}
function path3(dirname2, path4 = createPathNode()) {
return valibot__namespace.pipe(
valibot__namespace.string(),
valibot__namespace.transform((value) => path4.resolve(dirname2, value))
);
}
function uint8() {
return valibot__namespace.pipe(
valibot__namespace.any(),
valibot__namespace.check((value) => value instanceof Uint8Array),
valibot__namespace.transform((value) => value)
);
}
var prompts = {
password: (options) => valibot__namespace.optionalAsync(valibot__namespace.string(), () => {
return inquirer__namespace.password({
message: "Enter password",
...options
});
})
};
var extras = {
array: array2,
vars: schema,
stringList,
path: path3,
uint8,
number: valibot__namespace.pipe(
valibot__namespace.any(),
valibot__namespace.transform(Number),
valibot__namespace.check((n) => !isNaN(n)),
valibot__namespace.number()
)
};
var vWithExtras = {
...valibot__namespace,
extras,
prompts
};
var v2 = vWithExtras;
// src/providers/vault/config.ts
var schema2 = (dirname2, path4 = createPathNode()) => v2.object({
format: v2.optional(v2.string(), "markdown"),
path: v2.extras.path(dirname2, path4),
id_strategy: v2.optional(
v2.object({
name: v2.string(),
options: v2.optional(v2.any())
}),
{ name: "incremental" }
)
});
exports.schema = schema2;