UNPKG

hotshot-mod-manager

Version:

109 lines (93 loc) 2.79 kB
import inquirer from "inquirer"; import fs from "node:fs"; import path from "node:path"; import { cleanupString, configPath, getConfig, nameFixer } from "../main.js"; export async function cacheDriverCreatorTrigger() { const { cacheDriverName } = await inquirer.prompt([ { type: "input", name: "cacheDriverName", message: "Enter the name of the cache driver:", validate: (input) => input.trim() ? true : "Cache Driver name cannot be empty.", }, ]); if (cacheDriverName) { await cacheDriverCreator(cleanupString(cacheDriverName)); } } export async function cacheDriverCreator(moduleName) { const config = await getConfig(); if ( config?.cacheDrivers?.find((m) => m.name === moduleName) ) { throw new Error( `Error: Cache Driver '${moduleName}' already exists in config.`, ); } const cacheDriverContent = cacheDriverContents(moduleName); /* |----------------------------------------------------------------------- | File System IO |----------------------------------------------------------------------- */ const cacheDriverDirPath = path.join( process.cwd(), "src", "cache_drivers", ); fs.mkdirSync(cacheDriverDirPath, { recursive: true }); fs.writeFileSync( path.join(cacheDriverDirPath, `${moduleName}_cache.ts`), cacheDriverContent, ); /* |----------------------------------------------------------------------- | Config Update |----------------------------------------------------------------------- */ config?.cacheDrivers?.push({ core: "Redis", name: moduleName, path: `./src/cache_drivers/${moduleName}_cache`, }); const updatedConfigContent = JSON.stringify(config, null, 2); fs.writeFileSync(configPath, updatedConfigContent); } /* |----------------------------------------------------------------------- | Cache Driver Contents |----------------------------------------------------------------------- */ function cacheDriverContents(cacheDriverName) { const cacheDriverClassName = `${nameFixer(cacheDriverName, true)}CacheDriver`; return ` import { cacheNameGen, cacheResponse } from "#libs/ioredis_json" export class ${cacheDriverClassName} { private readonly cachePartition = "${ nameFixer(cacheDriverName, false) }-cache" async get(key: string) { return cacheResponse( cacheNameGen(this.cachePartition, key), null, false, ) } async set<T>(key: string, payload: T) { return cacheResponse( cacheNameGen(this.cachePartition, key), null, false, ) } async delete(key: string) { return cacheResponse( cacheNameGen(this.cachePartition, key), null, false, ) } } `; }