pre-deploy.js

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const directoryPath = path.join(__dirname, '../build');

function fromDir(startPath, filter) {
	if (!fs.existsSync(startPath)) {
		console.log(`${startPath} is not a valid path`);
		return;
	}

	const files = fs.readdirSync(startPath);
	for (var i = 0; i < files.length; i++) {
		const file = files[i];
		const filename = path.join(startPath, file);
		const stat = fs.lstatSync(filename);
		if (stat.isDirectory()) {
			fromDir(filename, filter);
		} else if (filename.endsWith(filter)) {
			console.log('-- found: ', filename);
			if (file !== 'index.html') {
				const directoryPath = path.join(startPath, path.parse(file).name);
				const fileNewPath = path.join(directoryPath, 'index.html');
				createDir(directoryPath);
				moveFile(filename, fileNewPath);
			}
		}
	}
}

function createDir(directoryPath) {
	if (!fs.existsSync(directoryPath)) {
		try {
			fs.mkdirSync(directoryPath);
			console.log('-- created:', directoryPath);
		} catch (error) {
			console.error('Could not create:', directoryPath);
		}
	}
}

function moveFile(oldFile, newFile) {
	fs.rename(oldFile, newFile, function (err) {
		if (err) throw err;
		console.log('-- moved:', newFile);
	});
}

fromDir(directoryPath, '.html');