UNPKG

@netlify/build

Version:
57 lines (56 loc) 1.84 kB
import { rm, stat } from 'node:fs/promises'; import { resolve } from 'node:path'; import { listFrameworks } from '@netlify/framework-info'; const dirExists = async (path) => { try { await stat(path); return true; } catch { return false; } }; const getDirtyDirs = async function ({ buildDir, constants: { INTERNAL_EDGE_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC }, }) { const dirs = []; if (INTERNAL_FUNCTIONS_SRC && (await dirExists(resolve(buildDir, INTERNAL_FUNCTIONS_SRC)))) { dirs.push(INTERNAL_FUNCTIONS_SRC); } if (INTERNAL_EDGE_FUNCTIONS_SRC && (await dirExists(resolve(buildDir, INTERNAL_EDGE_FUNCTIONS_SRC)))) { dirs.push(INTERNAL_EDGE_FUNCTIONS_SRC); } return dirs; }; const coreStep = async (input) => { const dirs = await getDirtyDirs(input); for (const dir of dirs) { await rm(resolve(input.buildDir, dir), { recursive: true, force: true }); } input.systemLog(input.logs, `Cleaned up ${dirs.join(', ')}.`); return {}; }; const condition = async (input) => { // We don't want to clear directories for Remix or Remix-based frameworks, // due to the way they run Netlify Dev. try { const frameworks = await listFrameworks({ projectDir: input.buildDir }); for (const framework of frameworks) { if (framework.id === 'hydrogen' || framework.id === 'remix') { return false; } } } catch { // no-op } const dirs = await getDirtyDirs(input); return dirs.length > 0; }; export const preDevCleanup = { event: 'onPreDev', coreStep, coreStepId: 'pre_dev_cleanup', coreStepName: 'Pre Dev cleanup', coreStepDescription: () => 'Cleaning up leftover files from previous builds', condition, quiet: true, };