11'use strict' ;
22
33
4- const ftp = require ( 'vinyl -ftp' ) ;
4+ const ftp = require ( 'basic -ftp' ) ;
55const path = require ( 'path' ) ;
66const plumber = require ( 'gulp-plumber' ) ;
77const size = require ( 'gulp-size' ) ;
@@ -13,10 +13,42 @@ const { parseAsync } = require('json2csv');
1313
1414const vaultFields = require ( './helpers/vault_fields' ) . vaultFields ;
1515
16- module . exports = function ( gulp , options ) {
16+ module . exports = function ( gulp , options ) {
1717
18- function prepareVaultData ( keyMessages ) {
19- return new Promise ( function ( resolve , reject ) {
18+ async function ftpDeploy ( files , ftpPath = '' ) {
19+
20+ const client = new ftp . Client ( ) ;
21+
22+ client . trackProgress ( info => {
23+ utils . log . log ( '' ) ;
24+ utils . log . log ( ' File: ' , info . name ) ;
25+ utils . log . log ( ' Type: ' , info . type ) ;
26+ utils . log . log ( ' Transferred: ' , info . bytes ) ;
27+ utils . log . log ( ' Transferred Overall: ' , info . bytesOverall ) ;
28+ } ) ;
29+
30+ client . ftp . verbose = options . verbose ;
31+ try {
32+ await client . access ( {
33+ host : options . ftp . host ,
34+ user : options . ftp . user ,
35+ password : options . ftp . pass ,
36+ port : options . ftp . port || 21 ,
37+ secure : options . ftp . secure ,
38+ secureOptions : { rejectUnauthorized : false }
39+ } ) ;
40+
41+ for ( const file of files ) {
42+ await client . uploadFrom ( path . join ( options . paths . deploy , file ) , path . join ( ftpPath , file ) ) ;
43+ }
44+ } catch ( err ) {
45+ console . log ( err ) ;
46+ }
47+ client . close ( ) ;
48+ }
49+
50+ function prepareVaultData ( keyMessages ) {
51+ return new Promise ( function ( resolve , reject ) {
2052 const buildArray = [ ] ;
2153
2254 try {
@@ -57,92 +89,31 @@ module.exports = function (gulp, options) {
5789 } ) ;
5890 }
5991
60- function handleFTPZips ( ) {
61- return new Promise ( function ( resolve , reject ) {
6292
63- utils . log . log ( '⤷ Deploying zipped files' ) ;
93+ function handleFTPZips ( ) {
6494
65- const conn = ftp . create ( {
66- debug : utils . log . debug ,
67- host : options . ftp . host ,
68- log : utils . log . verbose ,
69- parallel : 4 ,
70- password : options . ftp . pass ,
71- port : options . ftp . port || 21 ,
72- secure : options . ftp . secure || false ,
73- secureOptions : { rejectUnauthorized : false } ,
74- read : false ,
75- user : options . ftp . user ,
76- maxConnections : 11
77- } ) ;
95+ utils . log . log ( ' ⤷ Deploying zipped files' ) ;
7896
79- /**
80- * Overide function as there's an error in the vinyl-ftp module.
81- * Error: when checking the remote's directory (based on glob's path), if there's a symbolic link (a file),
82- * an error is thrown.
83- */
84- conn . _mkdirp = {
85- get : function ( path , cb ) {
86- return cb ( ) ;
87- }
88- } ;
97+ return utils . getFiles ( path . join ( process . cwd ( ) , options . paths . deploy ) )
98+ . then ( ( files ) => files . filter ( el => path . extname ( el ) === '.zip' ) )
99+ . then ( ftpDeploy ) ;
89100
90- gulp . src ( path . join ( '**' , '*.zip' ) , {
91- buffer : false ,
92- cwd : options . paths . deploy ,
93- } )
94- . pipe ( plumber ( ) )
95- . pipe ( conn . dest ( `${ options . ftp . remotePath } ` ) )
96- . on ( 'error' , function ( err ) {
97- reject ( err ) ;
98- } )
99- . on ( 'end' , function ( ) {
100- console . log ( 'FTP Zips complete' ) ;
101- resolve ( ) ;
102- } ) ;
103- } ) ;
104101 }
105102
106- function handleFTPCtls ( ) {
107- return new Promise ( function ( resolve , reject ) {
103+ function handleFTPCtls ( ) {
108104
109- utils . log . log ( '⤷ Deploying control files' ) ;
105+ utils . log . log ( '' ) ;
106+ utils . log . log ( ' ⤷ Deploying control files' ) ;
110107
111- const conn = ftp . create ( {
112- debug : utils . log . debug ,
113- host : options . ftp . host ,
114- log : utils . log . verbose ,
115- parallel : 4 ,
116- password : options . ftp . pass ,
117- port : options . ftp . port || 21 ,
118- read : false ,
119- secure : options . ftp . secure || false ,
120- secureOptions : { rejectUnauthorized : false } ,
121- user : options . ftp . user ,
122- maxConnections : 11
123- } ) ;
124-
125-
126- gulp . src ( path . join ( options . paths . deploy , '**' , '*.ctl' ) , {
127- buffer : false
128- } )
129- . pipe ( plumber ( ) )
130- . pipe ( conn . dest ( `${ options . ftp . remotePath } /ctlfile` ) )
131- . on ( 'error' , function ( err ) {
132- console . log ( err ) ;
133- reject ( err ) ;
134- } )
135- . on ( 'end' , function ( ) {
136- console . log ( 'done ctrl ftp' ) ;
137- resolve ( ) ;
138- } ) ;
139- } ) ;
108+ return utils . getFiles ( path . join ( process . cwd ( ) , options . paths . deploy ) )
109+ . then ( ( files ) => files . filter ( el => path . extname ( el ) === '.ctl' ) )
110+ . then ( ( files ) => ftpDeploy ( files , 'ctlfile' ) ) ;
140111 }
141112
142- function handleStaging ( keyMessages ) {
113+ function handleStaging ( keyMessages ) {
143114
144- function keyMessageCompress ( keyMessage ) {
145- return new Promise ( function ( resolve , reject ) {
115+ function keyMessageCompress ( keyMessage ) {
116+ return new Promise ( function ( resolve , reject ) {
146117
147118 let formattedKeyMessage = keyMessage [ 'key_message' ] ;
148119
@@ -151,80 +122,82 @@ module.exports = function (gulp, options) {
151122 }
152123
153124 gulp . src ( '**/*' , {
154- cwd : path . join ( process . cwd ( ) , options . paths . dist , formattedKeyMessage ) ,
155- base : options . paths . dist
156- } )
157- . pipe ( plumber ( ) )
158- . pipe ( zip ( formattedKeyMessage + '.zip' ) )
159- . pipe ( size ( {
160- title : util . colors . green . bold ( '⤷ Zipping Key Message: ' ) + util . colors . yellow . bold ( formattedKeyMessage ) ,
161- showFiles : options . verbose
162- } ) )
163- . pipe ( gulp . dest ( options . paths . deploy ) )
164- . on ( 'error' , function ( err ) {
165- reject ( err ) ;
166- } )
167- . on ( 'end' , function ( ) {
168- resolve ( ) ;
169- } ) ;
125+ cwd : path . join ( process . cwd ( ) , options . paths . dist , formattedKeyMessage ) ,
126+ base : options . paths . dist
127+ } )
128+ . pipe ( plumber ( ) )
129+ . pipe ( zip ( formattedKeyMessage + '.zip' ) )
130+ . pipe ( size ( {
131+ title : util . colors . green . bold ( '⤷ Zipping Key Message: ' ) + util . colors . yellow . bold ( formattedKeyMessage ) ,
132+ showFiles : options . verbose
133+ } ) )
134+ . pipe ( gulp . dest ( options . paths . deploy ) )
135+ . on ( 'error' , function ( err ) {
136+ reject ( err ) ;
137+ } )
138+ . on ( 'end' , function ( ) {
139+ resolve ( ) ;
140+ } ) ;
170141 } ) ;
171142 }
172- function keyMessageControlFile ( keyMessage ) {
143+
144+ function keyMessageControlFile ( keyMessage ) {
173145 let formattedKeyMessage = keyMessage [ 'key_message' ] ;
174146
175147 if ( options . clm . product && options . clm . product . name ) {
176148 formattedKeyMessage = options . clm . product . name + options . clm . product . suffix + formattedKeyMessage ;
177149 }
178150
179151 const controlFile = 'USER=' + options . ftp . user + '\n' +
180- 'PASSWORD=' + options . ftp . pass + '\n' +
181- 'EMAIL=' + options . ftp . email + '\n' +
182- 'NAME=' + formattedKeyMessage + '\n' +
183- 'Description_vod__c=' + keyMessage [ 'description' ] + '\n' +
184- 'FILENAME=' + formattedKeyMessage + '.zip' ;
152+ 'PASSWORD=' + options . ftp . pass + '\n' +
153+ 'EMAIL=' + options . ftp . email + '\n' +
154+ 'NAME=' + formattedKeyMessage + '\n' +
155+ 'Description_vod__c=' + keyMessage [ 'description' ] + '\n' +
156+ 'FILENAME=' + formattedKeyMessage + '.zip' ;
185157
186158 return utils . setFile ( path . join ( options . paths . deploy , formattedKeyMessage + '.ctl' ) , controlFile ) ;
187159 }
188160
189161 return utils . mkFolder ( options . paths . deploy )
190- . then ( ( ) => {
162+ . then ( ( ) => {
191163
192- const promises = [ ] ;
164+ const promises = [ ] ;
193165
194- keyMessages . push ( { key_message : 'shared' } ) ;
195- for ( const km of keyMessages ) {
196- promises . push ( keyMessageCompress ( km ) ) ;
197- promises . push ( keyMessageControlFile ( km ) ) ;
198- }
166+ keyMessages . push ( { key_message : 'shared' } ) ;
167+ for ( const km of keyMessages ) {
168+ promises . push ( keyMessageCompress ( km ) ) ;
169+ promises . push ( keyMessageControlFile ( km ) ) ;
170+ }
199171
200- return Promise . all ( promises ) ;
201- } ) ;
172+ return Promise . all ( promises ) ;
173+ } ) ;
202174 }
203175
204176
205- gulp . task ( 'deploy' , function ( ) {
177+ gulp . task ( 'deploy' , function ( ) {
206178
207179 return handleFTPZips ( )
208- . then ( ( ) => handleFTPCtls ( ) )
209- . then ( ( ) => utils . log . success ( 'Done Deploying Key Messages' ) ) ;
180+ . then ( ( ) => handleFTPCtls ( ) )
181+ . then ( ( ) => utils . log . log ( '' ) )
182+ . then ( ( ) => utils . log . success ( 'Done Deploying Key Messages' ) ) ;
210183
211184 } ) ;
212185
213- gulp . task ( 'stage' , [ 'clean:deploy' , 'build' ] , function ( ) {
186+ gulp . task ( 'stage' , [ 'clean:deploy' , 'build' ] , function ( ) {
214187
215188 return handleStaging ( options . clm . key_messages )
216- . then ( ( ) => utils . log . success ( 'Done Staging Key Messages' ) ) ;
189+ . then ( ( ) => utils . log . success ( 'Done Staging Key Messages' ) ) ;
217190 } ) ;
218191
219- gulp . task ( 'stage-vault' , function ( ) {
192+ gulp . task ( 'stage-vault' , function ( ) {
220193
221194 utils . log . log ( '⤷ Generating Veeva Vault CSV file' ) ;
222195
223196 return utils . mkFolder ( options . paths . deploy )
224- . then ( ( ) => prepareVaultData ( options . clm . key_messages ) )
225- . then ( ( buildArray ) => parseAsync ( buildArray , vaultFields ) )
226- . then ( ( csv ) => utils . setFile ( path . join ( process . cwd ( ) , options . paths . deploy , 'VAULT_CSV.csv' ) , csv ) )
227- . then ( ( ) => utils . log . success ( 'Veeva Vault CSV file has been successfully generated' ) ) ;
197+ . then ( ( ) => prepareVaultData ( options . clm . key_messages ) )
198+ . then ( ( buildArray ) => parseAsync ( buildArray , vaultFields ) )
199+ . then ( ( csv ) => utils . setFile ( path . join ( process . cwd ( ) , options . paths . deploy , 'VAULT_CSV.csv' ) , csv ) )
200+ . then ( ( ) => utils . log . success ( 'Veeva Vault CSV file has been successfully generated' ) ) ;
228201 } ) ;
229202
230203} ;
0 commit comments