UNPKG

2.78 kBJavaScriptView Raw
1//- JavaScript source code
2
3//- update-katamari.js ~~
4//
5// Example usage: $ node examples/update-katamari.js k1.json k2.json
6//
7// ~~ (c) SRW, 29 Sep 2013
8// ~~ last updated 12 Aug 2014
9
10(function () {
11 'use strict';
12
13 // Pragmas
14
15 /*jshint maxparams: 2, quotmark: single, strict: true */
16
17 /*jslint indent: 4, maxlen: 80, node: true */
18
19 /*properties
20 argv, base64, error, exit, hasOwnProperty, last_modified, length, log,
21 mime_type, stringify, writeFile
22 */
23
24 // Declarations
25
26 var fs, update_katamari;
27
28 // Definitions
29
30 fs = require('fs');
31
32 update_katamari = function (xname, yname) {
33 // This function "updates" an old katamari by modifying the new katamari
34 // in-place to use original timestamps where content has not changed.
35 var flag, key, tx, ty, x, y;
36 x = require(xname);
37 y = require(yname);
38 for (key in y) {
39 if (y.hasOwnProperty(key)) {
40 if (x.hasOwnProperty(key)) {
41 flag = ((x[key].base64 === y[key].base64) &&
42 (x[key].mime_type === y[key].mime_type));
43 if (flag === true) {
44 tx = new Date(x[key].last_modified);
45 ty = new Date(y[key].last_modified);
46 if (tx < ty) {
47 console.log('Using original', key, '...');
48 y[key] = x[key];
49 } else {
50 console.log('Same', key, 'already ...');
51 }
52 } else {
53 console.log('Different', key, '...');
54 }
55 } else {
56 console.log('Adding', key, '...');
57 }
58 }
59 }
60 fs.writeFile(yname, JSON.stringify(y), function (err) {
61 // This function needs documentation.
62 if (err !== null) {
63 throw err;
64 }
65 console.log('Saved to "' + yname + '".');
66 return;
67 });
68 return;
69 };
70
71 // Invocations
72
73 if (process.argv.length !== 4) {
74 console.error('Incorrect number of arguments.');
75 return process.exit(1);
76 }
77
78 if (typeof process.argv[2] !== 'string') {
79 console.error('"Second" argument should be a string.');
80 return process.exit(1);
81 }
82
83 if (typeof process.argv[3] !== 'string') {
84 console.error('"Third" argument should be a string.');
85 return process.exit(1);
86 }
87
88 update_katamari(process.argv[2], process.argv[3]);
89
90 // That's all, folks!
91
92 return;
93
94}());
95
96//- vim:set syntax=javascript: