prisma-zod-generator
Version:
Prisma 2+ generator to emit Zod schemas from your Prisma schema
66 lines • 2.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatFile = void 0;
exports.parserForFile = parserForFile;
const prettier_1 = __importDefault(require("prettier"));
const PARSER_BY_EXTENSION = {
'.ts': 'typescript',
'.tsx': 'typescript',
'.mts': 'typescript',
'.cts': 'typescript',
'.js': 'babel',
'.jsx': 'babel',
'.mjs': 'babel',
'.cjs': 'babel',
'.json': 'json',
'.md': 'markdown',
'.mdx': 'mdx',
'.yaml': 'yaml',
'.yml': 'yaml',
'.css': 'css',
'.scss': 'scss',
'.html': 'html',
'.graphql': 'graphql',
};
/**
* Pick a Prettier parser from the target file name.
*
* Returns `undefined` for formats Prettier has no parser for (`.sql`, `.py`,
* `.txt`, …). Callers must treat that as "write this content verbatim" — it is
* not an error. Historically every write was parsed as TypeScript, so any
* non-TS artefact threw and was dropped by writeFileSafely's catch.
*/
function parserForFile(filePath) {
const match = /\.[^.\\/]+$/.exec(filePath);
return match ? PARSER_BY_EXTENSION[match[0].toLowerCase()] : undefined;
}
/**
* Format `content` with Prettier.
*
* `filePath` selects the parser. When it is omitted the content is parsed as
* TypeScript, preserving the original behaviour for the core schema writers.
* Content that Prettier cannot parse is returned unchanged rather than throwing,
* so a formatting problem can never cost the caller its file.
*/
const formatFile = async (content, filePath) => {
const parser = filePath === undefined ? 'typescript' : parserForFile(filePath);
if (!parser) {
return content;
}
const resolved = await prettier_1.default.resolveConfig(process.cwd());
const formatOptions = resolved !== null && resolved !== void 0 ? resolved : {
trailingComma: 'all',
tabWidth: 2,
printWidth: 80,
bracketSpacing: true,
semi: true,
singleQuote: true,
useTabs: false,
};
return prettier_1.default.format(content, { ...formatOptions, parser });
};
exports.formatFile = formatFile;
//# sourceMappingURL=formatFile.js.map