apiveritas
Version:
Lightweight CLI tool for consumer-driven API contract testing via JSON schema and payload comparisons.
54 lines (53 loc) • 2.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InitCommand = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const PathResolver_1 = require("../core/utils/PathResolver");
class InitCommand {
constructor(logger) {
this.logger = logger;
const resolver = new PathResolver_1.PathResolver();
this.templatesDir = resolver.templatesDir();
}
async run(force = false) {
// Fixed target folder: 'apiveritas' under current working directory
const finalTarget = 'apiveritas';
const destPath = path_1.default.resolve(process.cwd(), finalTarget);
this.logger.info(`Using fixed target folder: "${finalTarget}"`);
this.logger.info(`Destination full path: "${destPath}"`);
this.logger.info(chalk_1.default.cyan(`\nInitializing ApiVeritas templates in: ${chalk_1.default.white(destPath)}\n`));
this.logger.info(`Templates directory resolved to: ${this.templatesDir}`);
try {
if (!await fs_extra_1.default.pathExists(this.templatesDir)) {
this.logger.error(chalk_1.default.red(`Templates directory not found at ${this.templatesDir}`));
process.exit(1);
}
const filesToCheck = await fs_extra_1.default.readdir(this.templatesDir);
const conflicts = [];
for (const file of filesToCheck) {
const destFilePath = path_1.default.join(destPath, file);
if (await fs_extra_1.default.pathExists(destFilePath)) {
conflicts.push(destFilePath);
}
}
if (conflicts.length > 0 && !force) {
this.logger.error(chalk_1.default.red(`The following files already exist:\n ${conflicts.join('\n ')}\nUse --force to overwrite.`));
process.exit(1);
}
await fs_extra_1.default.ensureDir(destPath);
await fs_extra_1.default.copy(this.templatesDir, destPath, { overwrite: force, errorOnExist: false });
this.logger.info(chalk_1.default.green(`✅ Templates initialized successfully in ${destPath}!\n`));
}
catch (error) {
const msg = error instanceof Error ? error.message : String(error);
this.logger.error(chalk_1.default.red(`Failed to initialize templates: ${msg}`));
process.exit(1);
}
}
}
exports.InitCommand = InitCommand;