This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
314 lines (287 loc) · 9.4 KB
/
cli.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env node
/**
* Created by busyhe on 2021/9/29.
* Email: [email protected]
* Description:
*/
const fs = require('fs')
const path = require('path')
const ini = require('ini')
const chalk = require('chalk')
const inquirer = require('inquirer')
const { Command } = require('commander')
const program = new Command()
const Package = require('./package.json')
const { isWindows } = require('./utils')
const logger = require('./lib/logger')
const LOCAL_REPOS = require('./repos.json')
const axios = require('axios')
const dayjs = require('dayjs')
const relativeTime = require('dayjs/plugin/relativeTime')
const ora = require('ora')
const { existsSync: exists } = require('fs')
const { sync: rm } = require('rimraf')
const download = require('download-git-repo')
const generate = require('./lib/generate')
const home = require('user-home')
const localPath = require('./lib/local-path')
dayjs.extend(relativeTime)
inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt'))
const HOME_PATH = process.env[isWindows() ? 'USERPROFILE' : 'HOME']
const WFE_CLI_REPOS_RC = path.join(HOME_PATH, '.wfe_cli_repos_rc')
const DEFAULT_REPO = 'wfe' // 默认repo
const KFZ_TEMPLATES = 'fe-templates' // 私有化库地址
program.version(Package.version)
program.usage('<command> [options]')
program
.command('init <project>')
.description('generate a new project from a template')
.option('-t, --template <template>', 'by template')
.action(initProject)
program
.command('list')
.alias('ls')
.description('list available official templates')
.action(showTemplates)
// repos command manage multiple sources
const reposProgram = program.command('repos')
reposProgram
.command('list')
.alias('ls')
.action(showRepos)
reposProgram
.command('use <template>')
.action(useRepo)
reposProgram
.command('add <name> <repo> [template]')
.action(addRepo)
reposProgram
.command('del <name>')
.action(removeRepo)
program.parse(process.argv)
process.on('exit', () => {
console.log()
})
async function initProject(projectName, opts) {
let { template } = opts
if (!template) {
const templatesRes = await getRepoTemplates()
const templatesData = templatesRes.data
const len = Math.max(...templatesData.map(key => key.name.length)) + 1
const templates = templatesData.map(item => {
return {
name: item.name + chalk.gray(line(item.name, len) + item.description) + ' ' + chalk.gray(dayjs(item.updated_at || item.last_activity_at).fromNow()),
value: item.name
}
})
const { template: selectTmp } = await inquirer.prompt([
{
type: 'list',
name: 'template',
message: 'create project should by a template?',
choices: templates
}]
)
template = selectTmp
} else {
if (localPath.isLocalPath(template)) {
const templatePath = localPath.getTemplatePath(template)
if (exists(templatePath)) {
generate(projectName, templatePath, path.resolve(projectName), err => {
if (err) logger.fatal(err)
console.log()
logger.success('Generated "%s".', projectName)
})
} else {
logger.fatal('Local template "%s" not found.', template)
}
return
}
}
downloadAndGenerate(projectName, template)
}
function downloadAndGenerate(projectName, template) {
const repo = getCurrentRepo()
const isKfz = repo.templates === KFZ_TEMPLATES
const inPlace = !projectName || projectName === '.'
const name = inPlace ? path.relative('../', process.cwd()) : projectName
const tmp = path.join(home, `.${repo.templates}`, template.replace(/[/:]/g, '-')) // 本地存储模板路径
const to = path.resolve(projectName || '.') // 目标路径
template = isKfz ? `direct:[email protected]:frontend/fe-cli/${repo.templates}/${template}.git` : `${repo.templates}/${template}`
const spinner = ora('downloading template')
spinner.start()
// Remove if local template exists
if (exists(tmp)) rm(tmp)
download(template, tmp, {
clone: isKfz // 如果使用私有化模板库则clone
}, err => {
spinner.stop()
if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim())
generate(name, tmp, to, err => {
if (err) logger.fatal(err)
console.log()
logger.success('Generated "%s".', name)
})
})
}
async function getRepoTemplates() {
const currentRepo = getCurrentRepo()
return axios.get(currentRepo.repos, {}, {
headers: {
'User-Agent': Package.name
}
})
}
/**
* 显示模板列表
*/
async function showTemplates() {
const res = await getRepoTemplates()
const requestBody = res.data
if (Array.isArray(requestBody)) {
console.log()
console.log(' Available templates:')
console.log()
requestBody.forEach(repo => {
console.log(
' ' + chalk.yellow('★') +
' ' + chalk.blueBright(repo.name) +
' - ' + repo.description +
' ' + chalk.gray(dayjs(repo.updated_at || repo.last_activity_at).fromNow()))
})
console.log()
res.headers['x-ratelimit-remaining'] && console.log(chalk.gray(` Ratelimit-Remaining: ${res.headers['x-ratelimit-remaining']}`))
} else {
// 速率限制
if (requestBody.message.indexOf('API rate limit exceeded') >= 0) {
console.log(chalk.yellow(`ratelimit-reset: ${dayjs.unix(res.headers['x-ratelimit-reset']).format('YYYY-MM-DD HH:mm:ss')}`))
console.log()
}
console.log(chalk.red(requestBody.message))
}
}
/**
* 显示repos列表
*/
function showRepos() {
const infos = []
const allRepos = getRepos()
const { current } = getCustomRepos()
const keys = Object.keys(allRepos)
const len = Math.max(...keys.map(key => key.length)) + 1
const templateLen = Math.max(...Object.values(allRepos).map(i => i.templates.length))
console.log()
console.log(' Available Repos:')
console.log()
Object.keys(allRepos).forEach(function(key) {
const item = allRepos[key]
const prefix = current === key ? '* ' : ' '
infos.push(' ' + chalk.yellow(prefix) + ' ' + chalk.blueBright(key) + line(key, len) + item.templates + line(item.templates, templateLen) + item.repos)
})
infos.push('')
infos.forEach(function(info) {
console.log(info)
})
}
/**
* 设置当前repo
* @param repoName
*/
function useRepo(repoName) {
const allRepos = getRepos()
if (!allRepos[repoName]) {
console.log('')
logger.fatal('Not find repo ' + repoName)
return
}
let customRepos = getCustomRepos()
customRepos = Object.assign({}, customRepos, {
current: repoName
})
setCustomRepos(customRepos)
logger.log(repoName + ' : ' + allRepos[repoName].templates + '-' + allRepos[repoName].repos)
}
/**
* 添加自定义repo
* @param repoName
* @param repo
* @param template
*/
function addRepo(repoName, repo, template = repoName) {
const allRepos = getRepos()
if (allRepos[repoName]) {
console.log('')
logger.fatal(`repo ${repoName} is existed`)
return
}
const customRepos = getCustomRepos()
customRepos.repos[repoName] = {
templates: template,
repos: repo
}
setCustomRepos(customRepos)
logger.log(template + ' : ' + template + '-' + repo)
}
function removeRepo(repoName) {
const allRepos = getRepos()
if (!allRepos[repoName]) {
console.log('')
logger.fatal(`repo ${repoName} is not existed`)
return
}
const customRepos = getCustomRepos()
if (!customRepos.repos[repoName]) {
console.log('')
logger.fatal(`custom repo ${repoName} is not existed`)
return
}
delete customRepos.repos[repoName]
if (repoName === customRepos.current) {
customRepos.current = DEFAULT_REPO
logger.log('current repo is ' + repoName + ' : ' + allRepos[repoName].templates + '-' + allRepos[repoName].repos)
}
setCustomRepos(customRepos)
}
/**
* 获取全部repos
* @returns {{wfe: {templates: string, repos: string}}}
*/
function getRepos() {
const { repos: customRepos } = getCustomRepos()
return Object.assign({}, LOCAL_REPOS, customRepos)
}
function getCurrentRepo() {
const { current, repos = {} } = getCustomRepos()
const allRepos = getRepos()
if (allRepos[current]) return allRepos[current]
setCustomRepos({
current: DEFAULT_REPO,
repos
})
return LOCAL_REPOS[DEFAULT_REPO]
}
function line(str, len) {
const line = new Array(Math.max(2, len - str.length + 2)).join('-')
return ' ' + line + ' '
}
/**
* 获取自定义repos
* @returns {*|{current: string, repos: {}}}
*/
function getCustomRepos() {
const customRepos = fs.existsSync(WFE_CLI_REPOS_RC)
? ini.parse(fs.readFileSync(WFE_CLI_REPOS_RC, 'utf-8'))
: {
current: DEFAULT_REPO,
repos: {}
}
if (!customRepos.repos) customRepos.repos = {}
return customRepos
}
/**
* 设置自定义repos
* @param repo
*/
function setCustomRepos(repos) {
fs.writeFileSync(WFE_CLI_REPOS_RC, ini.stringify(repos))
}