UNPKG

radashi-helper

Version:
97 lines (95 loc) 2.81 kB
import { findSources } from "./chunk-652CQLOF.js"; import { cwdRelative, projectFolders } from "./chunk-BKS73LZK.js"; import { prompt } from "./chunk-MJC327DM.js"; import { debug, getEnv } from "./chunk-DSXY7YVB.js"; import { EarlyExitError, RadashiError, log } from "./chunk-JLKIE3A2.js"; import { objectify } from "./chunk-VOEQFXI5.js"; // src/fn-remove.ts import { existsSync } from "node:fs"; import fs, { readdir, rm } from "node:fs/promises"; import { dirname, join, relative } from "node:path"; async function removeFunction(options = {}) { const env = options.env ?? getEnv(options.dir); let oldFuncPath = options.funcPath; if (!oldFuncPath) { const pathsInside = await findSources(env, ["src", "overrides"]); debug("pathsInside:", pathsInside); const { src: sourceFuncs = [], overrides: overrideFuncs = [] } = objectify( Object.entries(pathsInside), ([type]) => type, ([type, sourcePaths]) => { const sourceRoot = join( type === "src" ? env.root : env.overrideDir, "src" ); return sourcePaths?.map( (sourcePath) => relative(sourceRoot, sourcePath).replace(/\.ts$/, "") ); } ); debug("sourceFuncs:", sourceFuncs); debug("overrideFuncs:", overrideFuncs); const selectedFunc = await prompt({ type: "autocomplete", name: "selectedFunc", message: "Select a function to remove:", choices: [...sourceFuncs, ...overrideFuncs].sort().map((funcPath) => ({ title: funcPath, value: funcPath })) }); if (!selectedFunc) { throw new EarlyExitError("No function selected. Exiting..."); } oldFuncPath = selectedFunc; } const checkFile = (folderPrefix2) => existsSync(join(folderPrefix2, oldFuncPath + ".ts")); const folderPrefix = checkFile(join(env.root, "src")) ? env.root : checkFile(join(env.overrideDir, "src")) ? env.overrideDir : null; if (!folderPrefix) { throw new RadashiError( `Function ${oldFuncPath} was not found in ${env.root}/src or the overrides folder` ); } const [oldGroup, oldFuncName] = oldFuncPath.split("/"); for (const folder of projectFolders) { const prevPath = join( folderPrefix, folder.name, oldGroup, oldFuncName + folder.extension ); if (!existsSync(prevPath)) { continue; } log(`Removing ${cwdRelative(prevPath)}`); await rm(prevPath); const prevDir = dirname(prevPath); if ((await readdir(prevDir)).length === 0) { log(`Removing empty directory: ${cwdRelative(prevDir)}`); await fs.rm(prevDir, { recursive: true }); } } if (env.radashiDir) { const { default: build } = await import("./build-7F3PTR5C.js"); await build({ env }); } } export { removeFunction };