UNPKG

@practica/create-node-app

Version:

Create Node.js app that is packed with best practices AND strive for simplicity

66 lines (65 loc) 3.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.chooseORM = void 0; const fsExtra = __importStar(require("fs-extra")); const path_1 = __importDefault(require("path")); const string_manipulation_helpers_1 = require("../string-manipulation-helpers"); // This feature allows the user to choose which ORM (DB mapper) to use async function chooseORM(generatedAppRoot, options) { const microservicePath = (0, string_manipulation_helpers_1.getMicroservicePath)(generatedAppRoot); if (options.ORM === "prisma") { await adjustTheCodeToPrismaORM(microservicePath); } else if (options.ORM === "sequelize") { await adjustTheCodeToSequelizeORM(microservicePath); } } exports.chooseORM = chooseORM; async function adjustTheCodeToPrismaORM(microservicePath) { // Remove the sequelize folder and all the phrased from package.json const sequelizeFolder = path_1.default.join(microservicePath, "data-access"); await fsExtra.rm(sequelizeFolder, { recursive: true }); const prismaFolder = path_1.default.join(microservicePath, "data-access-prisma"); const prismaFolderNewName = path_1.default.join(microservicePath, "data-access"); await fsExtra.rename(prismaFolder, prismaFolderNewName); const packageJSONPath = path_1.default.join(microservicePath, "package.json"); await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"(.*?)sequelize(.*?)": "(.*)"(,?)\n', ""); await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"db:migrate":().*,\n', ""); // Now rename the Prisma things to be the default await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, "db:migrate:prisma", "db:migrate"); await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, "data-access-prisma/prisma/schema.prisma", "data-access/prisma/schema.prisma"); } async function adjustTheCodeToSequelizeORM(microservicePath) { const prismaFolder = path_1.default.join(microservicePath, "data-access-prisma"); await fsExtra.rm(prismaFolder, { recursive: true }); const packageJSONPath = path_1.default.join(microservicePath, "package.json"); await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"(.*?)prisma(.*?)": "(.*)"(,?)\n', ""); await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"(.*?)postinstall(.*?)": "(.*)"(,?)\n', ""); await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"db:generate-client"(.*?)\n', ""); }