import { promises as fs } from "fs";
import * as util from "../util";
import * as path from "path";

export default async function copy() {
	const files = await readDirRecursive("site/static");
	for (const f of files) {
		await util.write(path.relative("site/static", f), await fs.readFile(f));
	}
}

async function readDirRecursive(dir: string): Promise<string[]> {
	const files = await fs.readdir(dir);
	const promises = files.map(async (f) => {
		const fPath = path.join(dir, f);
		const stats = (await fs.stat(fPath));
		if (stats.isDirectory()) {
			return readDirRecursive(fPath);
		} else {
			return [fPath];
		}
	});
	const groups = await Promise.all(promises);
	return Array.prototype.concat(...groups);
}