UNPKG

2.66 kBJavaScriptView Raw
1#! /usr/bin/env node
2
3const { lstatAsync } = require('./lib/utils')
4
5const { state, init } = require('./lib/state')
6const { checkVersion, log } = require('./lib/utils')
7
8const AVAILIABLE_COMMANDS = ['dev', 'build', 'init']
9
10checkVersion()
11
12const cliParts = process.argv.join('__').split('__-')
13
14let [ , ,command, root ] = cliParts[0].split('__').filter(o => !!o)
15let args = cliParts.length > 1 ? cliParts[1].split('__').filter(o => !!o) : []
16if (!command) command = 'dev'
17if (!root) root = '.'
18
19if (!AVAILIABLE_COMMANDS.includes(command)) { console.error(`command ${command} does not exist`); process.exit(1) }
20
21let options = {}
22args = args.join(' ').split('-').filter(o => !!o).map(o => o.trim())
23args.forEach(arg => {
24 const [ k, v ] = arg.split(' ')
25 if (k !== undefined) {
26 options[k] = v || true
27 }
28})
29
30
31if (options.layout) options.config = { layout: options.layout } // HACK
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
65process.on('unhandledRejection', (r, p) => log('_RED_[WEBO ERROR]', r, p.catch(o => console.log(o.stack))))
66
67
68
69
70async 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
87function 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