total5 0.0.1 → 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.
package/bin/total5 ADDED
@@ -0,0 +1,245 @@
1
+ #! /usr/bin/env node
2
+
3
+ require('total5');
4
+
5
+ const ARGS = process.argv; // TEST: ['-', '-', 'CMD']
6
+ const DIR = process.cwd();
7
+ const DIVIDER = '----------------------------------------';
8
+
9
+ function done(err) {
10
+ err && console.error('ERROR:', err);
11
+ console.timeEnd('[Done]');
12
+ }
13
+
14
+ function load(args) {
15
+
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, ''));
20
+
21
+ if ((/^\d+$/).test(cmd)) {
22
+ arg.unshift(+cmd);
23
+ cmd = 'server';
24
+ }
25
+
26
+ if (!cmd)
27
+ cmd = 'help';
28
+
29
+ console.log('* Total.js v5: [' + cmd.capitalize() + ']');
30
+ console.time('[Done]');
31
+
32
+ var fn = FUNC[cmd];
33
+ fn && fn.apply(global, arg);
34
+ }
35
+
36
+ setTimeout(load, 10, ARGS);
37
+
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
+ };
47
+
48
+ FUNC.translate = function(filename) {
49
+
50
+ var translate = function(value) {
51
+
52
+ var index = -1;
53
+ var dictionary = {};
54
+
55
+ while (true) {
56
+
57
+ index = value.indexOf('@(', index);
58
+
59
+ if (index === -1)
60
+ break;
61
+
62
+ var counter = 0;
63
+ for (let i = index + 2; i < value.length; i++) {
64
+
65
+ var c = value[i];
66
+
67
+ if (c == '(') {
68
+ counter++;
69
+ } else if (c === ')') {
70
+
71
+ if (counter) {
72
+ counter--;
73
+ continue;
74
+ }
75
+
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;
82
+ }
83
+ }
84
+ }
85
+
86
+ return dictionary;
87
+ };
88
+
89
+ var extensions = { html: true, js: true };
90
+
91
+ U.ls(DIR, function(files) {
92
+
93
+ var dictionary = {};
94
+
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];
100
+ }
101
+
102
+ let builder = [];
103
+ for (let key in dictionary)
104
+ builder.push(key.padRight(12, ' ') + ' : ' + dictionary[key]);
105
+
106
+ F.Fs.writeFileSync(filename || 'translate.resource', builder.join('\n'), 'utf8');
107
+ done();
108
+
109
+ }, (filename, isdir) => isdir ? true : extensions[U.getExtension(filename)]);
110
+
111
+ };
112
+
113
+ FUNC.bundle = function(filename) {
114
+
115
+ if (!filename)
116
+ filename = 'app.bundle';
117
+
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;
135
+
136
+ if (filename[0] !== '/')
137
+ ignore['/' + filename] = 1;
138
+ else
139
+ ignore[filename] = 1;
140
+
141
+ ignore['/.git/'] = 1;
142
+
143
+ if (filename.toLowerCase().lastIndexOf('.bundle') === -1)
144
+ filename += '.bundle';
145
+
146
+ ignore[filename] = 1;
147
+ F.backup(filename, DIR, done, path => ignore[path] == null);
148
+ };
149
+
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
+ };
157
+
158
+ FUNC.minify = function(filename) {
159
+
160
+ var ext = U.getExtension(filename).toLowerCase();
161
+ var body = F.Fs.readFileSync(filename, 'utf8');
162
+
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
+ }
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
+ };
183
+
184
+ FUNC.server = function(port) {
185
+
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;
191
+
192
+ ROUTE('GET /*', function($) {
193
+
194
+ var dir = PATH.public($.url.substring(1));
195
+ var filename = PATH.join($.url, 'index.html').substring(1);
196
+
197
+ PATH.exists(filename, function(e) {
198
+
199
+ if (e)
200
+ return $.file(filename, '');
201
+
202
+ F.Fs.readdir(dir, function(err, items) {
203
+
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
+ };
207
+
208
+ var directories = [];
209
+ var files = [];
210
+
211
+ if ($.url !== '/')
212
+ directories.push('<a href=".." class="directory-back">..</a>');
213
+
214
+ if (err)
215
+ return render($, directories, files);
216
+
217
+ items.wait(function(item, next) {
218
+ var filename = PATH.join(dir, item);
219
+ F.Fs.stat(filename, function(err, info) {
220
+
221
+ if (err) {
222
+ next();
223
+ return;
224
+ }
225
+
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
+ });
234
+
235
+ });
236
+ });
237
+
238
+ F.http({ load: 'none', port: port || 8000 });
239
+ };
240
+
241
+ FUNC.edit = function(url) {
242
+ if (!url)
243
+ throw new Error('Invalid URL address');
244
+ require('../edit').init(url);
245
+ };