22// and adjust them to match the list of provided aliases; removing ones that
33// don't belong and adding ones that are new
44
5- const equal = require ( 'deep-equal' )
6- , after = require ( 'after' )
7- , xtend = require ( 'xtend' )
8- , listRoutes = require ( './list-routes' )
9- , addRoute = require ( './add-route' )
10- , deleteRoute = require ( './delete-route' )
11-
5+ const equal = require ( 'deep-equal' )
6+ const after = require ( 'after' )
7+ const xtend = require ( 'xtend' )
8+ const listRoutes = require ( './list-routes' )
9+ const addRoute = require ( './add-route' )
10+ const deleteRoute = require ( './delete-route' )
1211
1312function toExpression ( domain , alias ) {
1413 return `match_recipient("${ alias . from } @${ domain } ")`
1514}
1615
17-
1816function toActions ( alias ) {
19- var to = Array . isArray ( alias . to ) ? alias . to : [ alias . to ]
17+ const to = Array . isArray ( alias . to ) ? alias . to : [ alias . to ]
2018 return to . map ( function ( to ) {
2119 return `forward("${ to } ")`
22- } ) . concat ( [ ` stop()` ] )
20+ } ) . concat ( [ ' stop()' ] )
2321}
2422
25-
2623function diff ( domain , a1 , a2 ) {
2724 a2 = a2 . slice ( )
2825
2926 return a1 . filter ( function ( alias1 ) {
3027 return ! a2 . some ( function ( alias2 , i ) {
31- if ( alias2 . expression == alias1 . expression && equal ( alias2 . actions , alias1 . actions ) ) {
28+ if ( alias2 . expression === alias1 . expression && equal ( alias2 . actions , alias1 . actions ) ) {
3229 a2 . splice ( i , 1 ) // remove it so we don't match duplicates
3330 return true
3431 }
3532 } )
3633 } )
3734}
3835
39-
4036function updateAliases ( domain , creds , aliases , dryRun , callback ) {
4137 function adjustRoutes ( routes ) {
42-
43- var current = routes . items
38+ const current = routes . items
4439
4540 // adjust the incoming aliases to have .expression and .actions properties
4641 aliases = aliases . map ( function ( alias ) {
4742 return xtend ( alias , {
48- expression : toExpression ( domain , alias )
49- , actions : toActions ( alias )
43+ expression : toExpression ( domain , alias ) ,
44+ actions : toActions ( alias )
5045 } )
5146 } )
5247
53- var toAdd = diff ( domain , aliases , current ) // this diff gives us new additions
54- , toRemove = diff ( domain , current , aliases ) // this diff gives us stale routes
55- , done = after ( toAdd . length + toRemove . length , callback )
48+ const toAdd = diff ( domain , aliases , current ) // this diff gives us new additions
49+ const toRemove = diff ( domain , current , aliases ) // this diff gives us stale routes
50+ const done = after ( toAdd . length + toRemove . length , callback )
5651
5752 console . log ( `${ toAdd . length } route(s) to add` )
5853 console . log ( `${ toRemove . length } route(s) to remove` )
5954
6055 toAdd . forEach ( function ( alias ) {
61- var to = Array . isArray ( alias . to ) ? alias . to : [ alias . to ]
56+ const to = Array . isArray ( alias . to ) ? alias . to : [ alias . to ]
6257 console . log ( `Adding ${ alias . from } -> ${ to . join ( ', ' ) } ...` )
6358 if ( ! dryRun ) {
6459 addRoute ( domain , creds , alias . from , alias . expression , alias . actions , done )
@@ -74,11 +69,9 @@ function updateAliases (domain, creds, aliases, dryRun, callback) {
7469 }
7570
7671 listRoutes ( domain , creds , function ( err , routes ) {
77- if ( err )
78- throw err
72+ if ( err ) { throw err }
7973 adjustRoutes ( routes )
8074 } )
8175}
8276
83-
8477module . exports = updateAliases
0 commit comments