| 1 | import { existsSync } from 'node:fs' |
| 2 | import fs from 'node:fs/promises' |
| 3 | import { fileURLToPath } from 'node:url' |
| 4 | import { dirname, resolve } from 'pathe' |
| 5 | |
| 6 | const __dirname = fileURLToPath(new URL('.', import.meta.url)) |
| 7 | const __templateDir = resolve(__dirname, 'template') |
| 8 | const __pagesDir = resolve(__templateDir, 'pages') |
| 9 | |
| 10 | const shouldCreatePagesDict = () => !existsSync(__pagesDir) |
| 11 | |
| 12 | // key: copy to (relative ./) |
| 13 | // value: origin (relative ./template) |
| 14 | const needCopyFiles = { |
| 15 | 'slides.md': '../../../demo/starter/slides.md', |
| 16 | 'pages/imported-slides.md': '../../../demo/starter/pages/imported-slides.md', |
| 17 | 'snippets/external.ts': '../../../demo/starter/snippets/external.ts', |
| 18 | } |
| 19 | |
| 20 | async function main() { |
| 21 | if (shouldCreatePagesDict()) |
| 22 | await fs.mkdir(__pagesDir, { recursive: true }) |
| 23 | |
| 24 | await Promise.all( |
| 25 | Object.keys(needCopyFiles).map(async (relativeTargetPath) => { |
| 26 | const sourcePath = resolve(__templateDir, needCopyFiles[relativeTargetPath]) |
| 27 | const targetPath = resolve(__templateDir, relativeTargetPath) |
| 28 | if (existsSync(targetPath)) |
| 29 | await fs.rm(targetPath, { recursive: true, force: true }) |
| 30 | |
| 31 | await fs.mkdir(dirname(targetPath), { recursive: true }) |
| 32 | await fs.copyFile(sourcePath, targetPath) |
| 33 | }), |
| 34 | ) |
| 35 | // eslint-disable-next-line no-console |
| 36 | console.log('done...') |
| 37 | } |
| 38 | |
| 39 | main() |
| 40 |