1 | #!/usr/bin/env node --harmony
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const program = require('commander')
|
6 |
|
7 | program
|
8 | .option('-p --port [port]', 'port to listen on', 5000)
|
9 | .option('-P --paths [paths]', 'components load path', '.')
|
10 |
|
11 | program.on('--help', function() {
|
12 | console.log(' Examples:')
|
13 | console.log('')
|
14 | console.log(' $ ocean serve --port 4000')
|
15 | console.log('')
|
16 | })
|
17 |
|
18 | program.parse(process.argv)
|
19 |
|
20 |
|
21 | const path = require('path')
|
22 | const fs = require('fs')
|
23 | const Koa = require('koa')
|
24 |
|
25 | const exists = fs.existsSync
|
26 |
|
27 |
|
28 | const cwd = process.cwd()
|
29 | const opts = {
|
30 | paths: program.paths,
|
31 | cachePersist: true,
|
32 | dest: 'tmp'
|
33 | }
|
34 |
|
35 | const pkgPath = path.join(cwd, 'package.json')
|
36 |
|
37 |
|
38 | if (!exists(pkgPath)) {
|
39 | console.error('Failed to find package.json')
|
40 | process.exit()
|
41 | }
|
42 |
|
43 | serve()
|
44 |
|
45 |
|
46 | function serve() {
|
47 | const app = new Koa()
|
48 |
|
49 | const serveStatic = require('koa-static')
|
50 | app.use(serveStatic(cwd))
|
51 | app.use(serveStatic(path.join(cwd, 'tmp')))
|
52 |
|
53 | const porter = require('@cara/porter')
|
54 | app.use(porter(opts))
|
55 |
|
56 | app.listen(program.port, function() {
|
57 | console.log('Server started at', program.port)
|
58 | })
|
59 | }
|