16
16
17
17
var when = require ( "when" ) ;
18
18
var fs = require ( 'fs' ) ;
19
+ var path = require ( 'path' ) ;
19
20
var child_process = require ( 'child_process' ) ;
20
21
var request = require ( 'request' ) ;
21
22
var mustache = require ( 'mustache' ) ;
22
23
var jsStringEscape = require ( 'js-string-escape' ) ;
23
24
var obfuscator = require ( 'javascript-obfuscator' ) ;
24
25
var csv = require ( 'csv-string' ) ;
25
- var path = require ( 'path' ) ;
26
26
var CodeGen = require ( 'swagger-js-codegen' ) . CodeGen ;
27
+ var jimp = require ( "jimp" ) ;
27
28
28
29
function createCommonFiles ( templateDirectory , data ) {
30
+ "use strict" ;
29
31
// Make directories
30
32
try {
31
33
fs . mkdirSync ( path . join ( data . dst , data . module ) ) ;
@@ -34,14 +36,38 @@ function createCommonFiles(templateDirectory, data) {
34
36
console . error ( error ) ;
35
37
}
36
38
}
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
+ }
42
48
}
43
49
}
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 {
45
71
var icons = fs . readdirSync ( path . join ( templateDirectory , 'icons' ) ) ;
46
72
icons . forEach ( function ( icon ) {
47
73
try {
@@ -51,11 +77,8 @@ function createCommonFiles(templateDirectory, data) {
51
77
console . error ( error ) ;
52
78
}
53
79
} ) ;
54
- } catch ( error ) {
55
- if ( error . code !== 'ENOENT' ) {
56
- console . error ( error ) ;
57
- }
58
80
}
81
+
59
82
try {
60
83
fs . mkdirSync ( path . join ( data . dst , data . module , 'locales' ) ) ;
61
84
} catch ( error ) {
@@ -89,6 +112,7 @@ function createCommonFiles(templateDirectory, data) {
89
112
}
90
113
91
114
function runNpmPack ( data ) {
115
+ "use strict" ;
92
116
var npmCommand = process . platform === 'win32' ? 'npm.cmd' : 'npm' ;
93
117
try {
94
118
child_process . execFileSync ( npmCommand , [ 'pack' , './' + data . module ] , { cwd : data . dst } ) ;
@@ -98,6 +122,7 @@ function runNpmPack(data) {
98
122
}
99
123
100
124
function extractKeywords ( keywordsStr ) {
125
+ "use strict" ;
101
126
var keywords = [ "node-red-nodegen" ] ;
102
127
keywords = keywordsStr ? keywords . concat ( csv . parse ( keywordsStr ) [ 0 ] ) : keywords ;
103
128
keywords = keywords . map ( k => ( { name : k } ) ) ;
@@ -106,6 +131,7 @@ function extractKeywords(keywordsStr) {
106
131
}
107
132
108
133
function function2node ( data , options ) {
134
+ "use strict" ;
109
135
return when . promise ( function ( resolve , reject ) {
110
136
// Read meta data in js file
111
137
var meta = { } ;
@@ -142,6 +168,25 @@ function function2node(data, options) {
142
168
data . version = '0.0.1' ;
143
169
}
144
170
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
+
145
190
if ( data . name === 'function' ) {
146
191
reject ( '\'function\' is duplicated node name. Use another name.' ) ;
147
192
return ;
@@ -155,10 +200,18 @@ function function2node(data, options) {
155
200
projectVersion : data . version ,
156
201
keywords : extractKeywords ( data . keywords ) ,
157
202
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' ,
158
211
func : jsStringEscape ( data . src ) ,
159
212
outputs : meta . outputs
160
213
} ;
161
-
214
+
162
215
createCommonFiles ( __dirname + '/../templates/function' , data ) ;
163
216
164
217
// Create package.json
@@ -205,6 +258,7 @@ function function2node(data, options) {
205
258
}
206
259
207
260
function swagger2node ( data , options ) {
261
+ "use strict" ;
208
262
return when . promise ( function ( resolve , reject ) {
209
263
// Modify swagger data
210
264
var swagger = JSON . parse ( JSON . stringify ( data . src ) , function ( key , value ) {
@@ -272,6 +326,25 @@ function swagger2node(data, options) {
272
326
}
273
327
}
274
328
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
+
275
348
createCommonFiles ( path . join ( __dirname , '../templates/swagger' ) , data ) ;
276
349
277
350
// Create Node.js SDK
@@ -318,18 +391,18 @@ function swagger2node(data, options) {
318
391
var isNotBodyParam = function ( ) {
319
392
return function ( content , render ) {
320
393
return render ( '{{camelCaseName}}' ) !== 'body' ? render ( content ) : '' ;
321
- }
394
+ } ;
322
395
} ;
323
396
var isBodyParam = function ( ) {
324
397
return function ( content , render ) {
325
398
return render ( '{{camelCaseName}}' ) === 'body' ? render ( content ) : '' ;
326
- }
399
+ } ;
327
400
} ;
328
401
var hasOptionalParams = function ( ) {
329
402
return function ( content , render ) {
330
403
var params = render ( '{{#parameters}}{{^required}}{{camelCaseName}},{{/required}}{{/parameters}}' ) ;
331
404
return params . split ( ',' ) . filter ( p => p ) . some ( p => p !== 'body' ) ? render ( content ) : '' ;
332
- }
405
+ } ;
333
406
} ;
334
407
var hasServiceParams = swagger . host === undefined || swagger . security !== undefined ;
335
408
@@ -368,6 +441,14 @@ function swagger2node(data, options) {
368
441
mustache : {
369
442
nodeName : data . name ,
370
443
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' ,
371
452
isNotBodyParam : isNotBodyParam ,
372
453
hasOptionalParams : hasOptionalParams ,
373
454
hasServiceParams : hasServiceParams
0 commit comments