total5 0.0.1-9 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/api.js +18 -10
  2. package/bin/flow5 +142 -0
  3. package/bin/total5 +166 -905
  4. package/builders.js +90 -35
  5. package/changelog.txt +3 -1
  6. package/cluster.js +1 -1
  7. package/cms.js +185 -51
  8. package/controller.js +233 -70
  9. package/cron.js +1 -1
  10. package/debug.js +12 -5
  11. package/edit.js +26 -33
  12. package/filestorage.js +38 -17
  13. package/flow-flowstream.js +199 -68
  14. package/flow.js +16 -10
  15. package/flowstream.js +4 -3
  16. package/global.js +84 -1
  17. package/htmlparser.js +41 -29
  18. package/http.js +3 -1
  19. package/image.js +4 -0
  20. package/images.js +1 -1
  21. package/index.js +246 -117
  22. package/jsonschema.js +21 -17
  23. package/macros.js +222 -0
  24. package/mail.js +11 -27
  25. package/markdown.js +21 -11
  26. package/minificators.js +1 -1
  27. package/nosql-querybuilder.js +4 -0
  28. package/nosql.js +8 -0
  29. package/openclient.js +1 -1
  30. package/package.json +4 -3
  31. package/pause.html +1 -1
  32. package/release.js +1 -1
  33. package/routing.js +118 -35
  34. package/sourcemap.js +6 -3
  35. package/test.js +9 -2
  36. package/tms.js +11 -14
  37. package/uibuilder.js +59 -23
  38. package/utils.js +147 -102
  39. package/viewengine.js +19 -8
  40. package/websocket.js +10 -6
  41. package/503.html +0 -65
  42. package/CONTRIBUTING.md +0 -55
  43. package/helpers/index.js +0 -32
  44. package/templates.json +0 -74
  45. package/tests/bundles/index.js +0 -25
  46. package/tests/cron.js +0 -0
  47. package/tests/htmlparser.js +0 -0
  48. package/tests/minifactors.js +0 -17
  49. package/tests/nosql.js +0 -18
  50. package/tests/proxy/index.js +0 -21
  51. package/tests/routing/index.js +0 -27
  52. package/tests/schemas.js +0 -17
  53. package/tests/server/index.js +0 -24
  54. package/tests/staticfiles/index.js +0 -24
  55. package/tests/utils.js +0 -17
  56. package/tmsclient.js +0 -125
  57. package/todo.txt +0 -11
  58. package/tools/beta.sh +0 -6
  59. package/tools/release.sh +0 -6
package/bin/total5 CHANGED
@@ -2,983 +2,244 @@
2
2
 
3
3
  require('total5');
4
4
 
5
- const Internal = require('total4/internal');
6
- var $type = 0;
7
- var isDirectory = false;
8
-
9
- function display_help() {
10
- log_title('Help');
11
-
12
- console.log('create <template> <path> : Download template from "total4 templates" list. Default path is "./<template>". Requires git.');
13
- console.log('templates : List of all Total.js templates');
14
- console.log('translate : Creates a resource file with the localized text from views');
15
- console.log('translate "TEXT" : Creates an identificator for the resource');
16
- console.log('translate <filename> : Parses and creates a resource file from the text file');
17
- console.log('translatecsv : Parses and creates CSV with localization in the current directory');
18
- console.log('csv <filename> : Parses CSV and creates resources from CSV file');
19
- console.log('diff <source> <target> : Creates differences between two resources');
20
- console.log('merge <source> <target> : Merges first resource into the second');
21
- console.log('clean <source> : Cleans a resource file "total4 clean source"');
22
- console.log('minify <filename> : Minifies .js, .css or .html file into filename.min.<extension>');
23
- console.log('bundle <filename> : Makes a bundle from the current directory');
24
- console.log('package <filename> : Makes a package from the current directory');
25
- console.log('extract <filename> : Extracts a bundle/package');
26
- console.log('help : List all total4 commands');
27
- console.log('8000 : Starts a server on port "8000"');
28
- console.log('edit <url?id=project> : Synchronizes the current directory with the Total.js Code Editor');
29
- console.log('');
30
- }
31
-
32
- function log_title(title) {
33
- var line = title + ': ';
34
-
35
- var underline = '';
36
- for (var i = 0; i < line.length - 1; i++)
37
- underline += '╍';
38
-
39
- console.log(line);
40
- console.log(underline);
41
- }
5
+ const ARGS = process.argv; // TEST: ['-', '-', 'CMD']
6
+ const DIR = process.cwd();
7
+ const DIVIDER = '----------------------------------------';
42
8
 
