|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const EXTENSIONS = ["css"]; |
| 5 | + |
| 6 | +function makeGlob(extension: string): string { |
| 7 | + return `**/*.${extension}`; |
| 8 | +} |
| 9 | + |
| 10 | +function makeGlobs(extensions: string[]): string[] { |
| 11 | + return extensions.map(makeGlob); |
| 12 | +} |
| 13 | + |
| 14 | +if (!fs.existsSync("dist")) { |
| 15 | + fs.mkdirSync("dist"); |
| 16 | +} |
| 17 | + |
| 18 | +const suffixes = fs |
| 19 | + .globSync(makeGlobs(EXTENSIONS), { |
| 20 | + cwd: "./src", |
| 21 | + withFileTypes: true, |
| 22 | + }) |
| 23 | + .filter((entry) => entry.isFile()) |
| 24 | + .map((entry) => |
| 25 | + path.join( |
| 26 | + "src", |
| 27 | + path.relative("src", path.join(entry.parentPath, entry.name)), |
| 28 | + ), |
| 29 | + ); |
| 30 | + |
| 31 | +for (const srcPath of suffixes) { |
| 32 | + const dstPath = path.join("dist", srcPath); |
| 33 | + |
| 34 | + const dstBasePath = path.dirname(dstPath); |
| 35 | + |
| 36 | + if (!fs.existsSync(dstBasePath)) { |
| 37 | + console.log(`Create ${dstBasePath}`); |
| 38 | + fs.mkdirSync(dstBasePath, { recursive: true }); |
| 39 | + } |
| 40 | + |
| 41 | + if (!fs.existsSync(dstPath)) { |
| 42 | + console.log(`Copy ${srcPath} to ${dstPath}`); |
| 43 | + fs.copyFileSync(srcPath, dstPath); |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + const srcStat = fs.statSync(srcPath, { bigint: true }); |
| 48 | + const dstStat = fs.statSync(dstPath, { bigint: true }); |
| 49 | + |
| 50 | + let shouldCopy = true; |
| 51 | + |
| 52 | + if (!dstStat.isFile()) { |
| 53 | + console.log(`Remove non-file ${dstPath}`); |
| 54 | + fs.rmSync(dstPath, { recursive: true, force: true }); |
| 55 | + } else if (dstStat.mtimeNs >= srcStat.mtimeNs) { |
| 56 | + console.log(`File up-to-date ${dstPath}`); |
| 57 | + shouldCopy = false; |
| 58 | + } |
| 59 | + |
| 60 | + if (shouldCopy) { |
| 61 | + console.log(`Copy ${srcPath} to ${dstPath}`); |
| 62 | + fs.copyFileSync(srcPath, dstPath); |
| 63 | + } |
| 64 | +} |
0 commit comments