UNPKG

2.49 kBPlain TextView Raw
1#!/usr/bin/env node
2'use strict'
3const path = require('path')
4const program = require('commander')
5const base = require('../package.json')
6const IP = require('../lib/util/IP')
7const HOSTS = require('../lib/util/HOSTS')
8const fs = require('fs')
9const { getConfig, setConfig } = require('../lib/conf/F2E_CONFIG')
10
11const F2E_CONFIG = getConfig()
12program
13 .version(base.version)
14
15program
16 .command('conf')
17 .description(`生成 ${F2E_CONFIG} 配置文件模板`)
18 .action(options => {
19 fs.readFile(path.join(__dirname, '../', F2E_CONFIG), function (err, data) {
20 !err && fs.writeFile(path.resolve(F2E_CONFIG), data.toString().replace(`import('./index')`, `import('f2e-server')`), function (err) {
21 err && console.log(err)
22 })
23 })
24 })
25program
26 .command('build')
27 .option('-w, --watch <watch>', 'build with watching-update')
28 .option('-c, --config <config>', `config file. default to ${F2E_CONFIG}`)
29 .option('-t, --timeout <timeout>', 'build-in with timeout(1000ms) after build-out')
30 .description('编译输出')
31 .action(({ watch, timeout, config }) => {
32 timeout = (timeout | 0) || 1000
33 let beginTime = Date.now() + timeout
34 console.log('build begin...')
35 setConfig(config)
36 require('../lib/util/build')(function () {
37 console.log('build finished' + (watch ? '!\n & building...' : ' with: ' + (Date.now() - beginTime) + 'ms' ))
38 }, {watch, timeout})
39 })
40program
41 .command('start')
42 .option('-p, --port <port>', 'http server port')
43 .option('-c, --config <config>', `config file. default to ${F2E_CONFIG}`)
44 .option('-H, --host <host>', 'port 80 & set local host')
45 .option('-B, --build <build>', 'build with true')
46 .description('启动服务器')
47 .action(options => {
48 if (options.config) {
49 setConfig(options.config)
50 }
51 require('../index')(options)
52 })
53
54program
55 .command('ip')
56 .description('获取本地IP')
57 .action(options => console.log(IP))
58
59program
60 .command('host')
61 .description('加入host映射')
62 .action(options => {
63 HOSTS(options)
64 })
65
66program
67 .command('create')
68 .description('创建单页App')
69 .action(appname => {
70 console.log(`
71 1. 下载 https://github.com/f2e-server/app/archive/master.zip
72 2. 解压后进入目录
73 3. npm i
74 4. npm start
75`)
76 })
77// 开始解析用户输入的命令
78program.parse(process.argv)