UNPKG

1.08 kBJavaScriptView Raw
1#!/usr/bin/env node --harmony
2
3'use strict'
4
5const program = require('commander')
6
7program
8 .option('-p --port [port]', 'port to listen on', 5000)
9 .option('-P --paths [paths]', 'components load path', '.')
10
11program.on('--help', function() {
12 console.log(' Examples:')
13 console.log('')
14 console.log(' $ ocean serve --port 4000')
15 console.log('')
16})
17
18program.parse(process.argv)
19
20
21const path = require('path')
22const fs = require('fs')
23const Koa = require('koa')
24
25const exists = fs.existsSync
26
27
28const cwd = process.cwd()
29const opts = {
30 paths: program.paths,
31 cachePersist: true,
32 dest: 'tmp'
33}
34
35const pkgPath = path.join(cwd, 'package.json')
36
37
38if (!exists(pkgPath)) {
39 console.error('Failed to find package.json')
40 process.exit()
41}
42
43serve()
44
45
46function 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}