import * as CLI from 'yargs' import { z } from 'zod' import { enumerateHelpStrings, toYargs } from '../index' import { logger } from '../logger' import { extend, test, Templates } from '../shemas/path' import zodToJsonSchema from 'zod-to-json-schema' import { URL } from 'url' import * as fs from 'fs' export const E_Filters = z.enum(Object.keys({ test: 1, foo: 2 }) as any) export const validString = z.string().min(1, 'String cannot be empty'); export const validUrl = z.string().refine((url) => { try { new URL(url); return true; } catch (e) { console.log('error', e) return false; } }, 'Invalid URL'); export const validFilePath = 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 export const combineValidatorsOr = (validators: z.ZodTypeAny[]) => { return 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 z.ZodError(errors.flat()); } return true; }, 'Invalid value for all provided validators'); }; export const combineValidatorsOrUsingZod = (validators: z.ZodTypeAny[]) => { return validators.reduce((acc, validator) => acc.or(validator)); }; export const combineValidatorsOrUsingZod2 = (validators: z.ZodTypeAny[]) => { 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 z.ZodError([...errAcc.errors, ...errValidator.errors]); } } }) }) } export const testSchema = () => { try { return z.object({ //source: extend(z.string().optional().describe('Path to a source file').default('./package.json'), Templates.string), source: extend(z.string().describe('Path to a source file'), Templates.json), // resource: combineValidatorsOrUsingZod2([validUrl, validFilePath]).describe('URL or file path to a resource'), resource: validFilePath.or(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.error('testSchema', e.issues) } } let options = (yargs: CLI.Argv) => yargs.option('verb', {}) options = (yargs: CLI.Argv) => toYargs(yargs, testSchema()) export const register = (cli: CLI.Argv) => { return cli.command('test', 'Testing stuff', options, async (argv: CLI.Arguments) => { if (argv.help) { return; } try { const s1 = testSchema() s1.parse({ source: null, ...argv }) const d2 = s1.description const d3 = enumerateHelpStrings(s1, [], logger) //const j = zodToJsonSchema(s1) const pSchema = z.string().optional().describe('file output path, supports XLS|CSV|HTML') const pathType = extend(pSchema, Templates.json) const d = pathType.description const pathParsed = pathType.parse("${OSR_ROOT}/products/package.json") const pathSchema = test() const result = pathSchema.parse({ root: argv.root || "${OSR_ROOT}/products" }) logger.debug('test results', result) } catch (e) { logger.error('test', e.issues) } }) } register(CLI) const argv: any = CLI.argv if (argv.help) { CLI.showHelp() process.exit() }