@@ -53,41 +53,48 @@ const { argv } = require('yargs')
5353 . help ( 'h' ) ;
5454
5555// these are basic options that we want to send
56- const Promise = require ( 'bluebird' ) ;
57- const AMQPTransport = require ( '@microfleet/transport-amqp' ) ;
56+ const { connect } = require ( '@microfleet/transport-amqp' ) ;
5857const omit = require ( 'lodash/omit' ) ;
5958const pick = require ( 'lodash/pick' ) ;
60- const config = require ( '../lib/config' ) . get ( '/' , { env : process . env . NODE_ENV } ) ;
61- // App level code
59+ const getStore = require ( '../lib/config' ) ;
6260
63- const amqpConfig = omit ( config . amqp . transport , [ 'queue' , 'listen' , 'neck' , 'onComplete' , 'bindPersistantQueueToHeadersExchange' ] ) ;
64- const { prefix } = config . router . routes ;
65- const getTransport = ( ) => {
66- console . info ( 'establishing connection to amqp with %j' , amqpConfig ) ;
67- return AMQPTransport . connect ( amqpConfig ) . disposer ( ( amqp ) => amqp . close ( ) ) ;
68- } ;
61+ async function main ( ) {
62+ const store = await getStore ( { env : process . env . NODE_ENV } ) ;
63+ const config = store . get ( '/' ) ;
6964
70- // sends email
71- const sendEmail = ( amqp ) => {
72- const route = `${ prefix } .predefined` ;
73- const basics = pick ( argv , [ 'from' , 'to' , 'cc' , 'bcc' , 'subject' ] ) ;
74- const message = {
75- account : argv . account ,
76- email : { ...basics } ,
77- } ;
65+ const amqpConfig = omit ( config . amqp . transport , [ 'queue' , 'listen' , 'neck' , 'onComplete' , 'bindPersistantQueueToHeadersExchange' ] ) ;
66+ const { prefix } = config . router . routes ;
7867
79- message . email [ argv . type ] = argv . body ;
68+ // sends email
69+ async function sendEmail ( amqp ) {
70+ const route = `${ prefix } .predefined` ;
71+ const basics = pick ( argv , [ 'from' , 'to' , 'cc' , 'bcc' , 'subject' ] ) ;
72+ const message = {
73+ account : argv . account ,
74+ email : { ...basics } ,
75+ } ;
8076
81- // add attachments, ensure they are local
82- if ( Array . isArray ( argv . attachment ) && argv . attachment . length > 0 ) {
83- message . email . attachments = argv . attachment ;
77+ message . email [ argv . type ] = argv . body ;
78+
79+ // add attachments, ensure they are local
80+ if ( Array . isArray ( argv . attachment ) && argv . attachment . length > 0 ) {
81+ message . email . attachments = argv . attachment ;
82+ }
83+
84+ const rsp = await amqp . publishAndWait ( route , message , { timeout : 60000 } ) ;
85+
86+ console . log ( rsp . response ) ;
8487 }
8588
86- return amqp
87- . publishAndWait ( route , message , { timeout : 60000 } )
88- . tap ( ( rsp ) => console . log ( rsp . response ) ) ;
89- } ;
89+ let transport ;
90+ try {
91+ transport = await connect ( amqpConfig ) ;
92+ await sendEmail ( transport ) ;
93+ } catch ( err ) {
94+ console . error ( err ) ;
95+ } finally {
96+ if ( transport ) transport . close ( ) ;
97+ }
98+ }
9099
91- Promise
92- . using ( getTransport ( ) , sendEmail )
93- . done ( ) ;
100+ main ( ) ;
0 commit comments