1 | #!/usr/bin/env node
|
2 | 'use strict'
|
3 | const path = require('path')
|
4 | const program = require('commander')
|
5 | const base = require('../package.json')
|
6 | const IP = require('../lib/util/IP')
|
7 | const HOSTS = require('../lib/util/HOSTS')
|
8 | const fs = require('fs')
|
9 | const { getConfig, setConfig } = require('../lib/conf/F2E_CONFIG')
|
10 |
|
11 | const F2E_CONFIG = getConfig()
|
12 | program
|
13 | .version(base.version)
|
14 |
|
15 | program
|
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 | })
|
25 | program
|
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 | })
|
40 | program
|
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 |
|
54 | program
|
55 | .command('ip')
|
56 | .description('获取本地IP')
|
57 | .action(options => console.log(IP))
|
58 |
|
59 | program
|
60 | .command('host')
|
61 | .description('加入host映射')
|
62 | .action(options => {
|
63 | HOSTS(options)
|
64 | })
|
65 |
|
66 | program
|
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 | // 开始解析用户输入的命令
|
78 | program.parse(process.argv)
|