Files
Portfolio/scripts/materialize-empty-asset-set.mjs

34 lines
1.3 KiB
JavaScript

import { lstat, mkdir, readFile, readdir } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const manifestPath = resolve(repositoryRoot, 'content/public/asset-manifest.json');
const preparedAssetsPath = resolve(repositoryRoot, 'inputs/prepared-assets');
const readEmptyManifest = async () => {
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'));
if (Object.keys(manifest).length !== 1 || !Array.isArray(manifest.assets) || manifest.assets.length !== 0) {
throw new Error('Phase 1 asset manifest must contain exactly zero assets.');
}
};
const ensureEmptyPreparedAssetDirectory = async () => {
try {
const stats = await lstat(preparedAssetsPath);
if (stats.isSymbolicLink() || !stats.isDirectory()) {
throw new Error('Prepared asset path must be a non-symlink directory.');
}
} catch (error) {
if ((error).code !== 'ENOENT') throw error;
await mkdir(preparedAssetsPath, { recursive: true });
}
if ((await readdir(preparedAssetsPath)).length !== 0) {
throw new Error('Prepared asset directory must be empty during Phase 1.');
}
};
await readEmptyManifest();
await ensureEmptyPreparedAssetDirectory();