@n1k1t/unit-generator
Version:
Coverage based unit tests AI generator
101 lines • 3.87 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.File = exports.FileBuildError = exports.FileReplaceError = void 0;
const path_1 = __importDefault(require("path"));
const promises_1 = __importDefault(require("fs/promises"));
class FileReplaceError extends Error {
constructor(source, reason) {
super(`Cannot replace content in the file. Reason: "${reason}". Provided source lines: "${source}"`);
this.source = source;
}
}
exports.FileReplaceError = FileReplaceError;
class FileBuildError extends Error {
constructor(reason) {
super(`Cannot build file. Reason: ${reason}`);
}
}
exports.FileBuildError = FileBuildError;
class File {
constructor(provided) {
this.provided = provided;
this.path = this.provided.path;
this.dir = path_1.default.dirname(this.path);
this.cwd = this.provided.cwd;
this.content = this.provided.content;
this.lang = this.provided.lang;
this.options = this.provided.options;
}
append(content) {
if (!content.length) {
return this;
}
this.content = `${this.content}\n${content}`;
return this;
}
prepend(content) {
if (!content.length) {
return this;
}
this.content = `${content}\n${this.content}`;
return this;
}
replace(source, target, options) {
const segments = this.content.split(source);
if (this.options?.strict ?? options?.strict) {
if (segments.length === 1) {
throw new FileReplaceError(source, 'File does not have provided lines of content');
}
if (segments.length > 2) {
throw new FileReplaceError(source, [
'File has more than one potential replacement source',
'Try to capture more lines of the source file',
].join('. '));
}
}
this.content = segments.join(target);
return this;
}
/** Writes content to file */
async write(content) {
if (content !== undefined) {
this.content = content;
}
await promises_1.default.mkdir(path_1.default.join(this.cwd, this.dir), { recursive: true });
await promises_1.default.writeFile(path_1.default.join(this.cwd, this.path), this.content, 'utf8');
}
/** Refreshes and stores source content from file */
async refresh() {
this.content = await promises_1.default.readFile(path_1.default.join(this.cwd, this.path), 'utf8');
}
async remove() {
await promises_1.default.rm(path_1.default.join(this.cwd, this.path));
}
static async build(location, options) {
const target = typeof location === 'string' ? location : path_1.default.join(...location);
const cwd = options?.cwd ?? process.cwd();
const stats = await promises_1.default.stat(path_1.default.join(cwd, target)).catch(() => null);
if (stats?.isDirectory()) {
throw new FileBuildError('Cannot read directory');
}
if (options?.strict) {
if (!stats && options.state === 'existent') {
throw new FileBuildError('File is not exists');
}
if (stats && options.state === 'new') {
throw new FileBuildError('File already exists');
}
}
return new File({
cwd,
path: target,
lang: path_1.default.extname(target).substring(1),
content: stats ? await promises_1.default.readFile(path_1.default.join(cwd, target), 'utf8') : '',
});
}
}
exports.File = File;
//# sourceMappingURL=file.js.map