@synthart/synthlite
Version:
A fast, lightweight Gen AI powered synthetic data generator written in TypeScript. 🌞
121 lines (120 loc) • 4.77 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
const constants_1 = require("./constants");
const printer_1 = require("./printer");
const promises_1 = __importDefault(require("fs/promises"));
/**
* Validator: Validates CLI options and environment paths.
* @class Validator
*/
class Validator {
/**
* Validates the CLI options provided by the user.
*
* @param options - The CLI options to validate.
*/
static validateCLIOptions(options) {
return __awaiter(this, void 0, void 0, function* () {
const schema = options.schema;
if (!schema) {
printer_1.printer.error("schema file is required! Use -sc option to specify the schema file.");
process.exit(1);
}
const output = options.output;
if (!output) {
printer_1.printer.error("output file is required! Use -o option to specify the output file.");
process.exit(1);
}
yield Validator.createFileIfNotExists(output);
yield Validator.validateFilePath(output);
let rows = options.rows;
if (!rows) {
printer_1.printer.warn(`Number of rows not specified. Using default value: ${constants_1.Constants.DEFAULT_ROWS}`);
rows = constants_1.Constants.DEFAULT_ROWS.toString();
}
if (isNaN(rows)) {
printer_1.printer.error("Number of rows must be a valid number.");
process.exit(1);
}
let format = options.format;
if (!format) {
printer_1.printer.warn("Output format not specified. Using default value: json");
format = "json";
}
});
}
/**
* Validates the environment file path.
*
* @param envPath - The path to the environment file.
* @returns The final environment file path.
*/
static validateEnvPath(envPath) {
return __awaiter(this, void 0, void 0, function* () {
let finalEnvPath = envPath;
if (!envPath) {
printer_1.printer.warn("Environment file not specified. Using default value: .env");
finalEnvPath = ".env";
let error = "";
try {
yield promises_1.default.access(envPath);
}
catch (err) {
error =
"Error loading environment file. Please specify the environment file using -env option or add a .env file to current folder.";
}
if (error) {
printer_1.printer.error(error);
process.exit(1);
}
}
else {
yield Validator.validateFilePath(envPath);
}
return finalEnvPath;
});
}
/**
* Validates the file path.
* @param filePath - The path to the file to validate.
*/
static validateFilePath(filePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield promises_1.default.access(filePath);
}
catch (error) {
printer_1.printer.error(`File not found at path: ${filePath}. Please specify a valid file path.`);
process.exit(1);
}
});
}
/**
* Creates a file if it does not exist.
* @param filePath - The path to the file to create.
*/
static createFileIfNotExists(filePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield promises_1.default.access(filePath);
}
catch (error) {
yield promises_1.default.writeFile(filePath, "");
}
});
}
}
exports.Validator = Validator;