fite/index.js

63 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-10-21 14:10:38 +08:00
#!/usr/bin/env node
2022-09-28 18:56:47 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2022/09/28 15:12:45
*/
2022-10-11 19:31:04 +08:00
import fs from 'iofs'
2023-02-21 18:34:48 +08:00
import { join, normalize } from 'path'
2023-02-24 16:08:18 +08:00
import { red, blue } from 'kolorist'
2022-10-10 15:05:30 +08:00
import createServer from './lib/dev.js'
import compile from './lib/prod.js'
2022-09-28 18:56:47 +08:00
2022-10-21 14:10:38 +08:00
const WORK_SPACE = process.cwd()
2022-10-21 14:22:27 +08:00
const IS_WINDOWS = process.platform === 'win32'
2022-09-28 18:56:47 +08:00
const CONFIG_FILE = normalize(join(WORK_SPACE, 'fite.config.js'))
2022-10-21 14:22:27 +08:00
const PROTOCOL = IS_WINDOWS ? 'file://' : ''
2023-02-24 16:08:18 +08:00
const NODE_VERSION = +process.versions.node.split('.').slice(0, 2).join('.')
2022-10-09 19:19:21 +08:00
2022-09-28 18:56:47 +08:00
let args = process.argv.slice(2)
let mode = args.shift() || 'prod'
let clean = args.shift() !== '--no-clean'
2022-09-28 18:56:47 +08:00
2023-02-24 16:08:18 +08:00
if (NODE_VERSION < 16.6) {
console.log(red('Error: 你当前的环境不满足 Vue-live 构建工具的要求'))
console.log(
2023-02-25 22:22:34 +08:00
'Vue-live 需要Node.js版本在 %s 以上, \n你当前的Node.js版本为: %s',
2023-02-24 16:08:18 +08:00
blue('v16.6.0'),
red(process.version),
'\n\n'
)
process.exit()
}
switch (mode) {
2022-09-28 18:56:47 +08:00
case 'dev':
2022-10-21 14:22:27 +08:00
import(PROTOCOL + CONFIG_FILE)
2022-09-28 18:56:47 +08:00
.then(function (conf) {
2023-02-16 15:32:17 +08:00
createServer(WORK_SPACE, conf.default)
2022-09-28 18:56:47 +08:00
})
.catch(err => {
2022-10-11 19:31:04 +08:00
console.log(err)
2022-09-28 18:56:47 +08:00
})
break
case 'build':
2022-10-21 14:22:27 +08:00
import(PROTOCOL + CONFIG_FILE)
2022-10-09 19:19:21 +08:00
.then(function (conf) {
2022-10-11 19:31:04 +08:00
let dist = conf.buildDir || 'dist'
if (clean && fs.isdir(dist)) {
fs.mkdir(dist)
2022-10-11 19:31:04 +08:00
}
2023-02-16 15:32:17 +08:00
compile(WORK_SPACE, dist, conf.default)
2022-10-09 19:19:21 +08:00
})
.catch(err => {
2022-10-11 19:31:04 +08:00
console.log(err)
2022-10-09 19:19:21 +08:00
})
2022-09-28 18:56:47 +08:00
break
}