1 | outcome = require "outcome"
|
2 | _ = require "underscore"
|
3 | beanpoll = require "beanpoll"
|
4 | structr = require "structr"
|
5 | dolce = require "dolce"
|
6 | path = require "path"
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | class TaskMessenger extends beanpoll.Messenger
|
13 |
|
14 | |
15 |
|
16 |
|
17 | _next: (middleware) ->
|
18 |
|
19 | structr.copy @flattenData(true), @request.query
|
20 |
|
21 | middleware.listener.call @, @request.query, @response.success () => @next()
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | class TaskDirector extends beanpoll.Director
|
28 |
|
29 | |
30 |
|
31 |
|
32 | passive: false
|
33 |
|
34 | |
35 |
|
36 |
|
37 | _newMessenger: (request, middleware) -> new TaskMessenger request, middleware, @
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | module.exports = class Commands
|
44 |
|
45 | |
46 |
|
47 |
|
48 | constructor: (@factory) ->
|
49 | @_router = beanpoll.router()
|
50 | @_tasks
|
51 |
|
52 | @_router.use () =>
|
53 | name: 'task',
|
54 | director: new TaskDirector 'task', @_router
|
55 |
|
56 | |
57 |
|
58 |
|
59 | load: (rawTasks, inherit, parent) ->
|
60 |
|
61 |
|
62 | for routeStr of rawTasks
|
63 | for route in @_parseTaskName routeStr, inherit
|
64 |
|
65 | taskData = rawTasks[routeStr]
|
66 |
|
67 | @add @factory.newTask route, taskData, parent
|
68 |
|
69 | @
|
70 |
|
71 |
|
72 | |
73 |
|
74 |
|
75 | _parseTaskName: (routeStr, inherit) ->
|
76 |
|
77 |
|
78 | fixed = routeStr
|
79 | routes = @_router.parse(fixed)
|
80 |
|
81 |
|
82 | for route in routes
|
83 |
|
84 | route = @_extendRoute route, inherit
|
85 |
|
86 | return routes
|
87 |
|
88 | |
89 |
|
90 |
|
91 | _extendRoute: (target, parent) ->
|
92 |
|
93 | return target if not parent
|
94 |
|
95 | parentCopy = structr.copy(parent)
|
96 | realPathStr = @_router.parse.stringifySegments parentCopy.path.segments.concat target.path.segments
|
97 | target.path = @_router.parse.parsePath realPathStr
|
98 |
|
99 | thru = target
|
100 | thru = @_fixThru thru.thru, parent while thru.thru
|
101 | thru.thru = parentCopy.thru
|
102 |
|
103 | target
|
104 |
|
105 | |
106 |
|
107 |
|
108 | _fixThru: (target, route) ->
|
109 |
|
110 | tpv = target.path.value
|
111 | rpv = route.path.value
|
112 | normalized = tpv
|
113 |
|
114 | if tpv.substr(0,1) is '.'
|
115 | normalized = path.normalize(rpv + "/" + tpv)
|
116 |
|
117 | target.path = @_router.parse.parsePath normalized
|
118 | target
|
119 |
|
120 | |
121 |
|
122 |
|
123 | _fixPath: (path) ->
|
124 | path.
|
125 | replace(/:/g,'/').
|
126 | replace(/\{\{(\w+)\}\}/g, ':$1')
|
127 |
|
128 | |
129 |
|
130 |
|
131 | add: (task) ->
|
132 |
|
133 | self = @
|
134 |
|
135 | task.route.type = "task"
|
136 | @_router.on task.route, (target, next) ->
|
137 | target.currentPath = self._router.parse.stringifySegments(@current.path.segments, target)
|
138 | task.run target, next
|
139 | |
140 |
|
141 |
|
142 | run: (path, target, next) ->
|
143 |
|
144 | @_router.
|
145 | request(@_fixPath path).
|
146 | query(target).
|
147 | next (target, next) ->
|
148 | this.response.end()
|
149 | .
|
150 | error(next).
|
151 | success(next).
|
152 | dispatch('task');
|
153 |
|
154 |
|
155 |
|