@practica/create-node-app
Version:
Create Node.js app that is packed with best practices AND strive for simplicity
74 lines (73 loc) • 4.3 kB
JavaScript
;
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.chooseWebFramework = void 0;
const path_1 = __importDefault(require("path"));
const fsExtra = __importStar(require("fs-extra"));
const string_manipulation_helpers_1 = require("../string-manipulation-helpers");
//TODO: since for now we support only 2 frameworks, the generation is not very clever and many tasks are duplicated
// once we need to support more -> we will make the generation more flexible
async function chooseWebFramework(generatedAppRoot, options) {
const microservicePath = (0, string_manipulation_helpers_1.getMicroservicePath)(generatedAppRoot);
const librariesPath = (0, string_manipulation_helpers_1.getLibrariesPath)(generatedAppRoot);
if (options.webFramework === "express") {
await adjustTheCodeToExpressFramework(microservicePath, librariesPath);
}
else if (options.webFramework === "fastify") {
await adjustTheCodeToFastifyFramework(microservicePath, librariesPath);
}
}
exports.chooseWebFramework = chooseWebFramework;
async function adjustTheCodeToFastifyFramework(microservicePath, librariesPath) {
const expressFolder = path_1.default.join(microservicePath, "entry-points-express");
await fsExtra.rm(expressFolder, { recursive: true });
const expressMiddlewaresFolder = path_1.default.join(librariesPath, "common-express-middlewares");
await fsExtra.rm(expressMiddlewaresFolder, { recursive: true });
const fastifyFolderOldName = path_1.default.join(microservicePath, "entry-points-fastify");
const packageJSONPath = path_1.default.join(microservicePath, "package.json");
await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"(.*?)express(.*?)": "(.*)"(,?)\n', "");
const fastifyFolderNewName = path_1.default.join(microservicePath, "entry-points");
await fsExtra.move(fastifyFolderOldName, fastifyFolderNewName, {
overwrite: true,
});
await (0, string_manipulation_helpers_1.replacePhraseInAllFiles)(microservicePath, "/entry-points-fastify/api/", "/entry-points/api/");
}
async function adjustTheCodeToExpressFramework(microservicePath, librariesPath) {
const fastifyFolder = path_1.default.join(microservicePath, "entry-points-fastify");
await fsExtra.rm(fastifyFolder, { recursive: true });
const fastifyMiddlewaresFolder = path_1.default.join(librariesPath, "common-fastify-plugins");
await fsExtra.rm(fastifyMiddlewaresFolder, { recursive: true });
const expressFolderOldName = path_1.default.join(microservicePath, "entry-points-express");
const packageJSONPath = path_1.default.join(microservicePath, "package.json");
await (0, string_manipulation_helpers_1.replacePhraseInFile)(packageJSONPath, '"(.*?)fastify(.*?)": "(.*)"(,?)\n', "");
const expressFolderNewName = path_1.default.join(microservicePath, "entry-points");
await fsExtra.move(expressFolderOldName, expressFolderNewName, {
overwrite: true,
});
await (0, string_manipulation_helpers_1.replacePhraseInAllFiles)(microservicePath, "/entry-points-fastify/api/", "/entry-points/api/");
}