init
parent
f5efe1945a
commit
26334467d5
|
@ -0,0 +1,215 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
*
|
||||
* @author yutent<yutent.io@gmail.com>
|
||||
* @date 2022/10/10 15:17:36
|
||||
*/
|
||||
|
||||
import { request } from 'https'
|
||||
import { red, cyan, blue } from 'kolorist'
|
||||
import prompts from 'prompts'
|
||||
import fs from 'iofs'
|
||||
import { resolve, join, dirname, normalize } from 'path'
|
||||
|
||||
const NODE_VERSION = process.versions.node.split('.').map(n => +n)
|
||||
const CURRENT_DIR = process.cwd()
|
||||
const root = normalize(
|
||||
dirname(import.meta.url.slice(process.platform === 'win32' ? 8 : 7))
|
||||
)
|
||||
|
||||
const DEFAULT_NAME = 'wkitd-app'
|
||||
|
||||
let targetDir = ''
|
||||
|
||||
if (NODE_VERSION[0] < 16 || (NODE_VERSION[0] === 16 && NODE_VERSION[1] < 6)) {
|
||||
console.log(red('Error: 你当前的环境不满足 wkitd 构建工具的要求'))
|
||||
console.log(
|
||||
'wkitd 需要Node.js版本在 %s 以上, \n你当前的Node.js版本为: %s',
|
||||
blue('v16.6.0'),
|
||||
red(process.version),
|
||||
'\n\n'
|
||||
)
|
||||
process.exit()
|
||||
}
|
||||
|
||||
function isEmpty(dir) {
|
||||
let list = fs.ls(dir)
|
||||
if (list && list.length) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function sleep(num = 1) {
|
||||
return new Promise(resolve => setTimeout(resolve, num * 1000))
|
||||
}
|
||||
|
||||
!(async function () {
|
||||
let res = await prompts([
|
||||
{
|
||||
name: 'isLib',
|
||||
type: 'toggle',
|
||||
message: _ => `是否初始化为库模式? (否则为wkitd框架应用)`,
|
||||
initial: false,
|
||||
active: '是',
|
||||
inactive: '否'
|
||||
},
|
||||
{
|
||||
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()
|
||||
}
|
||||
return null
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'useScss',
|
||||
type: 'toggle',
|
||||
message: _ => `是否启用 SCSS ?`,
|
||||
initial: true,
|
||||
active: '是',
|
||||
inactive: '否'
|
||||
}
|
||||
])
|
||||
|
||||
console.log()
|
||||
|
||||
if (res.projectName === undefined) {
|
||||
console.log('已取消操作~~')
|
||||
process.exit()
|
||||
}
|
||||
|
||||
if (res.projectName === '.') {
|
||||
res.projectName = DEFAULT_NAME
|
||||
}
|
||||
|
||||
if (res.isLib) {
|
||||
res.projectName.replace(/\bwkitd\b/, 'wkit')
|
||||
}
|
||||
|
||||
targetDir = normalize(targetDir)
|
||||
|
||||
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初始化项目...'))
|
||||
|
||||
let fiteVerion = await getFiteVersion()
|
||||
|
||||
console.log('[c---------]', '10%')
|
||||
|
||||
if (res.isLib) {
|
||||
fs.cp(join(root, './lib/spa'), targetDir)
|
||||
}
|
||||
console.log('[ooc-------]', '30%')
|
||||
|
||||
fs.cp(join(root, './lib/common'), targetDir)
|
||||
fs.echo(
|
||||
`
|
||||
dist
|
||||
node_modules
|
||||
|
||||
._*
|
||||
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
`,
|
||||
join(targetDir, '.gitignore')
|
||||
)
|
||||
console.log('[oooooc----]', '60%')
|
||||
|
||||
{
|
||||
fs.echo(
|
||||
`
|
||||
import { createStore } from 'wkitd'
|
||||
|
||||
const store = createStore({
|
||||
foo: 123,
|
||||
bar: 456
|
||||
})
|
||||
|
||||
export default store
|
||||
`,
|
||||
join(targetDir, 'src/store.js')
|
||||
)
|
||||
|
||||
fs.echo(
|
||||
`
|
||||
import { createRouter, createWebHistory, createWebHashHistory } from 'wkitd'
|
||||
|
||||
import './views/home.js'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(), // default is createWebHashHistory()
|
||||
routes: [
|
||||
{
|
||||
path: '/',
|
||||
name: 'wc-home' // element tagname !importent
|
||||
},
|
||||
{
|
||||
path: '/about',
|
||||
name: 'wc-about',
|
||||
component: () => import('./views/about.js')
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
export default router
|
||||
`,
|
||||
join(targetDir, 'src/store.js')
|
||||
)
|
||||
|
||||
fs.echo(
|
||||
`
|
||||
{
|
||||
"name": "${res.projectName}",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "wkitd dev",
|
||||
"build": "wkitd build",
|
||||
"build:keep": "wkitd build --no-clean"
|
||||
},
|
||||
|
||||
}
|
||||
`,
|
||||
join(targetDir, 'package.json')
|
||||
)
|
||||
}
|
||||
console.log('[oooooooooo]', '100%')
|
||||
|
||||
console.log(cyan('初始化完成, 可依次执行以下命令启动项目: '))
|
||||
console.log(blue('npm i'))
|
||||
console.log(blue('npm start'))
|
||||
})()
|
14
package.json
14
package.json
|
@ -1,4 +1,16 @@
|
|||
{
|
||||
"name": "create-wkitd",
|
||||
"version": "0.0.0"
|
||||
"type": "module",
|
||||
"version": "0.0.1",
|
||||
"bin": {
|
||||
"create-wkitd": "index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"iofs": "^1.5.2",
|
||||
"prompts": "^2.4.2",
|
||||
"kolorist": "^1.6.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16.14.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue