osr-mono/packages/osr-commons/commands/index.js
2025-01-29 18:17:03 +01:00

172 lines
7.1 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = exports.testSchema = exports.combineValidatorsOrUsingZod2 = exports.combineValidatorsOrUsingZod = exports.combineValidatorsOr = exports.validFilePath = exports.validUrl = exports.validString = exports.E_Filters = void 0;
const CLI = __importStar(require("yargs"));
const zod_1 = require("zod");
const index_1 = require("../index");
const logger_1 = require("../logger");
const path_1 = require("../shemas/path");
const url_1 = require("url");
const fs = __importStar(require("fs"));
exports.E_Filters = zod_1.z.enum(Object.keys({ test: 1, foo: 2 }));
exports.validString = zod_1.z.string().min(1, 'String cannot be empty');
exports.validUrl = zod_1.z.string().refine((url) => {
try {
new url_1.URL(url);
return true;
}
catch (e) {
console.log('error', e);
return false;
}
}, 'Invalid URL');
exports.validFilePath = zod_1.z.string().refine((path) => {
try {
fs.lstatSync(path).isFile();
return true;
}
catch (e) {
console.log('error', e);
return false;
}
}, 'Invalid file path');
// Function to combine validators using OR logic
const combineValidatorsOr = (validators) => {
return zod_1.z.string().refine((value) => {
const errors = [];
const isValid = validators.some((validator) => {
try {
validator.parse(value);
return true;
}
catch (err) {
errors.push(err.errors);
return false;
}
});
if (!isValid) {
throw new zod_1.z.ZodError(errors.flat());
}
return true;
}, 'Invalid value for all provided validators');
};
exports.combineValidatorsOr = combineValidatorsOr;
const combineValidatorsOrUsingZod = (validators) => {
return validators.reduce((acc, validator) => acc.or(validator));
};
exports.combineValidatorsOrUsingZod = combineValidatorsOrUsingZod;
const combineValidatorsOrUsingZod2 = (validators) => {
return validators.reduce((acc, validator) => {
return acc.or(validator).refine((value) => {
try {
acc.parse(value);
return true;
}
catch (errAcc) {
try {
validator.parse(value);
return true;
}
catch (errValidator) {
throw new zod_1.z.ZodError([...errAcc.errors, ...errValidator.errors]);
}
}
});
});
};
exports.combineValidatorsOrUsingZod2 = combineValidatorsOrUsingZod2;
const testSchema = () => {
try {
return zod_1.z.object({
//source: extend(z.string().optional().describe('Path to a source file').default('./package.json'), Templates.string),
source: (0, path_1.extend)(zod_1.z.string().describe('Path to a source file'), path_1.Templates.json),
// resource: combineValidatorsOrUsingZod2([validUrl, validFilePath]).describe('URL or file path to a resource'),
resource: exports.validFilePath.or(exports.validUrl).describe('URL or file path to a resource'),
//env_key: z.string().default('OSR-CONFIG').describe('Environment key to the config path'),
//filters: E_Filters.default('test').describe('An array of filters to apply to the response: JSON'),
//prompts: extend(z.string().optional().default('${OSR_ROOT}/osr-ai-templates/prompts/documents.json').describe('Path to prompts file, JSON - Array'),Templates.json)
});
}
catch (e) {
logger_1.logger.error('testSchema', e.issues);
}
};
exports.testSchema = testSchema;
let options = (yargs) => yargs.option('verb', {});
options = (yargs) => (0, index_1.toYargs)(yargs, (0, exports.testSchema)());
const register = (cli) => {
return cli.command('test', 'Testing stuff', options, (argv) => __awaiter(void 0, void 0, void 0, function* () {
if (argv.help) {
return;
}
try {
const s1 = (0, exports.testSchema)();
s1.parse(Object.assign({ source: null }, argv));
const d2 = s1.description;
const d3 = (0, index_1.enumerateHelpStrings)(s1, [], logger_1.logger);
//const j = zodToJsonSchema(s1)
const pSchema = zod_1.z.string().optional().describe('file output path, supports XLS|CSV|HTML');
const pathType = (0, path_1.extend)(pSchema, path_1.Templates.json);
const d = pathType.description;
const pathParsed = pathType.parse("${OSR_ROOT}/products/package.json");
const pathSchema = (0, path_1.test)();
const result = pathSchema.parse({
root: argv.root || "${OSR_ROOT}/products"
});
logger_1.logger.debug('test results', result);
}
catch (e) {
logger_1.logger.error('test', e.issues);
}
}));
};
exports.register = register;
(0, exports.register)(CLI);
const argv = CLI.argv;
if (argv.help) {
CLI.showHelp();
process.exit();
}
//# sourceMappingURL=index.js.map