UNPKG

@alexaegis/nuke

Version:
85 lines (84 loc) 3.19 kB
"use strict"; const common = require("@alexaegis/common"); const workspaceTools = require("@alexaegis/workspace-tools"); const globby = require("globby"); const node_fs = require("node:fs"); const promises = require("node:fs/promises"); const node_path = require("node:path"); const logging = require("@alexaegis/logging"); const DEFAULT_NUKE_LIST = [ ".cache", "node_modules", "dist", ".turbo", "coverage", "package-lock.json", "pnpm-lock.yaml", "typedoc", ".svelte-kit" ]; const DEFAULT_NUKE_GLOBS = [ "./vite.config.ts.timestamp*", "./vitest.config.ts.timestamp*" ]; const normalizeNukeOptions = (options) => { return { ...workspaceTools.normalizeCollectWorkspacePackagesOptions(options), ...common.normalizeDryOption(options), ...logging.normalizeLoggerOption(options), skipNodeModules: options?.skipNodeModules ?? false, nukeList: options?.nukeList ?? DEFAULT_NUKE_LIST, nukeGlobs: options?.nukeGlobs ?? DEFAULT_NUKE_GLOBS, nukeMoreGlobs: options?.nukeMoreGlobs ?? [], nukeMore: options?.nukeMore ?? [], dontNukeIn: options?.dontNukeIn ?? [] }; }; const nuke = async (rawOptions) => { const options = normalizeNukeOptions(rawOptions); const allWorkspacePackages = await workspaceTools.collectWorkspacePackages(options); if (allWorkspacePackages.length === 0) { throw new Error("not in a workspace!"); } const nukeList = [...options.nukeList, ...options.nukeMore]; const nukeGlobs = [...options.nukeGlobs, ...options.nukeMoreGlobs]; const nonSkippedWorkspacePackages = allWorkspacePackages.filter( (workspacePackage) => !options.dontNukeIn.some( (skip) => typeof skip === "string" ? skip === workspacePackage.packagePath : skip.test(workspacePackage.packagePath) ) ); const rootPackage = allWorkspacePackages.find( (workspacePackage) => workspacePackage.packageKind === "root" ); if (!rootPackage) { options.logger.error("Not inside a workspace!"); return; } const packageFlatNukeTargets = nonSkippedWorkspacePackages.flatMap( (workspacePackage) => nukeList.map((toNuke) => node_path.join(workspacePackage.packagePath, toNuke)) ); const packageGlobNukeTargets = await Promise.all( nonSkippedWorkspacePackages.map( (packageDirectory) => globby.globby(nukeGlobs, { cwd: packageDirectory.packagePath, dot: true, followSymbolicLinks: false, expandDirectories: false }).then((paths) => paths.map((path) => node_path.join(packageDirectory.packagePath, path))) ) ); const everyNukeTarget = [...packageFlatNukeTargets, ...packageGlobNukeTargets.flat()].sort(); const dryRm = common.dry(options.dry, promises.rm); await Promise.allSettled( everyNukeTarget.filter((nukeTarget) => node_fs.existsSync(nukeTarget)).map((nukeTarget) => { options.logger.warn( "obliterating: " + node_path.relative(rootPackage.packagePath, nukeTarget) ); return dryRm(nukeTarget, { recursive: true }).catch(() => false); }) ); }; exports.DEFAULT_NUKE_GLOBS = DEFAULT_NUKE_GLOBS; exports.DEFAULT_NUKE_LIST = DEFAULT_NUKE_LIST; exports.normalizeNukeOptions = normalizeNukeOptions; exports.nuke = nuke;