nuka-code
Version:
A package brought to you by the Nuka-Cola Corporation to zap away those pesky `cache`, `node_modules`, and other related files & directories.
128 lines (123 loc) • 3.34 kB
JavaScript
import path4 from 'path';
import ignore from 'ignore';
import { rimraf } from 'rimraf';
import { readFile } from 'fs/promises';
import 'consola';
import 'fs';
import 'ts-pattern';
// src/nuke.ts
// src/lib/project.ts
var FILE_IGNORE = ".nukeignore";
var ignoreFileCache = null;
async function readIgnoreFile(filePath = process.cwd()) {
try {
if (ignoreFileCache) {
return ignoreFileCache;
}
const ignoreFile = path4.join(filePath, FILE_IGNORE);
ignoreFileCache = await readFile(ignoreFile, "utf8");
return ignoreFileCache;
} catch (error) {
return null;
}
}
function unique(array) {
return Array.from(new Set(array));
}
function toDeepGlob(filePaths) {
return unique(
filePaths.flatMap((filePath) => {
if (filePath.includes("node_modules")) {
return [filePath, `**/${filePath}`];
}
return [filePath, `**/[!node_modules]**/${filePath}`];
})
);
}
// src/nuke.ts
async function nukeEverything(rootDir = process.cwd()) {
const runId = Date.now();
const cache = await nukeCache(rootDir, runId);
const builds = await nukeBuilds(rootDir, runId);
const nodeModules = await nukeNodeModules(rootDir, runId);
return {
cache,
builds,
node_modules: nodeModules
};
}
async function nukeNodeModules(rootDir = process.cwd(), runId = Date.now()) {
const ignoreHelper = await createIgnoreFileHelper(rootDir);
return await rimraf(getNukeNodeModulesGlob(), {
glob: true,
filter: async (filePath) => {
return !ignoreHelper.test(path4.relative(rootDir, filePath)).ignored;
}
});
}
async function nukeCache(rootDir = process.cwd(), runId = Date.now()) {
const ignoreHelper = await createIgnoreFileHelper(rootDir);
return await rimraf(getNukeCacheGlob(), {
glob: true,
filter: async (filePath) => {
return !ignoreHelper.test(path4.relative(rootDir, filePath)).ignored;
}
});
}
async function nukeBuilds(rootDir = process.cwd(), runId = Date.now()) {
const ignoreHelper = await createIgnoreFileHelper(rootDir);
return await rimraf(getNukeBuildsGlob(), {
glob: true,
filter: async (filePath) => {
return !ignoreHelper.test(path4.relative(rootDir, filePath)).ignored;
}
});
}
function getNukeEverythingGlob() {
return unique([
...getNukeNodeModulesGlob(),
...getNukeCacheGlob(),
...getNukeBuildsGlob()
]);
}
function getNukeNodeModulesGlob() {
return [
// Deeply nested node_modules
"node_modules",
"**/node_modules",
// Yarn 2+
".pnp.cjs",
".pnp.loader.mjs"
];
}
function getNukeCacheGlob() {
return toDeepGlob([".turbo", ".nx/cache"]);
}
function getNukeBuildsGlob() {
return toDeepGlob([
// Build artifacts
"dist",
"out",
"output",
"outputs",
"bundle",
".output",
".outputs",
".build",
// Frameworks
".vercel",
".next",
".nuxt",
".svelte-kit",
".vinxi",
".vuepress/dist",
"storybook-static",
"coverage",
"public/build"
]);
}
async function createIgnoreFileHelper(rootDir = process.cwd()) {
const ignoreFile = await readIgnoreFile(rootDir);
return ignore().add(ignoreFile ?? "");
}
export { createIgnoreFileHelper, getNukeBuildsGlob, getNukeCacheGlob, getNukeEverythingGlob, getNukeNodeModulesGlob, nukeBuilds, nukeCache, nukeEverything, nukeNodeModules };