cli/index.js

161 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2023-01-05 17:00:49 +08:00
#!/usr/bin/node
2020-09-28 19:20:33 +08:00
/**
2023-01-05 17:00:49 +08:00
*
2020-09-28 19:20:33 +08:00
* @author yutent<yutent.io@gmail.com>
2023-01-05 17:00:49 +08:00
* @date 2022/10/10 15:17:36
2020-09-28 19:20:33 +08:00
*/
2023-01-05 17:05:34 +08:00
import { red, cyan, blue, gray } from 'kolorist'
2023-01-05 17:00:49 +08:00
import prompts from 'prompts'
import fs from 'iofs'
import { resolve, join, dirname } from 'path'
import {
writePackageJson,
writeGitIgnore,
writePrettierrc,
writePm2File
} from './lib/demo-config.js'
import { writeMainJs, writeAppJs, writeModelJs } from './lib/demo-js.js'
const CURRENT_DIR = process.cwd()
2023-11-02 11:16:54 +08:00
const root = dirname(
import.meta.url.slice(process.platform === 'win32' ? 10 : 7)
)
2023-01-05 17:00:49 +08:00
const { version } = JSON.parse(fs.cat(join(root, './package.json')))
2020-09-28 19:20:33 +08:00
2023-01-05 17:00:49 +08:00
const DEFAULT_NAME = 'gm5-app'
let args = process.argv.slice(2)
let targetDir = ''
function isEmpty(dir) {
let list = fs.ls(dir)
if (list && list.length) {
return false
2020-09-28 19:20:33 +08:00
}
2023-01-05 17:00:49 +08:00
return true
2020-09-28 19:20:33 +08:00
}
2023-01-05 17:00:49 +08:00
function sleep(num = 1) {
return new Promise(resolve => setTimeout(resolve, num * 1000))
2020-09-28 19:20:33 +08:00
}
2023-01-05 17:00:49 +08:00
function printHelp() {
console.log('Usage: create-five {command} [arguments]')
console.log(' ', 'create-five init', '一个快速创建Five.js项目的小工具')
console.log(' ', 'create-five -h[--help]', '打印帮助信息')
console.log()
2020-09-28 19:20:33 +08:00
process.exit()
}
2023-01-05 17:00:49 +08:00
!(async function () {
switch (args[0]) {
case '-v':
case '--version':
console.log('v' + version)
break
case '-h':
case '--help':
printHelp()
break
default:
let res = await prompts([
{
name: 'projectName',
type: 'text',
message: '项目名称(也是目录名, 只能为英文、数字、-、.):',
initial: DEFAULT_NAME,
validate: val => /^[a-zA-Z\d\-\.]+$/.test(val),
onState: ({ value }) => (targetDir = join(CURRENT_DIR, value))
},
{
name: 'shouldOverwrite',
type: _ => (isEmpty(targetDir) ? null : 'toggle'),
2023-11-02 11:16:54 +08:00
message: _ =>
`目录 ${cyan(targetDir)} 非空, 是否${red('删除')}目录下所有的文件?`,
2023-01-05 17:00:49 +08:00
initial: false,
active: '是',
inactive: '否'
},
{
name: 'confirmCheck',
type: shouldOverwrite => {
if (shouldOverwrite === false) {
console.log(red('✖') + ' 操作取消~~')
2023-01-05 17:19:55 +08:00
process.exit()
2023-01-05 17:00:49 +08:00
}
return null
}
}
])
console.log()
2023-01-05 17:16:55 +08:00
if (res.projectName === undefined) {
console.log('已取消操作~~')
2023-01-05 17:19:55 +08:00
process.exit()
2023-01-05 17:16:55 +08:00
}
2023-01-05 17:00:49 +08:00
if (res.projectName === '.') {
res.projectName = DEFAULT_NAME
}
console.log('指定的项目名为: %s', cyan(res.projectName))
console.log('项目目录为: %s', cyan(targetDir))
if (res.shouldOverwrite) {
console.log(red('目录非空, 1s 后将清空目录~~'))
await sleep(1)
let list = fs.ls(targetDir)
list.forEach(it => fs.rm(it, true))
} else {
console.log(red('程序将在 1s 后初始化项目~~'))
await sleep(1)
}
console.log(cyan('\n初始化项目...'))
2023-01-05 17:12:50 +08:00
console.log('[c---------]', '10%')
2023-01-05 17:00:49 +08:00
fs.mkdir(join(targetDir, 'apps'))
fs.mkdir(join(targetDir, 'models'))
fs.mkdir(join(targetDir, 'config'))
fs.mkdir(join(targetDir, 'data'))
fs.mkdir(join(targetDir, 'public'))
fs.mkdir(join(targetDir, 'views'))
2023-01-05 17:12:50 +08:00
console.log('[ooc-------]', '30%')
2023-01-05 17:00:49 +08:00
writePackageJson(join(targetDir, 'package.json'), res.projectName)
writeGitIgnore(join(targetDir, '.gitignore'))
writePrettierrc(join(targetDir, '.prettierrc.yaml'))
writePm2File(join(targetDir, 'app.dev.yaml'), {
name: res.projectName,
env: 'development',
cluster: false
})
writePm2File(join(targetDir, 'app.yaml'), {
name: res.projectName,
cluster: false
})
console.log('[oooooc----]', '60%')
writeMainJs(join(targetDir, 'app.js'))
writeAppJs(join(targetDir, 'apps/index.js'))
writeModelJs(join(targetDir, 'models/index.js'))
console.log('[oooooooooo]', '100%')
console.log(cyan('初始化完成, 可依次执行以下命令启动项目: '))
2023-01-05 17:12:50 +08:00
console.log(blue('cd ' + res.projectName))
2023-01-05 17:00:49 +08:00
console.log(blue('npm i'))
2023-01-05 17:05:34 +08:00
console.log(blue('pm2 start app.dev.yaml'), gray('# 开发模式'))
console.log(blue('pm2 start app.yaml'), gray('# 生产模式'))
2023-01-05 17:12:50 +08:00
console.log('访问地址: ', cyan('http://127.0.0.1:3000'))
2023-01-05 17:00:49 +08:00
break
}
})()