1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 | const async = require('async')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const _route = function(route) {
|
14 |
|
15 |
|
16 | route = Object.assign({}, route)
|
17 |
|
18 |
|
19 |
|
20 | if (route.name==null || route.name==='') throw new Error('Missing name property in route')
|
21 |
|
22 |
|
23 | if (route.path==null) throw new Error(`Missing path property in route '${ route.name }'`)
|
24 | if (route.handler==null) throw new Error(`Missing handler property in route '${ route.name }'`)
|
25 |
|
26 |
|
27 | if (path.isAbsolute(route.path)===true) throw new Error(`Path in route '${ route.name }' must be relative`)
|
28 |
|
29 |
|
30 | if (typeof route.handler==='string') route.handler = require(route.handler)
|
31 | if (typeof route.handler!=='function') throw new Error(`Handler in route '${ route.name }' is not a function nor a string`)
|
32 |
|
33 |
|
34 | if (route.opts==null) route.opts = {}
|
35 |
|
36 | return route
|
37 |
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | const _path = function(filePath) {
|
47 |
|
48 |
|
49 | filePath = path.resolve(filePath)
|
50 |
|
51 | return filePath
|
52 |
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const _opts = function(opts) {
|
62 |
|
63 |
|
64 | opts = Object.assign({}, opts)
|
65 |
|
66 |
|
67 | opts.ignore = (Array.isArray(opts.ignore)===true ? opts.ignore : [])
|
68 | opts.polling = (opts.polling===true ? true : false)
|
69 | opts.verbose = (opts.verbose===true ? true : false)
|
70 | opts.open = (opts.open===false ? false : true)
|
71 |
|
72 | return opts
|
73 |
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | const _next = function(next) {
|
83 |
|
84 |
|
85 | if (typeof next!=='function') next = (err) => {
|
86 |
|
87 | if (err!=null) throw err
|
88 |
|
89 | }
|
90 |
|
91 | return next
|
92 |
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | module.exports = {
|
99 | route : _route,
|
100 | path : _path,
|
101 | opts : _opts,
|
102 | next : _next
|
103 | } |
\ | No newline at end of file |