|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// @ts-check |
| 4 | +const fs = require('fs') |
| 5 | +const path = require('path') |
| 6 | +const argv = require('minimist')(process.argv.slice(2)) |
| 7 | +const { prompt } = require('enquirer') |
| 8 | + |
| 9 | +const targetDir = argv._[0] || '.' |
| 10 | +const cwd = process.cwd() |
| 11 | +const root = path.join(cwd, targetDir) |
| 12 | +const renameFiles = { |
| 13 | + _gitignore: '.gitignore' |
| 14 | +} |
| 15 | +console.log(`Scaffolding project in ${root}...`) |
| 16 | + |
| 17 | +async function init() { |
| 18 | + if (!fs.existsSync(root)) { |
| 19 | + fs.mkdirSync(root, { recursive: true }) |
| 20 | + } else { |
| 21 | + const existing = fs.readdirSync(root) |
| 22 | + if (existing.length) { |
| 23 | + /** |
| 24 | + * @type {{ yes: boolean }} |
| 25 | + */ |
| 26 | + const { yes } = await prompt({ |
| 27 | + type: 'confirm', |
| 28 | + name: 'yes', |
| 29 | + message: |
| 30 | + `Target directory ${targetDir} is not empty.\n` + |
| 31 | + `Remove existing files and continue?` |
| 32 | + }) |
| 33 | + if (yes) { |
| 34 | + emptyDir(root) |
| 35 | + } else { |
| 36 | + return |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const templateDir = path.join( |
| 42 | + __dirname, |
| 43 | + `template-${argv.t || argv.template || 'vue'}` |
| 44 | + ) |
| 45 | + |
| 46 | + const write = (file, content) => { |
| 47 | + const targetPath = renameFiles[file] |
| 48 | + ? path.join(root, renameFiles[file]) |
| 49 | + : path.join(root, file) |
| 50 | + if (content) { |
| 51 | + fs.writeFileSync(targetPath, content) |
| 52 | + } else { |
| 53 | + copy(path.join(templateDir, file), targetPath) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const files = fs.readdirSync(templateDir) |
| 58 | + for (const file of files.filter((f) => f !== 'package.json')) { |
| 59 | + write(file) |
| 60 | + } |
| 61 | + |
| 62 | + const pkg = require(path.join(templateDir, `package.json`)) |
| 63 | + pkg.name = path.basename(root) |
| 64 | + write('package.json', JSON.stringify(pkg, null, 2)) |
| 65 | + |
| 66 | + console.log(`\nDone. Now run:\n`) |
| 67 | + if (root !== cwd) { |
| 68 | + console.log(` cd ${path.relative(cwd, root)}`) |
| 69 | + } |
| 70 | + console.log(` npm install (or \`yarn\`)`) |
| 71 | + console.log(` npm run dev (or \`yarn dev\`)`) |
| 72 | + console.log() |
| 73 | +} |
| 74 | + |
| 75 | +function copy(src, dest) { |
| 76 | + const stat = fs.statSync(src) |
| 77 | + if (stat.isDirectory()) { |
| 78 | + copyDir(src, dest) |
| 79 | + } else { |
| 80 | + fs.copyFileSync(src, dest) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function copyDir(srcDir, destDir) { |
| 85 | + fs.mkdirSync(destDir, { recursive: true }) |
| 86 | + for (const file of fs.readdirSync(srcDir)) { |
| 87 | + const srcFile = path.resolve(srcDir, file) |
| 88 | + const destFile = path.resolve(destDir, file) |
| 89 | + copy(srcFile, destFile) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function emptyDir(dir) { |
| 94 | + if (!fs.existsSync(dir)) { |
| 95 | + return |
| 96 | + } |
| 97 | + for (const file of fs.readdirSync(dir)) { |
| 98 | + const abs = path.resolve(dir, file) |
| 99 | + // baseline is Node 12 so can't use rmSync :( |
| 100 | + if (fs.lstatSync(abs).isDirectory()) { |
| 101 | + emptyDir(abs) |
| 102 | + fs.rmdirSync(abs) |
| 103 | + } else { |
| 104 | + fs.unlinkSync(abs) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +init().catch((e) => { |
| 110 | + console.error(e) |
| 111 | +}) |
0 commit comments