61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
import { spawnSync } from "node:child_process";
|
|
import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
import { dirname, extname, join, relative } from "node:path";
|
|
import { createRequire } from "node:module";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const rootDir = process.cwd();
|
|
const srcDir = join(rootDir, "src");
|
|
const distDir = join(rootDir, "dist");
|
|
|
|
const ASSET_EXTENSIONS = new Set([
|
|
".css",
|
|
".scss",
|
|
".svg",
|
|
".png",
|
|
".jpg",
|
|
".jpeg",
|
|
".webp",
|
|
".gif",
|
|
".ico",
|
|
".json",
|
|
]);
|
|
|
|
const copyAssets = directory => {
|
|
for (const entry of readdirSync(directory)) {
|
|
const fullPath = join(directory, entry);
|
|
const stats = statSync(fullPath);
|
|
if (stats.isDirectory()) {
|
|
copyAssets(fullPath);
|
|
continue;
|
|
}
|
|
|
|
if (!ASSET_EXTENSIONS.has(extname(fullPath))) {
|
|
continue;
|
|
}
|
|
|
|
const relativePath = relative(srcDir, fullPath);
|
|
const destinationPath = join(distDir, relativePath);
|
|
const destinationDir = dirname(destinationPath);
|
|
if (!existsSync(destinationDir)) {
|
|
mkdirSync(destinationDir, { recursive: true });
|
|
}
|
|
copyFileSync(fullPath, destinationPath);
|
|
}
|
|
};
|
|
|
|
rmSync(distDir, { recursive: true, force: true });
|
|
|
|
const tscPath = require.resolve("typescript/bin/tsc");
|
|
const tscResult = spawnSync(
|
|
process.execPath,
|
|
[tscPath, "-p", "tsconfig.build.json"],
|
|
{ stdio: "inherit", cwd: rootDir },
|
|
);
|
|
|
|
if (tscResult.status !== 0) {
|
|
process.exit(tscResult.status ?? 1);
|
|
}
|
|
|
|
copyAssets(srcDir);
|