33const fs = require ( 'fs' )
44const path = require ( 'path' )
55
6+ function TicketError ( message ) {
7+ this . name = 'TicketError' ;
8+ this . message = message || 'Ticketing error' ;
9+ this . stack = ( new Error ( ) ) . stack ;
10+ }
11+ TicketError . prototype = Object . create ( Error . prototype ) ;
12+ TicketError . prototype . constructor = TicketError ;
13+
614const Sample = {
715 assignee : null ,
816 closed : null ,
@@ -17,76 +25,68 @@ const Sample = {
1725const actions = [
1826{
1927 action : ( _tickets , _message ) => 'DEFAULT ACTION' ,
20- regexp : / ^ $ / gi ,
28+ regexp : / ^ $ / i ,
2129 reply : ( message , _output ) => `Yes ${ message . userName } ?` ,
2230} ,
2331{
2432 action : ( _tickets , _message ) => help ( ) ,
25- regexp : / h e l p / gi ,
33+ regexp : / h e l p / i ,
2634 reply : ( message , output ) => `\n${ output } ` ,
2735} ,
2836{
2937 action : ( tickets , _message , id ) => closeTicket ( tickets , id ) ,
30- regexp : / c l o s e # ( [ 0 - 9 ] * ) / gi ,
38+ regexp : / c l o s e # ( [ 0 - 9 ] * ) / i ,
3139 reply : ( message , output ) => `ticket #${ output . id } is closed` ,
3240} ,
3341{
3442 action : ( tickets , message , content ) =>
3543 openTicket ( tickets , message , content ) ,
36- regexp : / p l e a s e ( .* ) / gi ,
44+ regexp : / p l e a s e ( .* ) / i ,
3745 reply : ( message , output ) => `will ${ output . content } (ticket #${ output . id } )` ,
3846} ,
3947{
4048 action : ( tickets , _message , id ) => findTicket ( tickets , id ) ,
41- regexp : / s h o w # ( [ 0 - 9 ] * ) / gi ,
49+ regexp : / s h o w # ( [ 0 - 9 ] * ) / i ,
4250 reply : ( message , output ) => showTicket ( output ) ,
4351} ,
4452{
4553 action : ( tickets , message , id ) => assign ( tickets , id , message . userName ) ,
46- regexp : / t a k e # ( [ 0 - 9 ] * ) / gi ,
54+ regexp : / t a k e # ( [ 0 - 9 ] * ) / i ,
4755 reply : ( message , output ) => `ticket #${ output . id } is assigned to ${ output . assignee } ` ,
4856} ,
4957{
5058 /* eslint-disable max-params */
5159 action : ( tickets , _message , id , assignee ) => assign ( tickets , id , assignee ) ,
52- regexp : / a s s i g n # ( [ 0 - 9 ] * ) t o ( \w * ) / gi ,
60+ regexp : / a s s i g n # ( [ 0 - 9 ] * ) t o ( \w * ) / i ,
5361 reply : ( message , output ) => `ticket #${ output . id } is assigned to ${ output . assignee } ` ,
5462} ,
5563{
5664 action : ( tickets , _message ) => tickets ,
57- regexp : / d e b u g / gi ,
65+ regexp : / d e b u g / i ,
5866 reply : ( message , output ) => JSON . stringify ( output , null , 4 ) ,
5967} ,
6068{
6169 action : ( tickets , _message ) => tickets ,
62- regexp : / t o d o / gi ,
70+ regexp : / t o d o / i ,
6371 reply : ( message , output ) => showTickets ( output , [ 'open' ] ) ,
6472} ,
6573{
6674 action : ( tickets , _message ) => tickets ,
67- regexp : / h i s t o r y / gi ,
75+ regexp : / h i s t o r y / i ,
6876 reply : ( message , output ) => showTickets ( output , [ 'open' , 'closed' ] ) ,
6977} ,
7078{
7179 action : ( tickets , _message ) => forget ( tickets ) ,
72- regexp : / f o r g e t i t / gi ,
80+ regexp : / f o r g e t i t / i ,
7381 reply : ( message , output ) => `deleted ${ output } tickets` ,
7482} ,
7583{
7684 action : ( tickets , _message , type ) => gimme ( type ) ,
77- regexp : / g i m m e \s * a * n * \s * ( \w * ) / gi ,
85+ regexp : / g i m m e \s * a * n * \s * ( \w * ) / i ,
7886 reply : ( message , output ) => `${ message . userName } ${ output [ 0 ] . toLowerCase ( ) } ${ output . substr ( 1 ) } ` ,
7987} ,
8088]
8189
82- function TicketError ( message ) {
83- this . name = 'TicketError' ;
84- this . message = message || 'Ticketing error' ;
85- this . stack = ( new Error ( ) ) . stack ;
86- }
87- TicketError . prototype = Object . create ( Error . prototype ) ;
88- TicketError . prototype . constructor = TicketError ;
89-
9090function createTicket ( ) {
9191 return JSON . parse ( JSON . stringify ( Sample ) )
9292}
@@ -169,17 +169,22 @@ function getRandomInt(min, max) {
169169var sources = { }
170170
171171function gimme ( type ) {
172+ const typePath = path . basename ( `${ type } .txt` )
173+ /* eslint-disable no-sync */
174+ if ( ! fs . existsSync ( typePath ) ) {
175+ throw new TicketError ( `I don't know how to give you ${ type } !` )
176+ }
172177 if ( ! sources [ type ] ) {
173178 /* eslint-disable no-sync */
174- const content = fs . readFileSync ( path . basename ( ` ${ type } .txt` ) )
179+ const content = fs . readFileSync ( typePath )
175180 sources [ type ] = content . toString ( ) . split ( '\n' )
176181 }
177182 const saying = sources [ type ] [ getRandomInt ( 0 , sources [ type ] . length ) ]
178183 return saying
179184}
180185
181186/**
182- * Callback for find.
187+ * Callback for Array. find.
183188 * @param action action object
184189 * @this object with content and result
185190 * @returns true if match regexp, false otherwise
@@ -201,14 +206,18 @@ function process(tickets, message) {
201206 result : null
202207 }
203208 const action = actions . find ( searchRegexp , findParams )
209+
204210 var reply ;
205211 if ( action ) {
206- var output = action . action ( tickets , message , ...findParams . result )
207- // If(!(output instanceof Object) && !output) {
208- // Reply = `${JSON.stringify(action)} did not return any output`
209- // } else {
212+ try {
213+ var output = action . action ( tickets , message , ...findParams . result )
210214 reply = `${ action . reply . bind ( output ) ( message , output ) } `
211- // }
215+ } catch ( e ) {
216+ if ( e instanceof TicketError ) {
217+ return e . message
218+ }
219+ throw e
220+ }
212221 } else {
213222 reply = `I don't understand: ${ message . content } , can you try again?`
214223 }
@@ -227,16 +236,25 @@ function store(storePath, tickets) {
227236}
228237
229238function load ( storePath ) {
230- try {
239+ /* eslint-disable no-sync */
240+ if ( fs . existsSync ( storePath ) ) {
231241 /* eslint-disable no-sync */
232- const datastore = fs . readFileSync ( storePath )
233- return JSON . parse ( datastore )
234- } catch ( e ) {
235- return createTickets ( )
242+ const datastore = fs . readFileSync ( storePath ) . toString ( )
243+ try {
244+ return JSON . parse ( datastore )
245+ } catch ( e ) {
246+ if ( e instanceof SyntaxError ) {
247+ throw new TicketError ( `invalid content of file: ${ storePath } ` )
248+ } else {
249+ throw e
250+ }
251+ }
236252 }
253+ return createTickets ( )
237254}
238255
239256module . exports = {
257+ TicketError,
240258 createTickets,
241259 load,
242260 openTicket,
0 commit comments