import { mkdtemp, rename, rm, stat } from 'node:fs/promises' import { dirname, join } from 'node:path' const exists = async (path) => { try { await stat(path) return true } catch (error) { if (error.code === 'ENOENT') return false throw error } } export const createOutput = (destination) => mkdtemp(join(dirname(destination), '.generated-next-')) export const publishOutput = async (staging, destination) => { const previous = `${staging}-previous` const hadPrevious = await exists(destination) if (hadPrevious) await rename(destination, previous) try { await rename(staging, destination) } catch (error) { if (hadPrevious) await rename(previous, destination) throw error } if (hadPrevious) await rm(previous, { recursive: true }) } export const discardOutput = (staging) => rm(staging, { recursive: true, force: true })