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