create-fite-app/index.js

201 lines
4.3 KiB
JavaScript
Raw Normal View History

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
*/
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-10 19:26:24 +08:00
2023-05-23 18:36:35 +08:00
const NODE_VERSION = process.versions.node.split('.').map(n => +n)
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
)
2022-10-11 15:39:24 +08:00
2023-03-01 11:20:07 +08:00
const DEFAULT_NAME = 'fite-app'
2022-10-21 11:46:50 +08:00
let targetDir = ''
2023-05-23 18:36:35 +08:00
if (NODE_VERSION[0] < 16 || (NODE_VERSION[0] === 16 && NODE_VERSION[1] < 6)) {
console.log(red('Error: 你当前的环境不满足 fite 构建工具的要求'))
2023-02-25 22:22:11 +08:00
console.log(
2023-05-23 18:36:35 +08:00
'fite 需要Node.js版本在 %s 以上, \n你当前的Node.js版本为: %s',
2023-02-25 22:22:11 +08:00
blue('v16.6.0'),
red(process.version),
'\n\n'
)
process.exit()
}
2022-10-21 11:46:50 +08:00
function isEmpty(dir) {
let list = fs.ls(dir)
if (list && list.length) {
return false
}
return true
}
2023-04-20 18:59:36 +08:00
function getFiteVersion() {
return new Promise(yes => {
2023-03-01 11:20:07 +08:00
request('https://registry.npmmirror.com/fite', res => {
let data = ''
res.on('data', chunk => (data += chunk))
res.on('end', _ => {
try {
data = JSON.parse(data)
yes(data['dist-tags'].latest)
} catch (e) {
2023-03-01 11:20:07 +08:00
yes('0.3.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
2022-10-21 11:46:50 +08:00
!(async function () {
2023-02-25 22:22:11 +08:00
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'),
message: _ =>
`目录 ${cyan(targetDir)} 非空, 是否${red('删除')}目录下所有的文件?`,
initial: false,
active: '是',
inactive: '否'
},
{
name: 'confirmCheck',
type: shouldOverwrite => {
if (shouldOverwrite === false) {
console.log(red('✖') + ' 操作取消~~')
process.exit()
2022-10-21 11:46:50 +08:00
}
2023-02-25 22:22:11 +08:00
return null
}
2023-02-25 22:22:11 +08:00
},
{
name: 'isSPA',
type: 'toggle',
message: _ => `是否初始化为单页应用? (否则为多页应用)`,
initial: true,
active: '是',
inactive: '否'
}
])
2023-02-25 22:22:11 +08:00
console.log()
if (res.projectName === undefined) {
console.log('已取消操作~~')
process.exit()
}
2022-10-10 19:26:24 +08:00
2023-02-25 22:22:11 +08:00
if (res.projectName === '.') {
res.projectName = DEFAULT_NAME
}
2023-02-21 17:44:45 +08:00
2023-02-25 22:22:11 +08:00
targetDir = normalize(targetDir)
2022-10-10 19:26:24 +08:00
2023-02-25 22:22:11 +08:00
console.log('指定的项目名为: %s', cyan(res.projectName))
console.log('项目目录为: %s', cyan(targetDir))
2022-10-10 19:26:24 +08:00
2023-02-25 22:22:11 +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
2023-02-25 22:22:11 +08:00
console.log(cyan('\n初始化项目...'))
2023-04-20 18:59:36 +08:00
let fiteVerion = await getFiteVersion()
2022-10-10 19:26:24 +08:00
2023-02-25 22:22:11 +08:00
console.log('[c---------]', '10%')
2022-10-21 11:46:50 +08:00
if (res.isSPA) {
fs.cp(join(root, './lib/spa'), targetDir)
} else {
fs.cp(join(root, './lib/mpa'), targetDir)
}
2023-02-25 22:22:11 +08:00
console.log('[ooc-------]', '30%')
2022-10-21 11:46:50 +08:00
fs.cp(join(root, './lib/common'), targetDir)
2023-05-30 15:36:23 +08:00
fs.echo(
`
dist
node_modules
._*
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride
`,
join(targetDir, '.gitignore')
)
console.log('[oooooc----]', '60%')
2022-10-21 11:46:50 +08:00
{
fs.echo(
`
import { reactive } from 'vue'
2022-10-21 11:46:50 +08:00
const store = reactive({
foo: 'bar',
version: '${fiteVerion}'
})
2022-10-21 11:46:50 +08:00
export default function (app) {
app.config.globalProperties.$store = store
}
`,
join(targetDir, res.isSPA ? 'src/store.js' : 'src/pages/index/store.js')
)
fs.echo(
`
{
"name": "fite-app",
"type": "module",
"scripts": {
"start": "fite dev",
"build": "fite build",
"build:keep": "fite build --no-clean"
},
"devDependencies": {
"fite": "^${fiteVerion}"
}
}
`,
join(targetDir, 'package.json')
)
2023-04-20 18:59:36 +08:00
}
2023-02-25 22:22:11 +08:00
console.log('[oooooooooo]', '100%')
2023-02-25 22:22:11 +08:00
console.log(cyan('初始化完成, 可依次执行以下命令启动项目: '))
console.log(blue('npm i'))
console.log(blue('npm start'))
2022-10-21 11:46:50 +08:00
})()