|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 3 | + |
| 4 | +// @ts-check |
| 5 | +const fs = require('fs') |
| 6 | +const path = require('path') |
| 7 | +const argv = require('minimist')(process.argv.slice(2)) |
| 8 | +const { prompt } = require('enquirer') |
| 9 | +const { cyan, blue, yellow, bold, dim, green } = require('kolorist') |
| 10 | +const { version } = require('./package.json') |
| 11 | +const { basename } = require('path') |
| 12 | + |
| 13 | +const cwd = process.cwd() |
| 14 | + |
| 15 | +const renameFiles = { |
| 16 | + _gitignore: '.gitignore', |
| 17 | + _npmrc: '.npmrc', |
| 18 | +} |
| 19 | + |
| 20 | +async function init() { |
| 21 | + console.log() |
| 22 | + console.log(` ${cyan('●') + blue('■') + yellow('▲')}`) |
| 23 | + console.log(`${bold(' Slidev') + dim(' Theme Creator')} ${blue(`v${version}`)}`) |
| 24 | + console.log() |
| 25 | + |
| 26 | + let targetDir = argv._[0] |
| 27 | + if (!targetDir) { |
| 28 | + /** |
| 29 | + * @type {{ name: string }} |
| 30 | + */ |
| 31 | + const { name } = await prompt({ |
| 32 | + type: 'input', |
| 33 | + name: 'name', |
| 34 | + message: 'Theme name:', |
| 35 | + initial: 'slidev-theme-starter', |
| 36 | + }) |
| 37 | + targetDir = name |
| 38 | + } |
| 39 | + const packageName = await getValidPackageName(targetDir) |
| 40 | + const root = path.join(cwd, targetDir) |
| 41 | + |
| 42 | + if (!fs.existsSync(root)) { |
| 43 | + fs.mkdirSync(root, { recursive: true }) |
| 44 | + } |
| 45 | + else { |
| 46 | + const existing = fs.readdirSync(root) |
| 47 | + if (existing.length) { |
| 48 | + console.log(yellow(` Target directory "${targetDir}" is not empty.`)) |
| 49 | + /** |
| 50 | + * @type {{ yes: boolean }} |
| 51 | + */ |
| 52 | + const { yes } = await prompt({ |
| 53 | + type: 'confirm', |
| 54 | + name: 'yes', |
| 55 | + initial: 'Y', |
| 56 | + message: 'Remove existing files and continue?', |
| 57 | + }) |
| 58 | + if (yes) |
| 59 | + emptyDir(root) |
| 60 | + |
| 61 | + else |
| 62 | + return |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + console.log(dim(' Scaffolding Slidev theme in ') + targetDir + dim(' ...')) |
| 67 | + |
| 68 | + prepareTemplate(root, path.join(__dirname, 'template'), packageName) |
| 69 | + |
| 70 | + const pkgManager = (/pnpm/.test(process.env.npm_execpath) || /pnpm/.test(process.env.npm_config_user_agent)) |
| 71 | + ? 'pnpm' |
| 72 | + : /yarn/.test(process.env.npm_execpath) ? 'yarn' : 'npm' |
| 73 | + |
| 74 | + const related = path.relative(cwd, root) |
| 75 | + |
| 76 | + console.log(green(' Done.\n')) |
| 77 | + |
| 78 | + console.log(dim('\n start it by:\n')) |
| 79 | + if (root !== cwd) |
| 80 | + console.log(blue(` cd ${bold(related)}`)) |
| 81 | + |
| 82 | + console.log(blue(` ${pkgManager === 'yarn' ? 'yarn' : `${pkgManager} install`}`)) |
| 83 | + console.log(blue(` ${pkgManager === 'yarn' ? 'yarn dev' : `${pkgManager} run dev`}`)) |
| 84 | + console.log() |
| 85 | + console.log(` ${cyan('●')} ${blue('■')} ${yellow('▲')}`) |
| 86 | + console.log() |
| 87 | +} |
| 88 | + |
| 89 | +function prepareTemplate(root, templateDir, packageName) { |
| 90 | + const write = (file, content) => { |
| 91 | + const targetPath = renameFiles[file] |
| 92 | + ? path.join(root, renameFiles[file]) |
| 93 | + : path.join(root, file) |
| 94 | + if (content) |
| 95 | + fs.writeFileSync(targetPath, content) |
| 96 | + |
| 97 | + else |
| 98 | + copy(path.join(templateDir, file), targetPath) |
| 99 | + } |
| 100 | + |
| 101 | + const files = fs.readdirSync(templateDir) |
| 102 | + for (const file of files.filter(f => !['package.json', 'README.md'].includes(f))) |
| 103 | + write(file) |
| 104 | + |
| 105 | + write( |
| 106 | + 'package.json', |
| 107 | + JSON.stringify({ |
| 108 | + name: packageName, |
| 109 | + version: '0.0.0', |
| 110 | + ...require(path.join(templateDir, 'package.json')), |
| 111 | + }, null, 2), |
| 112 | + ) |
| 113 | + |
| 114 | + write( |
| 115 | + 'README.md', |
| 116 | + fs |
| 117 | + .readFileSync(path.join(templateDir, 'README.md'), 'utf-8') |
| 118 | + .replace(/{{package-name}}/g, packageName) |
| 119 | + .replace(/{{name}}/g, getThemeName(packageName)), |
| 120 | + ) |
| 121 | +} |
| 122 | + |
| 123 | +function copy(src, dest) { |
| 124 | + const stat = fs.statSync(src) |
| 125 | + if (stat.isDirectory()) |
| 126 | + copyDir(src, dest) |
| 127 | + |
| 128 | + else |
| 129 | + fs.copyFileSync(src, dest) |
| 130 | +} |
| 131 | + |
| 132 | +function getValidPackageName(projectName) { |
| 133 | + projectName = basename(projectName) |
| 134 | + if (!projectName.startsWith('slidev-theme-')) |
| 135 | + return `slidev-theme-${projectName}` |
| 136 | + return projectName |
| 137 | +} |
| 138 | + |
| 139 | +function getThemeName(pkgName) { |
| 140 | + return pkgName.replace(/^slidev-theme-/, '') |
| 141 | +} |
| 142 | + |
| 143 | +function copyDir(srcDir, destDir) { |
| 144 | + fs.mkdirSync(destDir, { recursive: true }) |
| 145 | + for (const file of fs.readdirSync(srcDir)) { |
| 146 | + const srcFile = path.resolve(srcDir, file) |
| 147 | + const destFile = path.resolve(destDir, file) |
| 148 | + copy(srcFile, destFile) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +function emptyDir(dir) { |
| 153 | + if (!fs.existsSync(dir)) |
| 154 | + return |
| 155 | + |
| 156 | + for (const file of fs.readdirSync(dir)) { |
| 157 | + const abs = path.resolve(dir, file) |
| 158 | + // baseline is Node 12 so can't use rmSync :( |
| 159 | + if (fs.lstatSync(abs).isDirectory()) { |
| 160 | + emptyDir(abs) |
| 161 | + fs.rmdirSync(abs) |
| 162 | + } |
| 163 | + else { |
| 164 | + fs.unlinkSync(abs) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +init().catch((e) => { |
| 170 | + console.error(e) |
| 171 | +}) |
0 commit comments