|
1 | 1 | import fs from 'node:fs' |
2 | | -import { execSync } from 'child_process' |
| 2 | +import path from 'path' |
| 3 | +import { fileURLToPath } from 'url' |
3 | 4 | import type { AstroIntegration } from 'astro' |
4 | 5 |
|
| 6 | +const copyFiles = (src: string, dest: string) => { |
| 7 | + const entries = fs.readdirSync(src, { withFileTypes: true }) |
| 8 | + if (!fs.existsSync(dest)) { |
| 9 | + fs.mkdirSync(dest, { recursive: true }) |
| 10 | + } |
| 11 | + |
| 12 | + for (const entry of entries) { |
| 13 | + const srcPath = path.join(src, entry.name) |
| 14 | + const destPath = path.join(dest, entry.name) |
| 15 | + |
| 16 | + if (entry.isDirectory()) { |
| 17 | + copyFiles(srcPath, destPath) |
| 18 | + } else { |
| 19 | + if (!fs.existsSync(destPath)) { |
| 20 | + fs.copyFileSync(srcPath, destPath) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
5 | 26 | export default (): AstroIntegration => ({ |
6 | 27 | name: 'public-notion-copier', |
7 | 28 | hooks: { |
8 | 29 | 'astro:build:done': async ({ dir }) => { |
9 | | - const outDir = new URL('notion', dir.href).pathname |
| 30 | + const dirPath = fileURLToPath(dir) |
| 31 | + const outDir = path.join(dirPath, 'notion') |
10 | 32 | if (!fs.existsSync(outDir)) { |
11 | | - fs.mkdirSync(outDir) |
| 33 | + fs.mkdirSync(outDir, { recursive: true }) |
12 | 34 | } |
13 | 35 |
|
14 | | - execSync(`cp -n -r public/notion/* ${outDir} || true`) |
| 36 | + copyFiles('public/notion', outDir) |
| 37 | + console.log('Finished copying notion files to root!') |
15 | 38 | }, |
16 | 39 | }, |
17 | 40 | }) |
0 commit comments