nestjs-reverse-engineering
Version:
A powerful TypeScript/NestJS library for database reverse engineering, entity generation, and CRUD operations
267 lines • 7.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileBuilder = void 0;
/* eslint-disable prettier/prettier */
const fs = require("fs");
const path = require("path");
class FileBuilder {
constructor(options = {
outputPath: '',
indentation: ' ',
lineEnding: '\n'
}) {
this.options = options;
this.content = [];
this.indentLevel = 0;
this.indentSize = options.indentation || ' ';
this.lineEnding = options.lineEnding || '\n';
}
/**
* Add a line of content with proper indentation
*/
addLine(content = '') {
if (content.trim() === '') {
this.content.push('');
}
else {
const indent = this.indentSize.repeat(this.indentLevel);
this.content.push(indent + content);
}
return this;
}
/**
* Add multiple lines of content
*/
addLines(lines) {
lines.forEach(line => this.addLine(line));
return this;
}
/**
* Add raw content without indentation
*/
addRaw(content) {
this.content.push(content);
return this;
}
/**
* Add a comment block
*/
addComment(comment, style = 'line') {
if (style === 'block') {
this.addLine('/**');
comment.split('\n').forEach(line => {
this.addLine(` * ${line}`);
});
this.addLine(' */');
}
else {
comment.split('\n').forEach(line => {
this.addLine(`// ${line}`);
});
}
return this;
}
/**
* Add an import statement
*/
addImport(imports, from) {
if (Array.isArray(imports)) {
if (imports.length === 1) {
this.addLine(`import { ${imports[0]} } from '${from}';`);
}
else {
this.addLine(`import {`);
this.increaseIndent();
imports.forEach((imp, index) => {
const comma = index < imports.length - 1 ? ',' : '';
this.addLine(`${imp}${comma}`);
});
this.decreaseIndent();
this.addLine(`} from '${from}';`);
}
}
else {
this.addLine(`import { ${imports} } from '${from}';`);
}
return this;
}
/**
* Add a default import
*/
addDefaultImport(name, from) {
this.addLine(`import ${name} from '${from}';`);
return this;
}
/**
* Add namespace import
*/
addNamespaceImport(name, from) {
this.addLine(`import * as ${name} from '${from}';`);
return this;
}
/**
* Start a block (class, function, interface, etc.)
*/
startBlock(declaration) {
this.addLine(declaration + ' {');
this.increaseIndent();
return this;
}
/**
* End a block
*/
endBlock(semicolon = false) {
this.decreaseIndent();
this.addLine('}' + (semicolon ? ';' : ''));
return this;
}
/**
* Add a property declaration
*/
addProperty(name, type, options = {}) {
if (options.comment) {
this.addComment(options.comment);
}
if (options.decorators) {
options.decorators.forEach(decorator => {
this.addLine(decorator);
});
}
let declaration = '';
// Access modifiers
if (options.public)
declaration += 'public ';
if (options.private)
declaration += 'private ';
if (options.protected)
declaration += 'protected ';
// Other modifiers
if (options.static)
declaration += 'static ';
if (options.readonly)
declaration += 'readonly ';
// Property name and type
declaration += `${name}${options.optional ? '?' : ''}: ${type}`;
// Default value
if (options.defaultValue) {
declaration += ` = ${options.defaultValue}`;
}
this.addLine(declaration + ';');
return this;
}
/**
* Add a method declaration
*/
addMethod(name, parameters, returnType, options = {}) {
if (options.comment) {
this.addComment(options.comment);
}
if (options.decorators) {
options.decorators.forEach(decorator => {
this.addLine(decorator);
});
}
let declaration = '';
// Access modifiers
if (options.public)
declaration += 'public ';
if (options.private)
declaration += 'private ';
if (options.protected)
declaration += 'protected ';
// Other modifiers
if (options.static)
declaration += 'static ';
if (options.abstract)
declaration += 'abstract ';
if (options.async)
declaration += 'async ';
// Method signature
const params = parameters.map(p => {
let param = `${p.name}${p.optional ? '?' : ''}: ${p.type}`;
if (p.defaultValue)
param += ` = ${p.defaultValue}`;
return param;
}).join(', ');
declaration += `${name}(${params}): ${returnType}`;
if (options.abstract) {
this.addLine(declaration + ';');
}
else {
this.startBlock(declaration);
if (options.body) {
this.addLines(options.body);
}
this.endBlock();
}
return this;
}
/**
* Increase indentation level
*/
increaseIndent() {
this.indentLevel++;
return this;
}
/**
* Decrease indentation level
*/
decreaseIndent() {
if (this.indentLevel > 0) {
this.indentLevel--;
}
return this;
}
/**
* Add an empty line
*/
addEmptyLine() {
this.content.push('');
return this;
}
/**
* Get the current content as string
*/
getContent() {
return this.content.join(this.lineEnding);
}
/**
* Clear all content
*/
clear() {
this.content = [];
this.indentLevel = 0;
return this;
}
/**
* Write content to file
*/
async writeToFile(filePath) {
const targetPath = filePath || this.options.outputPath;
if (!targetPath) {
throw new Error('No output path specified');
}
// Create directories if needed
if (this.options.createDirectories !== false) {
const dir = path.dirname(targetPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
// Check if file exists and overwrite option
if (fs.existsSync(targetPath) && !this.options.overwrite) {
throw new Error(`File ${targetPath} already exists and overwrite is disabled`);
}
// Write file
const content = this.getContent();
fs.writeFileSync(targetPath, content, 'utf8');
}
/**
* Create a new FileBuilder with the same options
*/
clone() {
return new FileBuilder({ ...this.options });
}
}
exports.FileBuilder = FileBuilder;
//# sourceMappingURL=file-builder.js.map