Skip to content

Commit e76a78e

Browse files
committed
Merge pull request #376 from totaljs/v1.9.7
v1.9.7
2 parents f801ba0 + 6ad7253 commit e76a78e

22 files changed

+1944
-683
lines changed

bin/totaljs

Lines changed: 137 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ var isDirectory = false;
1212

1313
function display_help() {
1414
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"');
2223
console.log('-minify filename = minifies .js, .css or .html file into filename.min.[extension]');
24+
console.log('-v or -version = total.js version');
2325
console.log('/path/ = target (default current directory)');
2426
console.log('');
2527
}
@@ -170,8 +172,8 @@ function diff(a, b) {
170172
output += '\n';
171173
}
172174

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));
175177
console.log('========================================');
176178
console.log('Translation files differences:');
177179
console.log('========================================');
@@ -182,6 +184,106 @@ function diff(a, b) {
182184
console.log('');
183185
}
184186

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+
185287
function main() {
186288

187289
console.log('');
@@ -204,17 +306,35 @@ function main() {
204306

205307
if (cmd === '-t' || cmd === '-translate') {
206308
$type = 4;
207-
continue
309+
continue;
310+
}
311+
312+
if (cmd === '-merge') {
313+
merge(process.argv[i + 1] || '', process.argv[i + 2] || '');
314+
return;
208315
}
209316

210317
if (cmd === '-translate-csv' || cmd === '-translatecsv' || cmd === '-c') {
211318
$type = 6;
212-
continue
319+
continue;
213320
}
214321

215322
if (cmd === '-minify' || cmd === '-compress' || cmd === '-compile') {
216323
$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;
218338
}
219339

220340
if (cmd === '-diff') {
@@ -247,6 +367,9 @@ function main() {
247367
break;
248368
}
249369

370+
if (dir === '.')
371+
dir = process.cwd();
372+
250373
if ($type === 5) {
251374

252375
if (!fs.existsSync(dir)) {
@@ -423,12 +546,12 @@ function main() {
423546
var url = 'https://www.totaljs.com/packages/empty-project.package';
424547

425548
Utils.download(url, ['get'], function(err, response) {
426-
var filename = Utils.join(dir, 'total.package');
549+
var filename = path.join(dir, 'total.package');
427550
var stream = fs.createWriteStream(filename);
428551
response.pipe(stream);
429552
stream.on('finish', function() {
430553
console.log('Unpacking file.');
431-
exec('tpm unpack total.package', function() {
554+
exec('totalpackage unpack total.package', function() {
432555
fs.unlink(filename);
433556
console.log('Done.');
434557
console.log('');

0 commit comments

Comments
 (0)