Files
my-protfolio/scripts/prepare-standalone.mjs

30 lines
1.4 KiB
JavaScript

import { cp, readdir, rm } from 'node:fs/promises';
import path from 'node:path';
const projectRoot = path.resolve('.');
const standaloneRoot = path.join(projectRoot, '.next', 'standalone');
// Instrumentation traces can bypass Next's route-level exclusions. Runtime
// state belongs to the mounted volume, never to a distributable build.
async function removeRuntimeState(directory) {
for (const entry of await readdir(directory, { withFileTypes: true })) {
const target = path.resolve(directory, entry.name);
if (!target.startsWith(`${standaloneRoot}${path.sep}`)) throw new Error('Invalid package path');
if (entry.name === 'node_modules') continue;
if (entry.name === 'data' || entry.name === 'backups' || entry.name === 'auth-secret' || /\.sqlite(?:-|$)/.test(entry.name) || /^\.env(?:\.|$)/.test(entry.name)) {
await rm(target, { recursive: entry.isDirectory(), force: true });
} else if (entry.isDirectory()) {
await removeRuntimeState(target);
}
}
}
await removeRuntimeState(standaloneRoot);
await cp(path.join(projectRoot, 'public'), path.join(standaloneRoot, 'public'), { recursive: true });
await cp(path.join(projectRoot, 'migrations'), path.join(standaloneRoot, 'migrations'), { recursive: true });
await cp(
path.join(projectRoot, '.next', 'static'),
path.join(standaloneRoot, '.next', 'static'),
{ recursive: true },
);