@@ -12,14 +12,16 @@ var isDirectory = false;
12
12
13
13
function display_help ( ) {
14
14
console . log ( '' ) ;
15
- console . log ( ' = without arguments creates empty-project' ) ;
16
- console . log ( '-t or -translate = creates a resource file with the localized text from views' ) ;
17
- console . log ( '-t or -translate "TEXT" = creates an identificator for the resource' ) ;
18
- console . log ( '-t or -translate filename = parsers and creates a resource file from the text file' ) ;
19
- console . log ( '-c or -translatecsv = parsers and creates CSV with localization into the current directory' ) ;
20
- console . log ( '-d or -diff = creates a differences between two resources "-diff filename1 filename2"' ) ;
21
- console . log ( '-v or -version = total.js version' ) ;
15
+ console . log ( 'without arguments = creates empty-project' ) ;
16
+ console . log ( '-translate = creates a resource file with the localized text from views' ) ;
17
+ console . log ( '-translate "TEXT" = creates an identificator for the resource' ) ;
18
+ console . log ( '-translate filename = parsers and creates a resource file from the text file' ) ;
19
+ console . log ( '-translatecsv = parsers and creates CSV with localization into the current directory' ) ;
20
+ console . log ( '-diff source target = creates differences between two resources "-diff filename_source filename_target"' ) ;
21
+ console . log ( '-merge source target = merges first resource into the second "-merge filename_source filename_target"' ) ;
22
+ console . log ( '-clean source = cleans a resource file "-clean filename_source"' ) ;
22
23
console . log ( '-minify filename = minifies .js, .css or .html file into filename.min.[extension]' ) ;
24
+ console . log ( '-v or -version = total.js version' ) ;
23
25
console . log ( '/path/ = target (default current directory)' ) ;
24
26
console . log ( '' ) ;
25
27
}
@@ -170,8 +172,8 @@ function diff(a, b) {
170
172
output += '\n' ;
171
173
}
172
174
173
- var filename = path . join ( path . dirname ( a ) , path . basename ( a , '.resource' ) + '-diff.txt' ) ;
174
- fs . writeFileSync ( filename , '// Total.js diff file\n// Created: ' + new Date ( ) . format ( 'yyyy-MM-dd HH:mm' ) + '\n' + output ) ;
175
+ var filename = path . join ( path . dirname ( b ) , path . basename ( b , '.resource' ) + '-diff.txt' ) ;
176
+ fs . writeFileSync ( filename , '// Total.js diff file\n// Created: ' + new Date ( ) . format ( 'yyyy-MM-dd HH:mm' ) + '\n' + clean_resource ( output ) ) ;
175
177
console . log ( '========================================' ) ;
176
178
console . log ( 'Translation files differences:' ) ;
177
179
console . log ( '========================================' ) ;
@@ -182,6 +184,106 @@ function diff(a, b) {
182
184
console . log ( '' ) ;
183
185
}
184
186
187
+ function merge ( a , b ) {
188
+ if ( ! fs . existsSync ( a ) ) {
189
+ console . log ( 'The translation file does not exist: ' + a ) ;
190
+ return ;
191
+ }
192
+
193
+ if ( ! fs . existsSync ( b ) ) {
194
+ console . log ( 'The translation file does not exist: ' + b ) ;
195
+ return ;
196
+ }
197
+
198
+ var ba = fs . readFileSync ( b ) . toString ( 'utf8' ) ;
199
+ var bb = fs . readFileSync ( a ) . toString ( 'utf8' ) ;
200
+ var arr = ba . split ( '\n' ) ;
201
+ var output = [ ] ;
202
+ var cb = bb . parseConfig ( ) ;
203
+ var upd = 0 ;
204
+
205
+ for ( var i = 0 , length = arr . length ; i < length ; i ++ ) {
206
+
207
+ var line = arr [ i ] ;
208
+ if ( ! line || line [ 0 ] === '#' || line . startsWith ( '//' ) ) {
209
+ output . push ( line ) ;
210
+ continue ;
211
+ }
212
+
213
+ var index = line . indexOf ( ' :' ) ;
214
+ if ( index === - 1 ) {
215
+ index = line . indexOf ( '\t:' ) ;
216
+ if ( index === - 1 ) {
217
+ output . push ( line ) ;
218
+ continue ;
219
+ }
220
+ }
221
+
222
+ var key = line . substring ( 0 , index ) . trim ( ) ;
223
+ var val = cb [ key ] ;
224
+ if ( ! val ) {
225
+ output . push ( line ) ;
226
+ continue ;
227
+ }
228
+
229
+ upd ++ ;
230
+ output . push ( key . padRight ( index ) + ' : ' + val ) ;
231
+ }
232
+
233
+ var filename = path . join ( path . dirname ( b ) , path . basename ( b , '.resource' ) + '-merged.txt' ) ;
234
+ fs . writeFileSync ( filename , '// Total.js merged file\n// Created: ' + new Date ( ) . format ( 'yyyy-MM-dd HH:mm' ) + '\n' + clean_resource ( output . join ( '\n' ) ) ) ;
235
+ console . log ( '========================================' ) ;
236
+ console . log ( 'Merged result:' ) ;
237
+ console . log ( '========================================' ) ;
238
+ console . log ( '' ) ;
239
+ console . log ( 'merged : ' + upd ) ;
240
+ console . log ( 'output : ' + filename ) ;
241
+ console . log ( '' ) ;
242
+ }
243
+
244
+ function clean_resource ( content ) {
245
+ var lines = content . split ( '\n' ) ;
246
+ var output = [ ] ;
247
+ var max = 0 ;
248
+
249
+ for ( var i = 0 , length = lines . length ; i < length ; i ++ ) {
250
+ var line = lines [ i ] ;
251
+ if ( ! line || line [ 0 ] === '#' || line . startsWith ( '//' ) )
252
+ continue ;
253
+
254
+ var index = line . indexOf ( ' :' ) ;
255
+ if ( index === - 1 ) {
256
+ index = line . indexOf ( '\t:' ) ;
257
+ if ( index === - 1 )
258
+ continue ;
259
+ }
260
+
261
+ max = Math . max ( max , index ) ;
262
+ }
263
+
264
+ for ( var i = 0 , length = lines . length ; i < length ; i ++ ) {
265
+ var line = lines [ i ] ;
266
+ if ( ! line || line [ 0 ] === '#' || line . startsWith ( '//' ) ) {
267
+ output . push ( line ) ;
268
+ continue ;
269
+ }
270
+
271
+ var index = line . indexOf ( ' :' ) ;
272
+ if ( index === - 1 ) {
273
+ index = line . indexOf ( '\t:' ) ;
274
+ if ( index === - 1 ) {
275
+ output . push ( line ) ;
276
+ continue ;
277
+ }
278
+ }
279
+
280
+ var key = line . substring ( 0 , index ) . trim ( ) ;
281
+ output . push ( key . padRight ( max , ' ' ) + ' : ' + line . substring ( index + 2 ) . trim ( ) ) ;
282
+ }
283
+
284
+ return output . join ( '\n' ) ;
285
+ }
286
+
185
287
function main ( ) {
186
288
187
289
console . log ( '' ) ;
@@ -204,17 +306,35 @@ function main() {
204
306
205
307
if ( cmd === '-t' || cmd === '-translate' ) {
206
308
$type = 4 ;
207
- continue
309
+ continue ;
310
+ }
311
+
312
+ if ( cmd === '-merge' ) {
313
+ merge ( process . argv [ i + 1 ] || '' , process . argv [ i + 2 ] || '' ) ;
314
+ return ;
208
315
}
209
316
210
317
if ( cmd === '-translate-csv' || cmd === '-translatecsv' || cmd === '-c' ) {
211
318
$type = 6 ;
212
- continue
319
+ continue ;
213
320
}
214
321
215
322
if ( cmd === '-minify' || cmd === '-compress' || cmd === '-compile' ) {
216
323
$type = 5 ;
217
- continue
324
+ continue ;
325
+ }
326
+
327
+ if ( cmd === '-clean' ) {
328
+ var tmp = process . argv [ i + 1 ] || '' ;
329
+ var tt = path . join ( path . dirname ( tmp ) , path . basename ( tmp , '.resource' ) + '-cleaned.txt' ) ;
330
+ fs . writeFileSync ( tt , '// Total.js cleaned file\n// Created: ' + new Date ( ) . format ( 'yyyy-MM-dd HH:mm' ) + '\n' + clean_resource ( fs . readFileSync ( tmp ) . toString ( 'utf8' ) ) ) ;
331
+ console . log ( '========================================' ) ;
332
+ console . log ( 'Cleaned result:' ) ;
333
+ console . log ( '========================================' ) ;
334
+ console . log ( '' ) ;
335
+ console . log ( 'output : ' + tt ) ;
336
+ console . log ( '' ) ;
337
+ return ;
218
338
}
219
339
220
340
if ( cmd === '-diff' ) {
@@ -247,6 +367,9 @@ function main() {
247
367
break ;
248
368
}
249
369
370
+ if ( dir === '.' )
371
+ dir = process . cwd ( ) ;
372
+
250
373
if ( $type === 5 ) {
251
374
252
375
if ( ! fs . existsSync ( dir ) ) {
@@ -423,12 +546,12 @@ function main() {
423
546
var url = 'https://www.totaljs.com/packages/empty-project.package' ;
424
547
425
548
Utils . download ( url , [ 'get' ] , function ( err , response ) {
426
- var filename = Utils . join ( dir , 'total.package' ) ;
549
+ var filename = path . join ( dir , 'total.package' ) ;
427
550
var stream = fs . createWriteStream ( filename ) ;
428
551
response . pipe ( stream ) ;
429
552
stream . on ( 'finish' , function ( ) {
430
553
console . log ( 'Unpacking file.' ) ;
431
- exec ( 'tpm unpack total.package' , function ( ) {
554
+ exec ( 'totalpackage unpack total.package' , function ( ) {
432
555
fs . unlink ( filename ) ;
433
556
console . log ( 'Done.' ) ;
434
557
console . log ( '' ) ;
0 commit comments