prisma-json-types-generator
Version:
Changes JsonValues to your custom typescript type
93 lines • 3.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeclarationWriter = void 0;
exports.getNamespacePrelude = getNamespacePrelude;
const tslib_1 = require("tslib");
const promises_1 = tslib_1.__importDefault(require("node:fs/promises"));
const constants_1 = require("./constants");
const error_1 = require("./error");
const source_path_1 = require("./source-path");
const text_changes_1 = require("./text-changes");
/**
* A class to help with reading and writing the Prisma Client types file concurrently and
* converting positions indexes according with previous changes.
*/
class DeclarationWriter {
constructor(filepath, options, multifile, importFileExtension) {
this.filepath = filepath;
this.options = options;
this.multifile = multifile;
this.importFileExtension = importFileExtension;
/** The prisma's index.d.ts file content. */
this.content = '';
this.changes = [];
}
async template() {
let header;
const dotExt = this.importFileExtension ? `.${this.importFileExtension}` : '';
// Appends PJTG import statement
if (this.multifile) {
header = `import type * as PJTG from '../pjtg${dotExt}';`;
}
else {
header = await getNamespacePrelude({
namespace: this.options.namespace,
isNewClient: false,
dotExt
});
}
// wraps into extra lines to visually split our code from the rest
header = `\n${header}\n`;
const firstNonCommentLine = (0, source_path_1.findFirstCodeIndex)(this.content);
// Appends after all initial comments to preserve comments like `@ts-nocheck`
return (this.content.slice(0, firstNonCommentLine) + header + this.content.slice(firstNonCommentLine));
}
/** Loads the original file of sourcePath into memory. */
async load() {
if (!(await promises_1.default.stat(this.filepath))) {
throw new error_1.PrismaJsonTypesGeneratorError('Tried to load a file that does not exist', {
filepath: this.filepath
});
}
if (this.changes.length) {
throw new error_1.PrismaJsonTypesGeneratorError('Tried to load a file that has already been changed', { filepath: this.filepath, changeset: this.changes });
}
this.content = await promises_1.default.readFile(this.filepath, 'utf-8');
}
/** Save the original file of sourcePath with the content's contents */
async save() {
// Apply all changes to content
if (this.changes.length) {
this.content = (0, text_changes_1.applyTextChanges)(this.content, this.changes);
this.changes.length = 0;
}
// Apply template after all changes
this.content = await this.template();
// Writes it into the disk
await promises_1.default.writeFile(this.filepath, this.content);
}
/**
* Stack change to be applied before declaration save
*/
replace(start, end, text) {
// Adds the change to the list
this.changes.push({
start,
end,
text
});
}
}
exports.DeclarationWriter = DeclarationWriter;
async function getNamespacePrelude({ namespace, isNewClient, dotExt }) {
let prelude = await promises_1.default.readFile(constants_1.NAMESPACE_PATH, 'utf-8');
// Removes trailing spaces
prelude = prelude.trim();
// Replaces the namespace with the provided namespace
prelude = prelude.replace(/\$\$NAMESPACE\$\$/g, namespace);
if (isNewClient) {
prelude = `import * as Prisma from './internal/prismaNamespace${dotExt}';\n${prelude}`;
}
return prelude;
}
//# sourceMappingURL=declaration-writer.js.map