@@ -102,7 +102,7 @@ module.exports = yeoman.Base.extend({
102
102
} ) ;
103
103
} )
104
104
. then ( ( ) => {
105
- this . _downloadTemplate ( this . answer . templateToUse , listOfUrls [ this . answer . templateToUse ] , this . answer . functionName ) ;
105
+ this . _downloadTemplate ( this . answer . templateToUse , listOfUrls [ this . answer . templateToUse ] , this . answer . functionName , "" ) ;
106
106
} )
107
107
. catch ( err => {
108
108
this . log ( 'There was an error in searching for available templates...' ) ;
@@ -125,19 +125,6 @@ module.exports = yeoman.Base.extend({
125
125
. get ( templatesUrl )
126
126
. on ( 'end' , ( ) => {
127
127
templatesJson = require ( path . resolve ( './templates.json' ) ) ;
128
-
129
- // this.log(templatesJson[0]);
130
- // output:
131
- // { id: 'BlobTrigger-Batch',
132
- // function: { disabled: false, bindings: [ [Object] ] },
133
- // metadata:
134
- // { name: 'BlobTrigger - Batch',
135
- // description: 'A Batch function that will be run whenever a blob is added to a specified container',
136
- // defaultFunctionName: 'BlobTriggerBatch',
137
- // language: 'Batch',
138
- // category: [ 'Experimental' ],
139
- // userPrompt: [ 'connection', 'path' ] },
140
- // files: { 'run.bat': 'echo OFF\nSET /p input=<%input%\necho Windows Batch script processed blob \'%input%\'' } }
141
128
142
129
var sortedTemplatesByLanguage = { } ;
143
130
@@ -152,6 +139,8 @@ module.exports = yeoman.Base.extend({
152
139
}
153
140
}
154
141
142
+ this . log ( 'There are %d languages available' , Object . keys ( sortedTemplatesByLanguage ) . length ) ;
143
+
155
144
var prompts = [ {
156
145
type : 'rawlist' ,
157
146
name : 'languageChose' ,
@@ -177,11 +166,66 @@ module.exports = yeoman.Base.extend({
177
166
//------------------------------
178
167
//------------------------------
179
168
if ( this . answer . requestFunctionTemplates == TEMPLATES_BY_EVENT_TYPE ) {
180
- this . log ( 'Feature coming soon, just wait on it!' ) ;
169
+ // templates.json url
170
+ var templatesUrl = "https://ahmelsayed.blob.core.windows.net/public/templates.json" ;
171
+ var fileName = "templates.json" ;
172
+ var templatesJson = { } ;
173
+
174
+ request
175
+ . get ( templatesUrl )
176
+ . on ( 'end' , ( ) => {
177
+ templatesJson = require ( path . resolve ( './templates.json' ) ) ;
178
+
179
+ this . log ( 'templatesJson[0]: ' + templatesJson [ 0 ] [ 'function' ] . bindings [ 0 ] . type ) ;
180
+
181
+ var sortedTemplatesByEvent = { } ;
182
+
183
+ for ( var i in templatesJson ) {
184
+ var tempID = templatesJson [ i ] . id ;
185
+ var tempEvent = "" ;
186
+
187
+ if ( templatesJson [ i ] [ 'function' ] . bindings [ 0 ] !== undefined ) {
188
+ tempEvent = templatesJson [ i ] [ 'function' ] . bindings [ 0 ] . type ;
189
+ } else {
190
+ tempEvent = "empty" ;
191
+ }
192
+
193
+ if ( sortedTemplatesByEvent . hasOwnProperty ( tempEvent ) ) {
194
+ sortedTemplatesByEvent [ tempEvent ] . push ( tempID ) ;
195
+ } else {
196
+ sortedTemplatesByEvent [ tempEvent ] = new Array ( tempID ) ;
197
+ }
198
+ }
199
+
200
+ this . log ( 'There are %d event types available' , Object . keys ( sortedTemplatesByEvent ) . length ) ;
201
+
202
+ var prompts = [ {
203
+ type : 'rawlist' ,
204
+ name : 'eventChose' ,
205
+ message : 'Select an event type...' ,
206
+ choices : Object . keys ( sortedTemplatesByEvent ) ,
207
+ default : Object . keys ( sortedTemplatesByEvent ) [ 0 ]
208
+ } ] ;
209
+
210
+ return this . prompt ( prompts ) . then ( answer => {
211
+ this . answer = answer ;
212
+ this . _showRelevantTemplates ( sortedTemplatesByEvent [ this . answer . eventChose ] ) ;
213
+ } ) ;
214
+ } )
215
+ . on ( 'error' , err => {
216
+ this . log ( 'There was an error when downloading the templates.json file' ) ;
217
+ } )
218
+ . pipe ( fs . createWriteStream ( path . resolve ( './' , fileName ) ) ) ;
181
219
}
182
220
} ,
183
221
184
222
_showRelevantTemplates : function ( templatesToShow ) {
223
+ var languageToUse = "" ;
224
+
225
+ if ( this . answer . languageChose !== undefined ) {
226
+ languageToUse = this . answer . languageChose ;
227
+ }
228
+
185
229
var options = {
186
230
uri : 'https://api.github.com/repos/Azure/azure-webjobs-sdk-templates/contents/Templates' ,
187
231
headers : {
@@ -203,6 +247,8 @@ module.exports = yeoman.Base.extend({
203
247
}
204
248
}
205
249
250
+ this . log ( 'There are %d templates available' , listOfTemplates . length ) ;
251
+
206
252
var prompts = [ {
207
253
type : 'list' ,
208
254
name : 'templateToUse' ,
@@ -221,15 +267,15 @@ module.exports = yeoman.Base.extend({
221
267
} ) ;
222
268
} )
223
269
. then ( ( ) => {
224
- this . _downloadTemplate ( this . answer . templateToUse , listOfUrls [ this . answer . templateToUse ] , this . answer . functionName ) ;
270
+ this . _downloadTemplate ( this . answer . templateToUse , listOfUrls [ this . answer . templateToUse ] , this . answer . functionName , languageToUse ) ;
225
271
} )
226
272
. catch ( err => {
227
273
this . log ( 'There was an error in searching for available templates...' ) ;
228
274
this . log ( err ) ;
229
275
} ) ; ;
230
276
} ,
231
277
232
- _downloadTemplate : function ( templateToUse , urlToUse , functionName , language = "" ) {
278
+ _downloadTemplate : function ( templateToUse , urlToUse , functionName , language ) {
233
279
var languageToUse = language ;
234
280
235
281
var options = {
@@ -245,7 +291,6 @@ module.exports = yeoman.Base.extend({
245
291
requestPromise ( options )
246
292
. then ( files => {
247
293
var pathToSaveFunction = path . resolve ( './' , functionName ) ;
248
- var languageOfTemplate = "" ;
249
294
250
295
fs . mkdir ( pathToSaveFunction , err => {
251
296
if ( err ) {
@@ -254,86 +299,80 @@ module.exports = yeoman.Base.extend({
254
299
}
255
300
}
256
301
257
- this . log ( 'Location for your function: ' ) ;
258
- this . log ( pathToSaveFunction + '\n' ) ;
302
+ this . log ( 'Location for your function... ' ) ;
303
+ this . log ( pathToSaveFunction ) ;
259
304
260
- var filesToDownload = { } ;
305
+ var filesInTemplate = { } ;
261
306
for ( let i = 0 ; i < files . length ; i ++ ) {
262
- filesToDownload [ files [ i ] . name ] = files [ i ] . download_url ;
307
+ filesInTemplate [ files [ i ] . name ] = files [ i ] . download_url ;
263
308
}
264
309
310
+ // Function to download the files
311
+ var downloadFile = function ( filesToDownload , language ) {
312
+ for ( let file in filesToDownload ) {
313
+ request
314
+ . get ( filesToDownload [ file ] )
315
+ . on ( 'error' , err => {
316
+ this . log ( 'There was an error when downloading the file ' + file ) ;
317
+ this . log ( err ) ;
318
+ } )
319
+ . pipe ( fs . createWriteStream ( path . resolve ( pathToSaveFunction , file ) ) ) ;
320
+ }
321
+ } . bind ( this ) ;
322
+
265
323
// Verify language if none is given on function call
266
- if ( language === "" ) {
267
- // check metadata.json to verify language
324
+ if ( languageToUse === "" ) {
325
+ // download metadata.json to verify language
326
+ request
327
+ . get ( filesInTemplate [ 'metadata.json' ] )
328
+ . on ( 'end' , ( ) => {
329
+ // check the language
330
+ languageToUse = languages . resolveLanguage ( path . resolve ( pathToSaveFunction , "metadata.json" ) ) ;
331
+
332
+ // gather the files
333
+ var filesToDownload = { } ;
334
+ for ( let file in filesInTemplate ) {
335
+ if ( file . indexOf ( languagesJSON [ languageToUse ] . fileExtension ) >= 0 ) {
336
+ filesToDownload [ file ] = filesInTemplate [ file ] ;
337
+ } else if ( file === "function.json" ) {
338
+ filesToDownload [ file ] = filesInTemplate [ file ] ;
339
+ }
340
+ }
341
+
342
+ // download the rest of the files
343
+ downloadFile ( filesToDownload , languageToUse ) ;
344
+ } )
345
+ . on ( 'error' , err => {
346
+ this . log ( 'There was an error when downloading metadata.json' ) ;
347
+ this . log ( err ) ;
348
+ } )
349
+ . pipe ( fs . createWriteStream ( path . resolve ( pathToSaveFunction , 'metadata.json' ) ) ) ;
268
350
} else {
269
- // download the appropriate files
270
- }
351
+ // gather the files
352
+ var filesToDownload = { } ;
353
+ for ( let file in filesInTemplate ) {
354
+ this . log ( 'file: ' + file ) ;
355
+ if ( file . indexOf ( languagesJSON [ languageToUse ] . fileExtension ) >= 0 ) {
356
+ filesToDownload [ file ] = filesInTemplate [ file ] ;
357
+ } else if ( file === "metadata.json" ) {
358
+ filesToDownload [ file ] = filesInTemplate [ file ] ;
359
+ } else if ( file === "function.json" ) {
360
+ filesToDownload [ file ] = filesInTemplate [ file ] ;
361
+ }
362
+ }
271
363
364
+ this . log ( 'filesToDownload: ' + filesToDownload ) ;
272
365
273
- // for (let i = 0; i < files.length; i++) {
274
- // var fileName = files[i]['name'];
275
- // var fileUrl = files[i]['download_url'];
276
-
277
- // if (fileName === "metadata.json") {
278
- // request
279
- // .get(fileUrl)
280
- // .on('end', () => {
281
- // // Verify the language of the template
282
- // languageOfTemplate = languages.resolveLanguage(path.resolve(pathToSaveFunction, "metadata.json"));
283
- // if (languageToUse === "") {
284
- // languageToUse = languageOfTemplate;
285
-
286
- // for (let j = 0; j < files.length; j++) {
287
- // var fileName = files[j]['name'];
288
- // var fileUrl = files[j]['download_url'];
289
-
290
- // if (fileName.indexOf(languagesJSON[languageToUse].fileExtension) >= 0) {
291
- // request
292
- // .get(fileUrl)
293
- // .on('error', err => {
294
- // this.log('There was an error when downloading the file ' + fileName);
295
- // this.log(err);
296
- // })
297
- // .pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, fileName)));
298
-
299
- // this.log('Downloading file ' + fileName + ' to:');
300
- // this.log(path.resolve(pathToSaveFunction, fileName));
301
- // }
302
-
303
- // if (fileName === "function.json") {
304
- // request
305
- // .get(fileUrl)
306
- // .on('error', err => {
307
- // this.log('There was an error when downloading the file ' + fileName);
308
- // this.log(err);
309
- // })
310
- // .pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, fileName)));
311
-
312
- // this.log('Downloading file ' + fileName + ' to:');
313
- // this.log(path.resolve(pathToSaveFunction, fileName));
314
- // }
315
- // }
316
- // }
317
- // })
318
- // .on('error', err => {
319
- // this.log('There was an error when downloading the file ' + fileName);
320
- // this.log(err);
321
- // })
322
- // .pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, fileName)));
323
- // }
324
- // }
325
-
366
+ // download the files
367
+ downloadFile ( filesToDownload , languageToUse ) ;
368
+ }
326
369
return 1 ;
327
370
} ) ;
328
- return this . _configureTemplate ( pathToSaveFunction ) ;
371
+ return 1 ;
329
372
} )
330
373
. catch ( err => {
331
374
this . log ( 'There was an error in searching for the files for the template ' + templateToUse ) ;
332
375
this . log ( err ) ;
333
376
} ) ;
334
- } ,
335
-
336
- _configureTemplate : function ( pathOfTemplate ) {
337
- return 1 ;
338
377
}
339
378
} ) ;
0 commit comments