1616
1717var  when  =  require ( "when" ) ; 
1818var  fs  =  require ( 'fs' ) ; 
19+ var  path  =  require ( 'path' ) ; 
1920var  child_process  =  require ( 'child_process' ) ; 
2021var  request  =  require ( 'request' ) ; 
2122var  mustache  =  require ( 'mustache' ) ; 
2223var  jsStringEscape  =  require ( 'js-string-escape' ) ; 
2324var  obfuscator  =  require ( 'javascript-obfuscator' ) ; 
2425var  csv  =  require ( 'csv-string' ) ; 
25- var  path  =  require ( 'path' ) ; 
2626var  CodeGen  =  require ( 'swagger-js-codegen' ) . CodeGen ; 
27+ var  jimp  =  require ( "jimp" ) ; 
2728
2829function  createCommonFiles ( templateDirectory ,  data )  { 
30+     "use strict" ; 
2931    // Make directories 
3032    try  { 
3133        fs . mkdirSync ( path . join ( data . dst ,  data . module ) ) ; 
@@ -34,14 +36,38 @@ function createCommonFiles(templateDirectory, data) {
3436            console . error ( error ) ; 
3537        } 
3638    } 
37-     try  { 
38-         fs . mkdirSync ( path . join ( data . dst ,  data . module ,  'icons' ) ) ; 
39-     }  catch  ( error )  { 
40-         if  ( error . code  !==  'EEXIST' )  { 
41-             console . error ( error ) ; 
39+ 
40+     var  isStockIcon  =  data . icon  &&  data . icon . match ( / ^ ( a l e r t | a r d u i n o | a r r o w - i n | b a t c h | b l u e t o o t h | b r i d g e - d a s h | b r i d g e | c o g | c o m m e n t | d b | d e b u g | e n v e l o p e | f e e d | f i l e - i n | f i l e - o u t | f i l e | f u n c t i o n | h a s h | i n j e c t | j o i n | l e v e l d b | l i g h t | l i n k - o u t | m o n g o d b | m o u s e | n o d e - c h a n g e d | n o d e - e r r o r | p a r s e r - c s v | p a r s e r - h t m l | p a r s e r - j s o n | p a r s e r - x m l | p a r s e r - y a m l | r a n g e | r e d i s | r p i | s e r i a l | s o r t | s p l i t | s u b f l o w | s w a p | s w i t c h | t e m p l a t e | t i m e r | t r i g g e r | t w i t t e r | w a t c h | w h i t e - g l o b e ) \. p n g $ / ) ; 
41+     if  ( ! isStockIcon )  { 
42+         try  { 
43+             fs . mkdirSync ( path . join ( data . dst ,  data . module ,  'icons' ) ) ; 
44+         }  catch  ( error )  { 
45+             if  ( error . code  !==  'EEXIST' )  { 
46+                 console . error ( error ) ; 
47+             } 
4248        } 
4349    } 
44-     try  { 
50+     if  ( data . icon )  { 
51+         if  ( ! isStockIcon )  { 
52+             try  { 
53+                 jimp . read ( data . icon ,  function  ( error2 ,  image )  { 
54+                     if  ( ! error2 )  { 
55+                         var  outputPath  =  path . join ( data . dst ,  data . module ,  'icons' ,  path . basename ( data . icon ) ) ; 
56+                         if  ( image . bitmap . width  ===  20  &&  image . bitmap . height  ===  30 )  { 
57+                             var  buf  =  fs . readFileSync ( data . icon ) ; 
58+                             fs . writeFileSync ( outputPath ,  buf ) ; 
59+                         }  else  { 
60+                             image . background ( 0xFFFFFFFF ) . resize ( 20 ,  30 ) . write ( outputPath ) ; 
61+                         } 
62+                     }  else  { 
63+                         console . log ( 'error occurs while converting icon file.' ) ; 
64+                     } 
65+                 } ) ; 
66+             }  catch  ( error )  { 
67+                 console . error ( error ) ; 
68+             } 
69+         } 
70+     }  else  { 
4571        var  icons  =  fs . readdirSync ( path . join ( templateDirectory ,  'icons' ) ) ; 
4672        icons . forEach ( function  ( icon )  { 
4773            try  { 
@@ -51,11 +77,8 @@ function createCommonFiles(templateDirectory, data) {
5177                console . error ( error ) ; 
5278            } 
5379        } ) ; 
54-     }  catch  ( error )  { 
55-         if  ( error . code  !==  'ENOENT' )  { 
56-             console . error ( error ) ; 
57-         } 
5880    } 
81+ 
5982    try  { 
6083        fs . mkdirSync ( path . join ( data . dst ,  data . module ,  'locales' ) ) ; 
6184    }  catch  ( error )  { 
@@ -89,6 +112,7 @@ function createCommonFiles(templateDirectory, data) {
89112} 
90113
91114function  runNpmPack ( data )  { 
115+     "use strict" ; 
92116    var  npmCommand  =  process . platform  ===  'win32'  ? 'npm.cmd'  : 'npm' ; 
93117    try  { 
94118        child_process . execFileSync ( npmCommand ,  [ 'pack' ,  './'  +  data . module ] ,  {  cwd : data . dst  } ) ; 
@@ -98,6 +122,7 @@ function runNpmPack(data) {
98122} 
99123
100124function  extractKeywords ( keywordsStr )  { 
125+     "use strict" ; 
101126    var  keywords  =  [ "node-red-nodegen" ] ; 
102127    keywords  =  keywordsStr  ? keywords . concat ( csv . parse ( keywordsStr ) [ 0 ] )  : keywords ; 
103128    keywords  =  keywords . map ( k  =>  ( {  name : k  } ) ) ; 
@@ -106,6 +131,7 @@ function extractKeywords(keywordsStr) {
106131} 
107132
108133function  function2node ( data ,  options )  { 
134+     "use strict" ; 
109135    return  when . promise ( function  ( resolve ,  reject )  { 
110136        // Read meta data in js file 
111137        var  meta  =  { } ; 
@@ -142,6 +168,25 @@ function function2node(data, options) {
142168            data . version  =  '0.0.1' ; 
143169        } 
144170
171+         if  ( data . icon )  { 
172+             if  ( ! data . icon . match ( / \. ( p n g | g i f ) $ / ) )  { 
173+                 data . icon  =  data . icon  +  '.png' ; 
174+             } 
175+             if  ( ! data . icon . match ( / ^ [ a - z A - Z 0 - 9 \- \. / ] + $ / ) )  { 
176+                 reject ( 'invalid icon file name' ) ; 
177+                 return ; 
178+             } 
179+         } 
180+ 
181+         if  ( data . color )  { 
182+             if  ( data . color . match ( / ^ [ a - z A - Z 0 - 9 ] { 6 } $ / ) )  { 
183+                 data . color  =  '#'  +  data . color ; 
184+             }  else  { 
185+                 reject ( 'invalid color' ) ; 
186+                 return ; 
187+             } 
188+         } 
189+ 
145190        if  ( data . name  ===  'function' )  { 
146191            reject ( '\'function\' is duplicated node name. Use another name.' ) ; 
147192            return ; 
@@ -155,10 +200,18 @@ function function2node(data, options) {
155200                projectVersion : data . version , 
156201                keywords : extractKeywords ( data . keywords ) , 
157202                category : data . category  ||  'function' , 
203+                 icon : function  ( )  { 
204+                     if  ( data . icon )  { 
205+                         return  path . basename ( data . icon ) ; 
206+                     }  else  { 
207+                         return  'icon.png' ; 
208+                     } 
209+                 } , 
210+                 color : data . color  ||  '#C0DEED' , 
158211                func : jsStringEscape ( data . src ) , 
159212                outputs : meta . outputs 
160213            } ; 
161-      
214+ 
162215            createCommonFiles ( __dirname  +  '/../templates/function' ,  data ) ; 
163216
164217            // Create package.json 
@@ -205,6 +258,7 @@ function function2node(data, options) {
205258} 
206259
207260function  swagger2node ( data ,  options )  { 
261+     "use strict" ; 
208262    return  when . promise ( function  ( resolve ,  reject )  { 
209263        // Modify swagger data 
210264        var  swagger  =  JSON . parse ( JSON . stringify ( data . src ) ,  function  ( key ,  value )  { 
@@ -272,6 +326,25 @@ function swagger2node(data, options) {
272326            } 
273327        } 
274328
329+         if  ( data . icon )  { 
330+             if  ( ! data . icon . match ( / \. ( p n g | g i f ) $ / ) )  { 
331+                 data . icon  =  data . icon  +  '.png' ; 
332+             } 
333+             if  ( ! data . icon . match ( / ^ [ a - z A - Z 0 - 9 \- \. / ] + $ / ) )  { 
334+                 reject ( 'invalid icon file name' ) ; 
335+                 return ; 
336+             } 
337+         } 
338+ 
339+         if  ( data . color )  { 
340+             if  ( data . color . match ( / ^ [ a - z A - Z 0 - 9 ] { 6 } $ / ) )  { 
341+                 data . color  =  '#'  +  data . color ; 
342+             }  else  { 
343+                 reject ( 'invalid color' ) ; 
344+                 return ; 
345+             } 
346+         } 
347+ 
275348        createCommonFiles ( path . join ( __dirname ,  '../templates/swagger' ) ,  data ) ; 
276349
277350        // Create Node.js SDK 
@@ -318,18 +391,18 @@ function swagger2node(data, options) {
318391        var  isNotBodyParam  =  function  ( )  { 
319392            return  function  ( content ,  render )  { 
320393                return  render ( '{{camelCaseName}}' )  !==  'body'  ? render ( content )  : '' ; 
321-             } 
394+             } ; 
322395        } ; 
323396        var  isBodyParam  =  function  ( )  { 
324397            return  function  ( content ,  render )  { 
325398                return  render ( '{{camelCaseName}}' )  ===  'body'  ? render ( content )  : '' ; 
326-             } 
399+             } ; 
327400        } ; 
328401        var  hasOptionalParams  =  function  ( )  { 
329402            return  function  ( content ,  render )  { 
330403                var  params  =  render ( '{{#parameters}}{{^required}}{{camelCaseName}},{{/required}}{{/parameters}}' ) ; 
331404                return  params . split ( ',' ) . filter ( p  =>  p ) . some ( p  =>  p  !==  'body' )  ? render ( content )  : '' ; 
332-             } 
405+             } ; 
333406        } ; 
334407        var  hasServiceParams  =  swagger . host  ===  undefined  ||  swagger . security  !==  undefined ; 
335408
@@ -368,6 +441,14 @@ function swagger2node(data, options) {
368441            mustache : { 
369442                nodeName : data . name , 
370443                category : data . category  ||  'function' , 
444+                 icon : function  ( )  { 
445+                     if  ( data . icon )  { 
446+                         return  path . basename ( data . icon ) ; 
447+                     }  else  { 
448+                         return  'icon.png' ; 
449+                     } 
450+                 } , 
451+                 color : data . color  ||  '#89bf04' , 
371452                isNotBodyParam : isNotBodyParam , 
372453                hasOptionalParams : hasOptionalParams , 
373454                hasServiceParams : hasServiceParams  
0 commit comments