43
- function log_error(error) {
44
- console.log(' ⬢ [ERROR]: ' + error + ' ⬢');
45
- console.log('');
9
+ function done(err) {
10
+ err && console.error('ERROR:', err);
11
+ console.timeEnd('[Done]');
46
12
  }
47
13
 
48
- function translateFile(a) {
49
-
50
- if (!F.Fs.existsSync(a))
51
- return false;
52
-
53
- var arr = F.Fs.readFileSync(a).toString('utf8').split('\n');
54
- var builder = [];
55
- var count = 0;
56
-
57
- for (var i = 0, length = arr.length; i < length; i++) {
58
- var line = arr[i].trim();
59
- if (line.length) {
60
- builder.push('T' + HASH(line, true).toString(36).padRight(17, ' ') + ': ' + line);
61
- count++;
62
- }
63
- }
64
-
65
- F.Fs.writeFileSync('translate.resource', '// Total.js translation file\n// Created: ' + new Date().format('yyyy-MM-dd HH:mm') + '\n' + builder.join('\n'));
66
- console.log('Translation has been created successfully (' + count + ' texts)');
67
- return true;
68
- }
69
-
70
- function diff(a, b) {
71
-
72
- if (!F.Fs.existsSync(a)) {
73
- console.log('Translation file does not exist: ' + a);
74
- return;
75
- }
76
-
77
- if (!F.Fs.existsSync(b)) {
78
- console.log('Translation file does not exist: ' + b);
79
- return;
80
- }
81
-
82
- var ba = F.Fs.readFileSync(a).toString('utf8');
83
- var bb = F.Fs.readFileSync(b).toString('utf8');
84
- var ca = ba.parseConfig();
85
- var cb = bb.parseConfig();
86
- var ka = Object.keys(ca);
87
- var kb = Object.keys(cb);
88
-
89
- ba = ba.split('\n');
90
- bb = bb.split('\n');
91
-
92
- var output = '';
93
- var items = [];
94
- var add = 0;
95
- var rem = 0;
96
- var padding = 0;
97
-
98
- for (var i = 0, length = ba.length; i < length; i++) {
99
- if (ba[i].indexOf(ka[0]) !== -1) {
100
- padding = ba[i].indexOf(':');
101
- break;
102
- }
103
- }
104
-
105
- if (padding <= 0)
106
- padding = 17;
107
-
108
- function find_comment(arr, id) {
109
- var comment = '';
110
- for (var i = 0, length = arr.length; i < length; i++) {
111
- if (arr[i].indexOf(id) !== -1)
112
- return comment;
113
- var line = arr[i];
114
- if (line[0] !== '/' && line[1] !== '/')
115
- continue;
116
- comment = line;
117
- }
118
- return '';
119
- }
120
-
121
- var comment = '';
122
- var prev = '';
123
-
124
- for (var i = 0, length = ka.length; i < length; i++) {
125
- var key = ka[i];
14
+ function load(args) {
126
15
 
127
- if (cb[key] !== undefined)
128
- continue;
129
-
130
- comment = find_comment(ba, key);
131
-
132
- if (comment) {
133
- if (items[items.length - 1] !== '')
134
- items.push('');
135
- items.push(comment);
136
- }
137
-
138
- var empty = comment === prev;
139
-
140
- prev = comment;
141
- items.push(key.padRight(padding) + ': ' + ca[key]);
142
-
143
- if (!empty)
144
- items.push('');
145
-
146
- add++;
147
- }
16
+ var cmd = (args[2] || '').replace(/-{2,}/g, '');
17
+ var arg = [];
18
+ for (let i = 3; i < args.length; i++)
19
+ arg.push(args[i].trim().replace(/"/g, ''));
148
20
 
149
- if (items.length > 0) {
150
- output += '\n';
151
- output += 'Add to "' + b + '" these:\n';
152
- output += '\n';
153
- output += items.join('\n');
154
- output += '\n';
21
+ if ((/^\d+$/).test(cmd)) {
22
+ arg.unshift(+cmd);
23
+ cmd = 'server';
155
24
  }
156
25
 
157
- items = [];
26
+ if (!cmd)
27
+ cmd = 'help';
158
28
 
159
- for (var i = 0, length = kb.length; i < length; i++) {
160
- var key = kb[i];
29
+ console.log('* Total.js v5: [' + cmd.capitalize() + ']');
30
+ console.time('[Done]');
161
31
 
162
- if (ca[key] !== undefined)
163
- continue;
164
-
165
- comment = find_comment(bb, key);
166
-
167
- if (comment) {
168
- if (items[items.length - 1] !== '')
169
- items.push('');
170
- items.push(comment);
171
- }
172
- else if (prev !== '')
173
- items.push('');
174
-
175
- var empty = comment === prev;
176
-
177
- prev = comment;
178
- items.push(key.padRight(padding) + ': ' + cb[key]);
179
-
180
- if (!empty)
181
- items.push('');
182
-
183
- rem++;
184
- }
185
-
186
- if (items.length) {
187
- output += '\n';
188
- output += 'Remove from "' + b + '" these:\n';
189
- output += '\n';
190
- output += items.join('\n');
191
- output += '\n';
192
- }
193
-
194
- var filename = F.Path.join(F.Path.dirname(b), F.Path.basename(b, '.resource') + '-diff.txt');
195
- F.Fs.writeFileSync(filename, '// Total.js diff file\n// Created: ' + new Date().format('yyyy-MM-dd HH:mm') + '\n' + clean_resource(output));
196
- log_title('Translation differences:');
197
- console.log('');
198
- console.log('Add : ' + add);
199
- console.log('Rem : ' + rem);
200
- console.log('Output : ' + filename);
201
- console.log('');
32
+ var fn = FUNC[cmd];
33
+ fn && fn.apply(global, arg);
202
34
  }
203
35
 
204
- function merge(a, b) {
36
+ setTimeout(load, 10, ARGS);
205
37
 
206
- if (!F.Fs.existsSync(a)) {
207
- log_error('Translation file does not exist: ' + a);
208
- return;
209
- }
38
+ FUNC.help = function() {
39
+ console.log('translate : it makes a resource file with a localized dictionary from the current directory');
40
+ console.log('minify <filename> : it minifies .js, .css and .html files');
41
+ console.log('bundle <filename> : it makes a bundle from the current directory');
42
+ console.log('extract <filename> : it extracts a bundle into the current directory');
43
+ console.log('edit <url?id=project> : it opens remote editing of the current directory with the Total.js Code Editor');
44
+ console.log('8000 : it starts a web server on port "8000" for the current directory');
45
+ done();
46
+ };
210
47
 
211
- if (!F.Fs.existsSync(b)) {
212
- log_error('Translation file does not exist: ' + b);
213
- return;
214
- }
48
+ FUNC.translate = function(filename) {
215
49
 
216
- var ba = F.Fs.readFileSync(b).toString('utf8');
217
- var bb = F.Fs.readFileSync(a).toString('utf8');
218
- var arr = ba.split('\n');
219
- var output = [];
220
- var cb = bb.parseConfig();
221
- var upd = 0;
50
+ var translate = function(value) {
222
51
 
223
- for (var i = 0, length = arr.length; i < length; i++) {
52
+ var index = -1;
53
+ var dictionary = {};
224
54
 
225
- var line = arr[i];
226
- if (!line || line[0] === '#' || line.startsWith('//')) {
227
- output.push(line);
228
- continue;
229
- }
55
+ while (true) {
230
56
 
231
- var index = line.indexOf(' :');
232
- if (index === -1) {
233
- index = line.indexOf('\t:');
234
- if (index === -1) {
235
- output.push(line);
236
- continue;
237
- }
238
- }
57
+ index = value.indexOf('@(', index);
239
58
 
240
- var key = line.substring(0, index).trim();
241
- var val = cb[key];
242
- if (!val) {
243
- output.push(line);
244
- continue;
245
- }
246
-
247
- upd++;
248
- output.push(key.padRight(index) + ' : ' + val);
249
- }
250
-
251
- var filename = F.Path.join(F.Path.dirname(b), F.Path.basename(b, '.resource') + '-merged.txt');
252
- F.Fs.writeFileSync(filename, '// Total.js merged file\n// Created: ' + new Date().format('yyyy-MM-dd HH:mm') + '\n' + clean_resource(output.join('\n')));
253
- log_title('Merged result:');
254
- console.log('');
255
- console.log('Merged : ' + upd);
256
- console.log('Output : ' + filename);
257
- console.log('');
258
- }
259
-
260
- function clean_resource(content) {
261
- var lines = content.split('\n');
262
- var output = [];
263
- var max = 0;
264
-
265
- for (var i = 0, length = lines.length; i < length; i++) {
266
- var line = lines[i];
267
- if (!line || line[0] === '#' || line.startsWith('//'))
268
- continue;
269
-
270
- var index = line.indexOf(' :');
271
- if (index === -1) {
272
- index = line.indexOf('\t:');
273
59
  if (index === -1)
274
- continue;
275
- }
276
-
277
- max = Math.max(max, index);
278
- }
279
-
280
- for (var i = 0, length = lines.length; i < length; i++) {
281
- var line = lines[i];
282
- if (!line || line[0] === '#' || line.startsWith('//')) {
283
- output.push(line);
284
- continue;
285
- }
286
-
287
- var index = line.indexOf(' :');
288
- if (index === -1) {
289
- index = line.indexOf('\t:');
290
- if (index === -1) {
291
- output.push(line);
292
- continue;
293
- }
294
- }
295
-
296
- var key = line.substring(0, index).trim();
297
- output.push(key.padRight(max, ' ') + ' : ' + line.substring(index + 2).trim());
298
- }
299
-
300
- return output.join('\n');
301
- }
302
-
303
- function convert4(a) {
304
-
305
- if (!F.Fs.existsSync(a)) {
306
- log_error('Translation file does not exist: ' + a);
307
- return;
308
- }
309
-
310
- var body = F.Fs.readFileSync(a).toString('utf8');
311
- var output = [];
312
-
313
- var lines = body.split('\n');
314
- for (var i = 0; i < lines.length; i++) {
315
- var line = lines[i];
316
- var index = line.indexOf(' :');
317
- if (index === -1) {
318
- index = line.indexOf('\t:');
319
- if (index === -1) {
320
- output.push(line);
321
- continue;
322
- }
323
- }
324
-
325
- var key = line.substring(0, index).trim();
326
-
327
- if (key[0] !== 'T') {
328
- output.push(line);
329
- continue;
330
- }
331
-
332
- if (key[1] === '-')
333
- key = 'T' + ((+key.substring(1)) >>> 0).toString(36);
334
- else
335
- key = 'T' + (+key.substring(1)).toString(36);
336
-
337
- output.push(key.padRight(index, ' ') + ' : ' + line.substring(index + 2).trim());
338
- }
339
-
340
- var filename = a.replace('.', '-new.');
341
- F.Fs.writeFileSync(filename, clean_resource(output.join('\n')));
342
- log_title('Converted:');
343
- console.log('');
344
- console.log('Output : ' + filename);
345
- console.log('');
346
- }
347
-
348
- function parse_csv(content) {
349
-
350
- var output = {};
351
- var max = 0;
352
- var csv = content.parseCSV(';');
353
-
354
- for (var i = 1; i < csv.length; i++) {
355
- var line = csv[i];
356
- var key = line.a || '';
357
- var val = line.b || '';
358
- if (key) {
359
- max = Math.max(key.length, max);
360
- output[key] = val;
361
- }
362
- }
363
-
364
- var builder = [];
365
- max += 10;
366
-
367
- Object.keys(output).forEach(function(key) {
368
- builder.push('{0}: {1}'.format(key.padRight(max, ' '), output[key]));
369
- });
370
-
371
- return '\n' + builder.join('\n');
372
- }
373
-
374
- function main() {
375
-
376
- console.log('');
377
- console.log('╔──────────────────────────────────────────────────╗');
378
- console.log('│ ⬢ Total.js - www.totaljs.com │');
379
- console.log('│ ⬡ Version: v' + F.version_header.padRight(37) + '│');
380
- console.log('╚──────────────────────────────────────────────────╝');
381
- console.log('');
382
-
383
- var dir = process.cwd();
384
-
385
- for (var i = 2; i < process.argv.length; i++) {
386
- var arg = process.argv[i];
387
- var cmd = arg.toLowerCase();
388
-
389
- if (cmd.substring(0, 2) === '--')
390
- cmd = cmd.substring(1);
391
-
392
- if (i === 2) {
393
- var port = cmd.parseInt();
394
- if (port) {
395
-
396
- CONF.$dirtmp = F.Path.join(F.Os.tmpdir(), 'total' + dir.makeid());
397
- CONF.$dirpublic = dir;
398
- CONF.$minifyjs = false;
399
- CONF.$minifycss = false;
400
- CONF.$minifyhtml = false;
401
-
402
- F.directory = dir;
403
- F.http('debug', { port: port });
404
-
405
- ROUTE('GET /*', function() {
406
-
407
- var self = this;
408
- var dir = PATH.public(self.url.substring(1));
409
- var filename = F.Path.join(self.url, 'index.html').substring(1);
410
-
411
- PATH.exists(filename, function(e) {
412
-
413
- if (e)
414
- return self.file(filename, '');
415
-
416
- F.Fs.readdir(dir, function(err, items) {
417
-
418
- var render = function(controller, directories, files) {
419
- controller.content('<!DOCTYPE html><html><head><title>Directory listing: {0}</title><meta charset="utf-8" /><style>body{font-family:Arial;font-size:16px;padding:10px 30px 30px}a{display:block}.directory:last-child{margin-bottom:10px}.directory{padding:2px 10px;background-color:#F8F8F8;margin-bottom:2px;text-decoration:none;color:black;font-weight:bold;font-size:18px}.directory-back{text-decoration:none;font-size:50px;margin:0 0 10px 5px;color:gray}.file{color:gray;text-decoration:none;font-size:14px;padding:3px 10px;border-bottom:1px solid #F0F0F0;}.file span{float:right;font-size:12px;margin:2px 0 0 0;color:#A0A0A0}.file:hover{background-color:#F8F8F8}</style></head><body><div class="directories">{1}</div><div class="files">{2}</div></body></html>'.format(controller.url, directories.join(''), files.join('')), 'text/html');
420
- };
421
-
422
- var directories = [];
423
- var files = [];
424
-
425
- if (self.url !== '/')
426
- directories.push('<a href=".." class="directory-back">..</a>');
427
-
428
- if (err)
429
- return render(self, directories, files);
430
-
431
- items.wait(function(item, next) {
432
- var filename = F.Path.join(dir, item);
433
- F.Fs.stat(filename, function(err, info) {
434
-
435
- if (info.isFile())
436
- files.push('<a href="{1}" class="file">{0}<span>{2}</span></a>'.format(item, self.url + item, info.size.filesize()));
437
- else
438
- directories.push('<a href="{1}/" class="directory">{0}</a>'.format(item, self.url + item));
439
-
440
- next();
441
- });
442
- }, () => render(self, directories, files));
443
- });
444
-
445
- });
446
- });
447
- return;
448
- }
449
- }
450
-
451
- if (!$type && (cmd === '-v' || cmd === '-version' || cmd === 'version'))
452
- return;
453
-
454
- if (!$type && (cmd === '-e' || cmd === '-edit' || cmd === 'edit')) {
455
- F.dir(dir);
456
- console.log('Directory: ' + F.directory);
457
- require('../edit').init(process.argv[i + 1]);
458
- return;
459
- }
460
-
461
- if (!$type && (cmd === '-t' || cmd === '-translate' || cmd === 'translate')) {
462
- $type = 4;
463
- continue;
464
- }
465
-
466
- if (!$type && (cmd === '-merge' || cmd === 'merge')) {
467
- merge(process.argv[i + 1] || '', process.argv[i + 2] || '');
468
- return;
469
- }
470
-
471
- if (!$type && (cmd === '-translatecsv' || cmd === '-c' || cmd === 'translatecsv')) {
472
- $type = 6;
473
- continue;
474
- }
475
-
476
- if (!$type && (cmd === '-translate4' || cmd === '-4' || cmd === 'translate4')) {
477
- convert4(process.argv[i + 1] || '');
478
- return;
479
- }
480
-
481
- if (!$type && (cmd === '-csv' || cmd === 'csv')) {
482
- var tmp = process.argv[i + 1] || '';
483
- var tt = F.Path.join(F.Path.dirname(tmp), F.Path.basename(tmp, '.csv') + '.resource');
484
- F.Fs.writeFileSync(tt, '// Total.js resource file\n// Created: ' + new Date().format('yyyy-MM-dd HH:mm') + '\n' + parse_csv(F.Fs.readFileSync(tmp).toString('utf8')));
485
- log_title('Parsed CSV:');
486
- console.log('');
487
- console.log('Output : ' + tt);
488
- console.log('');
489
- continue;
490
- }
491
-
492
- if (cmd === '-minify' || cmd === '-compress' || cmd === '-compile' || cmd === 'minify') {
493
- $type = 5;
494
- break;
495
- }
496
-
497
- if (!$type && (cmd === '-clean' || cmd === 'clean')) {
498
- var tmp = process.argv[i + 1] || '';
499
- var tt = F.Path.join(F.Path.dirname(tmp), F.Path.basename(tmp, '.resource') + '-cleaned.txt');
500
- F.Fs.writeFileSync(tt, '// Total.js cleaned file\n// Created: ' + new Date().format('yyyy-MM-dd HH:mm') + '\n' + clean_resource(F.Fs.readFileSync(tmp).toString('utf8')));
501
- log_title('Cleaned result:');
502
- console.log('');
503
- console.log('Output : ' + tt);
504
- console.log('');
505
- return;
506
- }
507
-
508
- if (!$type && (cmd === '-extract' || cmd === 'extract')) {
509
- restorebundle(dir, process.argv[i + 1] || '');
510
- return;
511
- }
512
-
513
- if (!$type && (cmd === '-bundle' || cmd === 'bundle')) {
514
- makebundle(dir, process.argv[i + 1] || '');
515
- return;
516
- }
517
-
518
- if (!$type && (cmd === '-package' || cmd === 'package')) {
519
- makepackage(dir, process.argv[i + 1] || '');
520
- return;
521
- }
522
-
523
- if (!$type && (cmd === '-diff' || cmd === 'diff')) {
524
- diff(process.argv[i + 1] || '', process.argv[i + 2] || '');
525
- return;
526
- }
527
-
528
- if (!$type && (cmd === '-h' || cmd === 'h' || cmd === '-help' || cmd === 'help')) {
529
- display_help();
530
- return;
531
- }
532
-
533
- if (cmd === '-templates' || cmd === 'templates') {
534
- $type = 7;
535
- break;
536
- }
537
-
538
- if (cmd === 'create' || cmd === '-create' || cmd === 'template' || cmd === '-template' || cmd === 'download' || cmd === 'download') {
539
- $type = 8;
540
- break;
541
- }
542
-
543
- dir = arg;
544
- isDirectory = true;
545
- }
546
-
547
- if (!$type)
548
- $type = 1;
549
-
550
- if (dir === '.')
551
- dir = process.cwd();
552
-
553
- if ($type === 5) {
554
-
555
- dir = F.Path.join(dir, process.argv.last());
556
-
557
- if (!F.Fs.existsSync(dir)) {
558
- log_error('File not found');
559
- return;
560
- }
561
-
562
- var content = F.Fs.readFileSync(dir).toString('utf8');
563
- var extension = U.getExtension(dir);
564
- var filename = dir.replace('.' + extension, '.min.' + extension);
565
-
566
- switch (extension.toLowerCase()) {
567
- case 'html':
568
- F.Fs.writeFileSync(filename, U.minify_html(content));
569
- break;
570
- case 'js':
571
- F.Fs.writeFileSync(filename, U.minify_js(content));
572
- break;
573
- case 'css':
574
- F.Fs.writeFileSync(filename, U.minify_css(content));
575
60
  break;
576
- }
577
-
578
- console.log('Minified: ' + filename);
579
- return;
580
- }
581
-
582
- if ($type === 4) {
583
-
584
- log_title('Creating of translation');
585
-
586
- if (isDirectory) {
587
- if (translateFile(dir))
588
- return;
589
- console.log('T' + dir.hash(true).toString(36).padRight(17, ' ') + ': ' + dir);
590
- console.log('');
591
- return;
592
- }
593
-
594
- if (!F.Fs.existsSync(dir)) {
595
- log_error('Directory does not exist');
596
- return;
597
- }
598
-
599
- U.ls(dir, function(files) {
600
-
601
- var resource = {};
602
- var texts = {};
603
- var max = 0;
604
- var count = 0;
605
- var key;
606
- var file;
607
-
608
- for (var i = 0, length = files.length; i < length; i++) {
609
- var filename = files[i];
610
- var ext = U.getExtension(filename);
611
-
612
- if (filename.indexOf('sitemap') === -1 && ext !== 'html' && ext !== 'js')
613
- continue;
614
-
615
- var content = F.Fs.readFileSync(filename).toString('utf8');
616
- var command = Internal.findLocalization(content, 0);
617
- while (command !== null) {
618
-
619
- // Skip for direct reading
620
- if (command.command[0] === '#' && command.command[1] !== ' ') {
621
- command = Internal.findLocalization(content, command.end);
622
- continue;
623
- }
624
-
625
- key = 'T' + HASH(command.command, true).toString(36);
626
- file = filename.substring(dir.length + 1);
627
-
628
- texts[key] = command.command;
629
-
630
- if (resource[key]) {
631
- if (resource[key].indexOf(file) === -1)
632
- resource[key] += ', ' + file;
633
- } else
634
- resource[key] = file;
635
-
636
- count++;
637
- max = Math.max(max, key.length);
638
- command = Internal.findLocalization(content, command.end);
639
- }
640
-
641
- if (ext === 'js') {
642
- // ErrorBuilder
643
- var tmp = content.match(/\$\.invalid\('[a-z-0-9]+'\)/gi);
644
- if (tmp) {
645
- for (var j = 0; j < tmp.length; j++) {
646
- var m = (tmp[j] + '');
647
- m = m.substring(11, m.length - 2);
648
- key = m;
649
- file = filename.substring(dir.length + 1);
650
- texts[key] = m;
651
- if (resource[key]) {
652
- if (resource[key].indexOf(file) === -1)
653
- resource[key] += ', ' + file;
654
- } else
655
- resource[key] = file;
656
- count++;
657
- max = Math.max(max, key.length);
658
- }
659
- }
660
-
661
- // DBMS
662
- tmp = content.match(/\.(error|err)\('[a-z-0-9]+'/gi);
663
- if (tmp) {
664
- for (var j = 0; j < tmp.length; j++) {
665
- var m = (tmp[j] + '');
666
- m = m.substring(m.indexOf('(') + 2, m.length - 1);
667
- key = m;
668
- file = filename.substring(dir.length + 1);
669
- texts[key] = m;
670
- if (resource[key]) {
671
- if (resource[key].indexOf(file) === -1)
672
- resource[key] += ', ' + file;
673
- } else
674
- resource[key] = file;
675
- count++;
676
- max = Math.max(max, key.length);
677
- }
678
- }
679
- }
680
- }
681
-
682
- var keys = Object.keys(resource);
683
- var builder = [];
684
- var output = {};
685
-
686
- for (var i = 0, length = keys.length; i < length; i++) {
687
- if (!output[resource[keys[i]]])
688
- output[resource[keys[i]]] = [];
689
- output[resource[keys[i]]].push(keys[i].padRight(max + 5, ' ') + ': ' + texts[keys[i]]);
690
- }
691
-
692
- keys = Object.keys(output);
693
- for (var i = 0, length = keys.length; i < length; i++)
694
- builder.push('\n// ' + keys[i] + '\n' + output[keys[i]].join('\n'));
695
-
696
- F.Fs.writeFileSync('translate.resource', '// Total.js translation file\n// Created: ' + new Date().format('yyyy-MM-dd HH:mm') + '\n' + builder.join('\n'));
697
-
698
- console.log('Translation has been created successfully (' + count + ' texts)');
699
- console.log('');
700
-
701
- }, (path, dir) => dir ? (path.endsWith('/node_modules') || path.endsWith('/tmp') || path.endsWith('/.git')) ? false : true : true);
702
- return;
703
- }
704
-
705
- if ($type === 6) {
706
- log_title('Creating of translation to CSV');
707
-
708
- U.ls(dir, function(files) {
709
61
 
710
- var resource = {};
711
- var texts = {};
712
- var count = 0;
713
- var output = ['Hash;Text;Translation'];
62
+ var counter = 0;
63
+ for (let i = index + 2; i < value.length; i++) {
714
64
 
715
- for (var i = 0, length = files.length; i < length; i++) {
716
- var filename = files[i];
717
- var ext = U.getExtension(filename);
65
+ var c = value[i];
718
66
 
719
- if (ext !== 'html' && ext !== 'js')
720
- continue;
67
+ if (c == '(') {
68
+ counter++;
69
+ } else if (c === ')') {
721
70
 
722
- var content = F.Fs.readFileSync(filename).toString('utf8');
723
- var command = Internal.findLocalization(content, 0);
724
- while (command !== null) {
725
-
726
- // Skip for direct reading
727
- if (command.command[0] === '#' && command.command[1] !== ' ') {
728
- command = Internal.findLocalization(content, command.end);
71
+ if (counter) {
72
+ counter--;
729
73
  continue;
730
74
  }
731
75
 
732
- var key = 'T' + HASH(command.command, true).toString(36);
733
-
734
- texts[key] = command.command;
735
-
736
- if (!resource[key]) {
737
- output.push(key + ';"' + command.command.replace(/"/g, '""') + '";');
738
- resource[key] = true;
739
- count++;
740
- }
741
-
742
- command = Internal.findLocalization(content, command.end);
76
+ let text = value.substring(index, i + 1);
77
+ let translate = text.substring(2, text.length - 1);
78
+ let key = 'T' + translate.hash(true).toString(36);
79
+ dictionary[key] = translate;
80
+ index += text.length - 2;
81
+ break;
743
82
  }
744
83
  }
745
-
746
- F.Fs.writeFileSync('translate.csv', output.join('\n'));
747
- console.log('Translation has been created successfully (' + count + ' texts).');
748
- console.log('');
749
- }, (path, dir) => dir ? (path.endsWith('/node_modules') && path.endsWith('/tmp') && path.endsWith('/.git')) ? false : true : true);
750
- return;
751
- }
752
-
753
- if ($type === 7) {
754
- templates(function(templates) {
755
- log_title('Templates');
756
-
757
- for (var key in templates) {
758
- var template = templates[key];
759
- console.log(key.padRight(17, ' ') + ': ' + template.description);
760
- }
761
-
762
- console.log('');
763
- console.log('Create template with "total4 create <template> <path>".');
764
- console.log('Default path is "./<template>".');
765
- console.log('');
766
-
767
- });
768
- return;
769
- }
770
-
771
- if ($type === 8) {
772
-
773
- var name = process.argv[3] || 'empty';
774
- var path = process.argv[4];
775
-
776
- // Default path
777
- if (!path)
778
- path = './' + name;
779
-
780
- path = PATH.join(dir, path);
781
-
782
- if (!F.Fs.existsSync(path))
783
- F.Fs.mkdirSync(path);
784
-
785
- var files = F.Fs.readdirSync(path);
786
-
787
- if (files.length > 0) {
788
-
789
- var can = true;
790
- for (var i = 0; i < files.length; i++) {
791
- var name = files[i];
792
- if (name[0] !== '.')
793
- can = false;
794
- }
795
-
796
- if (!can) {
797
- log_error('Directory is not empty.');
798
- return;
799
- }
800
84
  }
801
85
 
802
- templates(function(res) {
803
-
804
- var template = res[name];
86
+ return dictionary;
87
+ };
805
88
 
806
- if (template)
807
- git(path, template);
808
- else
809
- log_error('Template "{0}" is invalid. Use "total4 templates" to list all avaliable templates.'.format(name));
810
- });
89
+ var extensions = { html: true, js: true };
811
90
 
812
- return;
813
- }
91
+ U.ls(DIR, function(files) {
814
92
 
815
- if (cmd)
816
- log_error('Command "{0}" is invalid. Use "total4 help" to list all commands.'.format(cmd));
817
- else {
818
- console.log('Use "total4 help" to list all commands.');
819
- console.log('');
820
- }
821
- }
93
+ var dictionary = {};
822
94
 
823
- function templates(callback) {
824
- RESTBuilder.GET('https://raw.githubusercontent.com/totaljs/framework4/master/templates.json').exec(function(err, res, out) {
825
- if (err || out.status !== 200) {
826
- log_error('Total.js templates are currently unavailable.');
827
- return;
95
+ for (let filename of files) {
96
+ let body = F.Fs.readFileSync(filename, 'utf8');
97
+ let dict = translate(body);
98
+ for (let key in dict)
99
+ dictionary[key] = dict[key];
828
100
  }
829
101
 
830
- callback(res);
831
- });
832
- }
102
+ let builder = [];
103
+ for (let key in dictionary)
104
+ builder.push(key.padRight(12, ' ') + ' : ' + dictionary[key]);
833
105
 
834
- function git(dir, template) {
106
+ F.Fs.writeFileSync(filename || 'translate.resource', builder.join('\n'), 'utf8');
107
+ done();
835
108
 
836
- var done = function() {
837
- console.log('Template "{0}" was created successfully.'.format(template.name));
838
- console.log('');
839
- };
109
+ }, (filename, isdir) => isdir ? true : extensions[U.getExtension(filename)]);
840
110
 
841
- F.Child.execFile('git', ['--version'], (error) => {
842
-
843
- if (error) {
844
- log_error('Git not found. Please, download the template from https://github.com/totaljs/{0}.git'.format(template.id));
845
- return;
846
- }
111
+ };
847
112
 
848
- U.ls(dir, function(fol, fil) {
849
-
850
- if (fol.length || fil.length) {
851
- log_error('Directory "{0}" is not empty.'.format(dir));
852
- return;
853
- }
854
-
855
- PATH.mkdir(dir);
856
- F.Child.exec('git clone https://github.com/totaljs/{0}.git {1}'.format(template.id, dir), function() {
857
- PATH.mkdir(F.Path.join(dir, '/node_modules/'));
858
- PATH.rmdir(F.Path.join(dir, '.git'), function() {
859
- PATH.unlink(F.Path.join(dir, '.gitignore'), function() {
860
- PATH.exists(F.Path.join(dir, 'package.json'), function(e) {
861
- if (e)
862
- F.Child.exec('npm install total5 --save', done);
863
- else
864
- F.Child.exec('npm install', done);
865
- });
866
- });
867
- });
868
- });
869
- });
870
- });
871
- }
872
-
873
- function makebundle(dir, filename) {
113
+ FUNC.bundle = function(filename) {
874
114
 
875
115
  if (!filename)
876
116
  filename = 'app.bundle';
877
117
 
878
- var blacklist = {};
879
- blacklist['/bundle.json'] = 1;
880
- blacklist['/debug.js'] = 1;
881
- blacklist['/debug.js.json'] = 1;
882
- blacklist['/debug.pid'] = 1;
883
- blacklist['/release.js'] = 1;
884
- blacklist['/release.js.json'] = 1;
885
- blacklist['/release.pid'] = 1;
886
- blacklist['/index.js'] = 1;
887
- blacklist['/index.js.json'] = 1;
888
- blacklist['/index.pid'] = 1;
889
- blacklist['/package.json'] = 1;
890
- blacklist['/readme.md'] = 1;
891
- blacklist['/license.txt'] = 1;
892
- blacklist['/bundles/'] = 1;
893
- blacklist['/tmp/'] = 1;
894
- blacklist['/.src/'] = 1;
118
+ let ignore = {};
119
+ ignore['/bundle.json'] = 1;
120
+ ignore['/debug.js'] = 1;
121
+ ignore['/debug.js.json'] = 1;
122
+ ignore['/debug.pid'] = 1;
123
+ ignore['/release.js'] = 1;
124
+ ignore['/release.js.json'] = 1;
125
+ ignore['/release.pid'] = 1;
126
+ ignore['/index.js'] = 1;
127
+ ignore['/index.js.json'] = 1;
128
+ ignore['/index.pid'] = 1;
129
+ ignore['/package.json'] = 1;
130
+ ignore['/readme.md'] = 1;
131
+ ignore['/license.txt'] = 1;
132
+ ignore['/bundles/'] = 1;
133
+ ignore['/tmp/'] = 1;
134
+ ignore['/.src/'] = 1;
895
135
 
896
136
  if (filename[0] !== '/')
897
- blacklist['/' + filename] = 1;
137
+ ignore['/' + filename] = 1;
898
138
  else
899
- blacklist[filename] = 1;
139
+ ignore[filename] = 1;
900
140
 
901
- blacklist['/.git/'] = 1;
141
+ ignore['/.git/'] = 1;
902
142
 
903
143
  if (filename.toLowerCase().lastIndexOf('.bundle') === -1)
904
144
  filename += '.bundle';
905
145
 
906
- blacklist[filename] = 1;
907
-
908
- console.log('--- CREATE BUNDLE --');
909
- console.log('');
910
- console.log('Directory :', dir);
911
- console.log('Filename :', filename);
146
+ ignore[filename] = 1;
147
+ F.backup(filename, DIR, done, path => ignore[path] == null);
148
+ };
912
149
 
913
- F.backup(filename, U.path(dir), function(err, path) {
150
+ FUNC.extract = function(filename) {
151
+ let stats = F.Fs.lstatSync(filename);
152
+ if (stats.isDirectory())
153
+ done();
154
+ else
155
+ F.restore(filename || 'app.bundle', DIR, done);
156
+ };
914
157
 
915
- if (err)
916
- throw err;
158
+ FUNC.minify = function(filename) {
917
159
 
918
- console.log('Success :', path.files.pluralize('# files,# file,# files,# files') + ' (' + path.size.filesize() + ')');
919
- console.log('');
160
+ var ext = U.getExtension(filename).toLowerCase();
161
+ var body = F.Fs.readFileSync(filename, 'utf8');
920
162
 
921
- }, function(path) {
922
- return blacklist[path] == null;
923
- });
924
- }
163
+ switch (ext) {
164
+ case 'js':
165
+ body = U.minify_js(body);
166
+ break;
167
+ case 'css':
168
+ body = U.minify_css(body);
169
+ break;
170
+ case 'html':
171
+ body = U.minify_html(body);
172
+ break;
173
+ }
925
174
 
175
+ var target = filename.replace('.' + ext, '.min.' + ext);
176
+ F.Fs.writeFileSync(target, body, 'utf8');
177
+ console.log(DIVIDER);
178
+ console.log('Filename :', filename);
179
+ console.log('Output :', target);
180
+ console.log(DIVIDER);
181
+ done();
182
+ };
926
183
 
927
- function restorebundle(dir, filename) {
184
+ FUNC.server = function(port) {
928
185
 
929
- F.restore(filename, U.path(dir), function(err, path) {
186
+ F.config.$dirpublic = DIR;
187
+ F.config.$sourcemap = false;
188
+ F.config.$minifyjs = false;
189
+ F.config.$minifycss = false;
190
+ F.config.$minifyhtml = false;
930
191
 
931
- if (err)
932
- throw err;
192
+ ROUTE('GET /*', function($) {
933
193
 
934
- console.log('Success :', path.files.pluralize('# files,# file,# files,# files') + ' (' + path.size.filesize() + ')');
935
- console.log('');
936
- });
937
- }
194
+ var dir = PATH.public($.url.substring(1));
195
+ var filename = PATH.join($.url, 'index.html').substring(1);
938
196
 
197
+ PATH.exists(filename, function(e) {
939
198
 
940
- function makepackage(dir, filename) {
199
+ if (e)
200
+ return $.file(filename, '');
941
201
 
942
- if (!filename)
943
- filename = 'noname.package';
944
-
945
- var blacklist = {};
946
- blacklist['/bundle.json'] = 1;
947
- blacklist['/debug.js'] = 1;
948
- blacklist['/release.js'] = 1;
949
- blacklist['/debug.pid'] = 1;
950
- blacklist['/package.json'] = 1;
951
- blacklist['/readme.md'] = 1;
952
- blacklist['/license.txt'] = 1;
953
- blacklist['/bundles/'] = 1;
954
- blacklist['/tmp/'] = 1;
202
+ F.Fs.readdir(dir, function(err, items) {
955
203
 
956
- if (filename[0] !== '/')
957
- blacklist['/' + filename] = 1;
958
- else
959
- blacklist[filename] = 1;
204
+ var render = function(controller, directories, files) {
205
+ controller.html('<!DOCTYPE html><html><head><title>Directory listing: {0}</title><meta charset="utf-8" /><style>body{font-family:Arial;font-size:16px;padding:10px 30px 30px}a{display:block}.directory:last-child{margin-bottom:10px}.directory{padding:2px 10px;background-color:#F8F8F8;margin-bottom:2px;text-decoration:none;color:black;font-weight:bold;font-size:18px}.directory-back{text-decoration:none;font-size:50px;margin:0 0 10px 5px;color:gray}.file{color:gray;text-decoration:none;font-size:14px;padding:3px 10px;border-bottom:1px solid #F0F0F0;}.file span{float:right;font-size:12px;margin:2px 0 0 0;color:#A0A0A0}.file:hover{background-color:#F8F8F8}</style></head><body><div class="directories">{1}</div><div class="files">{2}</div></body></html>'.format(controller.url, directories.join(''), files.join('')));
206
+ };
960
207
 
961
- blacklist['/.git/'] = 1;
208
+ var directories = [];
209
+ var files = [];
962
210
 
963
- if (filename.toLowerCase().lastIndexOf('.package') === -1)
964
- filename += '.package';
211
+ if ($.url !== '/')
212
+ directories.push('<a href=".." class="directory-back">..</a>');
965
213
 
966
- blacklist[filename] = 1;
214
+ if (err)
215
+ return render($, directories, files);
967
216
 
968
- console.log('--- CREATE PACKAGE --');
969
- console.log('');
970
- console.log('Directory :', dir);
971
- console.log('Filename :', filename);
217
+ items.wait(function(item, next) {
218
+ var filename = PATH.join(dir, item);
219
+ F.Fs.stat(filename, function(err, info) {
972
220
 
973
- F.backup(filename, dir, function(err, path) {
221
+ if (err) {
222
+ next();
223
+ return;
224
+ }
974
225
 
975
- if (err)
976
- throw err;
226
+ if (info.isFile())
227
+ files.push('<a href="{1}" class="file">{0}<span>{2}</span></a>'.format(item, $.url + item, info.size.filesize()));
228
+ else
229
+ directories.push('<a href="{1}/" class="directory">{0}</a>'.format(item, $.url + item));
230
+ next();
231
+ });
232
+ }, () => render($, directories, files));
233
+ });
977
234
 
978
- console.log('Success :', path.files.pluralize('# files,# file,# files,# files') + ' (' + path.size.filesize() + ')');
979
- console.log('');
235
+ });
236
+ });
980
237
 
981
- }, path => blacklist[path] == null);
982
- }
238
+ F.http({ load: 'none', port: port || 8000 });
239
+ };
983
240
 
984
- main();
241
+ FUNC.edit = function(url) {
242
+ if (!url)
243
+ throw new Error('Invalid URL address');
244
+ require('../edit').init(url);
245
+ };