UNPKG

prismixer

Version:

Allow you to create multiple Prisma schema files

64 lines 3.14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prismixer = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const glob_1 = __importDefault(require("glob")); const util_1 = require("util"); const prisma_ast_1 = require("@mrleebo/prisma-ast"); const lodash_1 = require("lodash"); const writeFile = (0, util_1.promisify)(fs_1.default.writeFile); const readFile = (0, util_1.promisify)(fs_1.default.readFile); function prismixer(options) { return __awaiter(this, void 0, void 0, function* () { let schemas = [ { type: "schema", list: [], }, ]; for (const input of options.input) { for (const file of glob_1.default.sync(input)) { const schema = yield readFile(file, { encoding: "utf-8" }); const blocks = (0, prisma_ast_1.getSchema)(schema); schemas[0].list = [...schemas[0].list, ...blocks.list]; } } schemas[0].list = schemas[0].list.reduce((acc, current) => { const block = acc.find((item) => item.type === "model" && current.type === "model" && item.name === current.name); if (!block) { return [...acc, Object.assign({}, current)]; } else { if (block.type === "model" && current.type === "model" && block.properties) { block.properties = (0, lodash_1.uniqBy)([...current.properties, ...block.properties], "name"); } return acc; } }, []); const sortGuide = ["datasource", "generator", "enum", "model"]; schemas[0].list = schemas[0].list.sort((it1, it2) => { return sortGuide.indexOf(it1.type) - sortGuide.indexOf(it2.type); }); schemas[0].list = schemas[0].list.filter((item) => item.type !== "break"); schemas[0].list = (0, lodash_1.uniqWith)(schemas[0].list, lodash_1.isEqual); let output = ` ${schemas.map((schema) => (0, prisma_ast_1.printSchema)(schema)).join("")} `; yield writeFile(path_1.default.join(process.cwd(), options.output), output); }); } exports.prismixer = prismixer; //# sourceMappingURL=prismixer.js.map