1 | #! /usr/bin/env node
|
2 |
|
3 | const { lstatAsync } = require('./lib/utils')
|
4 |
|
5 | const { state, init } = require('./lib/state')
|
6 | const { checkVersion, log } = require('./lib/utils')
|
7 |
|
8 | const AVAILIABLE_COMMANDS = ['dev', 'build', 'init']
|
9 |
|
10 | checkVersion()
|
11 |
|
12 | const cliParts = process.argv.join('__').split('__-')
|
13 |
|
14 | let [ , ,command, root ] = cliParts[0].split('__').filter(o => !!o)
|
15 | let args = cliParts.length > 1 ? cliParts[1].split('__').filter(o => !!o) : []
|
16 | if (!command) command = 'dev'
|
17 | if (!root) root = '.'
|
18 |
|
19 | if (!AVAILIABLE_COMMANDS.includes(command)) { console.error(`command ${command} does not exist`); process.exit(1) }
|
20 |
|
21 | let options = {}
|
22 | args = args.join(' ').split('-').filter(o => !!o).map(o => o.trim())
|
23 | args.forEach(arg => {
|
24 | const [ k, v ] = arg.split(' ')
|
25 | if (k !== undefined) {
|
26 | options[k] = v || true
|
27 | }
|
28 | })
|
29 |
|
30 |
|
31 | if (options.layout) options.config = { layout: options.layout }
|
32 |
|
33 | ;(async () => {
|
34 | const entryType = await resolveEntry(root)
|
35 | if (!entryType) { console.error(`Entry '${root}' is not valid`); process.exit() }
|
36 | state.entryType = entryType
|
37 |
|
38 | const version = require('./package.json').version
|
39 | if (options.v) { console.log('version ' + version); process.exit() }
|
40 |
|
41 | const started = new Date()
|
42 | log(`_GREEN_WEBO ${version} started`)
|
43 |
|
44 | state.clientRoot = root
|
45 |
|
46 | if (['dev', 'build'].includes(command) && entryType === 'express') {
|
47 | let expressRoot = await require('./lib/dev/express')(root, command)
|
48 | state.serverRoot = root
|
49 | root = calcCommonRoot(root, expressRoot)
|
50 | state.clientRoot = expressRoot
|
51 | }
|
52 |
|
53 | if (['dev', 'build'].includes(command)) await init(command, root, options)
|
54 |
|
55 |
|
56 | await require(`./lib/${command}/${command}`)()
|
57 |
|
58 |
|
59 | const ended = new Date()
|
60 | log('_GREEN_WEBO loaded at', (ended.getTime() - started.getTime()) / 1000 + 's')
|
61 |
|
62 | })()
|
63 |
|
64 |
|
65 | process.on('unhandledRejection', (r, p) => log('_RED_[WEBO ERROR]', r, p.catch(o => console.log(o.stack))))
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | async function resolveEntry(entry) {
|
71 | let result
|
72 | let entryType
|
73 | try {
|
74 | result = await lstatAsync(entry)
|
75 | if (result.isDirectory()) entryType = 'static'
|
76 | if (entry.endsWith('.html')) entryType = 'static'
|
77 | if (entry.endsWith('.js')) entryType = 'express'
|
78 | } catch (error) {
|
79 | try {
|
80 | result = await lstatAsync(entry + '.js')
|
81 | entryType = 'express'
|
82 | } catch (error) { }
|
83 | }
|
84 | return entryType
|
85 | }
|
86 |
|
87 | function calcCommonRoot(static, express) {
|
88 | const staticParts = static.split('/')
|
89 | const expressParts = express.split('/')
|
90 | let i = 0
|
91 | while (staticParts[i] === expressParts[i]) i++
|
92 | return staticParts.slice(0, i).join('/')
|
93 | } |
\ | No newline at end of file |