node-apis
Version:
🚀 Advanced TypeScript API generator with clean architecture, comprehensive testing, and automatic formatting. Generate production-ready Node.js APIs with complete integration test suites.
86 lines • 3.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatFile = exports.formatDirectory = exports.formatGeneratedFiles = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
* Formats TypeScript files using Prettier
*/
const formatGeneratedFiles = async (filePaths) => {
if (filePaths.length === 0)
return;
try {
// Check if prettier is available
const prettierPath = path_1.default.join(process.cwd(), 'node_modules', '.bin', 'prettier');
const prettierExists = await fs_extra_1.default.pathExists(prettierPath);
if (!prettierExists) {
console.warn('⚠️ Prettier not found. Skipping code formatting.');
return;
}
// Format each file
for (const filePath of filePaths) {
try {
if (await fs_extra_1.default.pathExists(filePath)) {
await execAsync(`"${prettierPath}" --write "${filePath}"`);
}
}
catch (error) {
console.warn(`⚠️ Failed to format ${filePath}:`, error instanceof Error ? error.message : 'Unknown error');
}
}
console.log(`✨ Formatted ${filePaths.length} generated files`);
}
catch (error) {
console.warn('⚠️ Code formatting failed:', error instanceof Error ? error.message : 'Unknown error');
}
};
exports.formatGeneratedFiles = formatGeneratedFiles;
/**
* Formats all TypeScript files in a directory
*/
const formatDirectory = async (directoryPath) => {
try {
const prettierPath = path_1.default.join(process.cwd(), 'node_modules', '.bin', 'prettier');
const prettierExists = await fs_extra_1.default.pathExists(prettierPath);
if (!prettierExists) {
console.warn('⚠️ Prettier not found. Skipping code formatting.');
return;
}
// Format all TypeScript files in the directory
const pattern = path_1.default.join(directoryPath, '**/*.ts');
await execAsync(`"${prettierPath}" --write "${pattern}"`);
console.log(`✨ Formatted all TypeScript files in ${directoryPath}`);
}
catch (error) {
console.warn('⚠️ Directory formatting failed:', error instanceof Error ? error.message : 'Unknown error');
}
};
exports.formatDirectory = formatDirectory;
/**
* Formats a single file
*/
const formatFile = async (filePath) => {
try {
const prettierPath = path_1.default.join(process.cwd(), 'node_modules', '.bin', 'prettier');
const prettierExists = await fs_extra_1.default.pathExists(prettierPath);
if (!prettierExists) {
console.warn('⚠️ Prettier not found. Skipping code formatting.');
return;
}
if (await fs_extra_1.default.pathExists(filePath)) {
await execAsync(`"${prettierPath}" --write "${filePath}"`);
console.log(`✨ Formatted ${filePath}`);
}
}
catch (error) {
console.warn(`⚠️ Failed to format ${filePath}:`, error instanceof Error ? error.message : 'Unknown error');
}
};
exports.formatFile = formatFile;
//# sourceMappingURL=formatter.service.js.map