2022-10-21 12:14:00 +08:00
|
|
|
#!/usr/bin/env node
|
2022-10-10 19:26:24 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2022/10/10 15:17:36
|
|
|
|
*/
|
|
|
|
|
2023-02-01 19:11:52 +08:00
|
|
|
import { request } from 'https'
|
2022-10-21 11:59:49 +08:00
|
|
|
import { red, cyan, blue } from 'kolorist'
|
2022-10-21 11:46:50 +08:00
|
|
|
import prompts from 'prompts'
|
2022-10-10 19:26:24 +08:00
|
|
|
import fs from 'iofs'
|
2023-02-21 17:44:45 +08:00
|
|
|
import { resolve, join, dirname, normalize } from 'path'
|
2022-10-21 11:46:50 +08:00
|
|
|
import {
|
|
|
|
writePackageJson,
|
|
|
|
writeConfigFile,
|
|
|
|
writeGitIgnore,
|
|
|
|
writePrettierrc
|
|
|
|
} from './lib/demo-config.js'
|
2022-10-10 19:26:24 +08:00
|
|
|
import { writeHtmlFile } from './lib/demo-html.js'
|
|
|
|
import { writeLogo } from './lib/logo.js'
|
|
|
|
import {
|
|
|
|
writeMainJs,
|
|
|
|
writeAppVue,
|
|
|
|
writeHomeVue,
|
|
|
|
writeAboutVue,
|
|
|
|
writeHelloVue,
|
|
|
|
writeRouter,
|
|
|
|
writeStore
|
|
|
|
} from './lib/demo-js.js'
|
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
const CURRENT_DIR = process.cwd()
|
2023-02-21 17:44:45 +08:00
|
|
|
const root = normalize(
|
|
|
|
dirname(import.meta.url.slice(process.platform === 'win32' ? 8 : 7))
|
2023-01-13 11:42:24 +08:00
|
|
|
)
|
2023-02-08 18:08:52 +08:00
|
|
|
const VERSION = '1.0.1'
|
2022-10-11 15:39:24 +08:00
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
const DEFAULT_NAME = 'vue-live-app'
|
|
|
|
|
2022-10-10 19:26:24 +08:00
|
|
|
let args = process.argv.slice(2)
|
2022-10-21 11:46:50 +08:00
|
|
|
let targetDir = ''
|
|
|
|
|
|
|
|
function isEmpty(dir) {
|
|
|
|
let list = fs.ls(dir)
|
|
|
|
if (list && list.length) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-02-01 19:11:52 +08:00
|
|
|
function getVueLiveVersion() {
|
|
|
|
return new Promise(yes => {
|
|
|
|
request('https://registry.npmmirror.com/@bytedo/vue-live', res => {
|
|
|
|
let data = ''
|
|
|
|
res.on('data', chunk => (data += chunk))
|
|
|
|
res.on('end', _ => {
|
|
|
|
try {
|
|
|
|
data = JSON.parse(data)
|
|
|
|
yes(data['dist-tags'].latest)
|
|
|
|
} catch (e) {
|
|
|
|
yes('0.1.1')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}).end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
function sleep(num = 1) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, num * 1000))
|
|
|
|
}
|
2022-10-10 19:26:24 +08:00
|
|
|
|
|
|
|
function printHelp() {
|
|
|
|
console.log('Usage: vue-live-cli {command} [arguments]')
|
|
|
|
console.log(' ', 'vue-live-cli init', '初始化一个符合vue-live的vue项目')
|
|
|
|
console.log(' ', 'vue-live-cli -h[--help]', '打印帮助信息')
|
|
|
|
console.log()
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
!(async function () {
|
|
|
|
switch (args[0]) {
|
|
|
|
case '-v':
|
|
|
|
case '--version':
|
2023-02-08 18:08:52 +08:00
|
|
|
console.log('v' + VERSION)
|
2022-10-21 11:46:50 +08:00
|
|
|
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-01-13 11:42:24 +08:00
|
|
|
message: _ =>
|
|
|
|
`目录 ${cyan(targetDir)} 非空, 是否${red('删除')}目录下所有的文件?`,
|
2022-10-21 11:46:50 +08:00
|
|
|
initial: false,
|
|
|
|
active: '是',
|
|
|
|
inactive: '否'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'confirmCheck',
|
|
|
|
type: shouldOverwrite => {
|
|
|
|
if (shouldOverwrite === false) {
|
|
|
|
console.log(red('✖') + ' 操作取消~~')
|
2023-02-01 19:11:52 +08:00
|
|
|
process.exit()
|
2022-10-21 11:46:50 +08:00
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
console.log()
|
|
|
|
|
2023-02-01 19:11:52 +08:00
|
|
|
if (res.projectName === undefined) {
|
|
|
|
console.log('已取消操作~~')
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
if (res.projectName === '.') {
|
|
|
|
res.projectName = DEFAULT_NAME
|
|
|
|
}
|
2022-10-10 19:26:24 +08:00
|
|
|
|
2023-02-21 17:44:45 +08:00
|
|
|
targetDir = normalize(targetDir)
|
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
console.log('指定的项目名为: %s', cyan(res.projectName))
|
|
|
|
console.log('项目目录为: %s', cyan(targetDir))
|
2022-10-10 19:26:24 +08:00
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
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)
|
|
|
|
}
|
2022-10-10 19:26:24 +08:00
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
console.log(cyan('\n初始化项目...'))
|
2022-10-10 19:26:24 +08:00
|
|
|
|
2023-02-01 19:11:52 +08:00
|
|
|
let vueLiveVer = await getVueLiveVersion()
|
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
fs.mkdir(join(targetDir, 'src'))
|
2022-10-10 19:26:24 +08:00
|
|
|
|
2022-10-21 11:46:50 +08:00
|
|
|
console.log('[c---------]', '10%')
|
|
|
|
|
2023-02-01 19:11:52 +08:00
|
|
|
writePackageJson(
|
|
|
|
join(targetDir, 'package.json'),
|
|
|
|
res.projectName,
|
|
|
|
vueLiveVer
|
|
|
|
)
|
2022-10-21 11:46:50 +08:00
|
|
|
writeConfigFile(join(targetDir, 'vue.live.js'))
|
|
|
|
writeGitIgnore(join(targetDir, '.gitignore'))
|
|
|
|
writePrettierrc(join(targetDir, '.prettierrc.yaml'))
|
|
|
|
|
|
|
|
console.log('[ooc-------]', '30%')
|
|
|
|
|
|
|
|
writeHtmlFile(join(targetDir, 'index.html'))
|
|
|
|
|
|
|
|
writeLogo(join(targetDir, 'src/assets/logo.svg'))
|
2023-02-16 15:43:47 +08:00
|
|
|
fs.cp(
|
|
|
|
join(root, 'lib/favicon.ico'),
|
|
|
|
join(targetDir, 'public/favicon.ico')
|
|
|
|
)
|
2022-10-21 11:46:50 +08:00
|
|
|
|
|
|
|
console.log('[oooooc----]', '60%')
|
|
|
|
|
|
|
|
writeMainJs(join(targetDir, 'src/main.js'))
|
|
|
|
writeAppVue(join(targetDir, 'src/app.vue'))
|
|
|
|
writeHomeVue(join(targetDir, 'src/views/home.vue'))
|
|
|
|
writeAboutVue(join(targetDir, 'src/views/about.vue'))
|
|
|
|
writeHelloVue(join(targetDir, 'src/components/hello.vue'))
|
|
|
|
writeRouter(join(targetDir, 'src/router.js'))
|
2023-02-01 19:11:52 +08:00
|
|
|
writeStore(join(targetDir, 'src/store.js'), vueLiveVer)
|
2022-10-21 11:46:50 +08:00
|
|
|
|
|
|
|
console.log('[oooooooooo]', '100%')
|
|
|
|
console.log(cyan('初始化完成, 可依次执行以下命令启动项目: '))
|
2022-10-21 11:59:49 +08:00
|
|
|
console.log(blue('npm i'))
|
|
|
|
console.log(blue('npm start'))
|
2022-10-21 11:46:50 +08:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})()
|