-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect
executable file
·139 lines (119 loc) · 4.19 KB
/
collect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env node
var _ = require('underscore');
var fs = require('fs');
var crypto = require('crypto');
var serialize = require('node-serialize');
var config = require('config');
var Config = config.Config;
var __ = config.__;
var stringOrFunctionToString = function (what) {
if (!what) {
return '';
} else if (typeof what == 'string') {
return what;
} else if (typeof what == 'function') {
return what.call(this, Config);
} else throw "Error";
}
lib = {
readFile: function (file, callback) {
fs.readFile( file, { encoding: 'utf8' }, function ( err, data ) {
if (err) return callback(err);
callback(null, data);
});
},
readFileSync: function (file) {
return fs.readFileSync(file, { encoding: 'utf8' })
},
writeFile: function (file, data) {
fs.writeFile( file, data, function (err) {
if ( err ) throw err;
});
},
overwriteFile: function (file, data) {
fs.writeFileSync( file, data, { encoding: 'utf8', flag: 'w+' } );
},
indentBlock: function (block, indent) {
if ( indent ) {
block = new String( block );
block = block.replace( /^/gm, __.repeat( Config.indent_string, indent ) );
block = block.toString();
}
return block
}
}
_.extend( __, lib );
var getStorables = function () {
if ( ! fs.existsSync( Config.storables) ) {
file = fs.openSync( Config.storables, 'w' );
fs.closeSync(file);
}
var content = __.readFileSync( Config.storables );
content = ( content.length > 0 ) ? content : '{}';
return serialize.unserialize( content );
}
var saveStorables = function (data) {
__.overwriteFile( Config.storables, serialize.serialize( data ) );
Config.redis.rpush( 'node_utils:log', '68 Storables saved' );
}
var collectKey = function (obj, kind, storables_hash) {
var strSum = '';
var template = Config.pages[kind];
var checkSum = function () {
_.each( _.keys(obj), function ( file ) {
_.each( _.keys( obj[file] ), function ( page ) {
strSum += stringOrFunctionToString( obj[file][page][kind] );
});
});
return crypto.createHash('md5').update( strSum ).digest('hex');
}
var updateFile = function () {
var fileTarget = template.header ? template.header : '';
_.each( _.keys(obj), function ( file ) {
_.each( _.keys(obj[file]), function ( page ) {
var block = stringOrFunctionToString( obj[file][page][kind] );
if ( block ) {
block = __.indentBlock( block, template.indent );
var target = template.format.call( this, page, block );
fileTarget += target;
}
});
});
__.overwriteFile( template.target_file, fileTarget );
console.log( 'Updated ' + template.target_file);
Config.redis.rpush( 'node_utils:log', ' 97 fileTarget:' + template.target_file );
};
var hash = checkSum();
if ( fs.existsSync(template.target_file) ) {
if ( storables_hash !== hash )
updateFile();
} else
updateFile();
Config.redis.rpush( 'node_utils:log', '106 kind:' + kind );
return hash;
}
var collectPages = function (pages_in_file) {
var storables = getStorables();
_.each( Config.templates, function ( type ) {
storables[type + '_hash'] = collectKey( pages_in_file, type, storables[type + '_hash'] );
Config.redis.rpush( 'node_utils:log', '117 type:' + type );
});
saveStorables( storables );
Config.redis.rpush( 'node_utils:log', '118 done' );
};
(function () {
rc = Config.redis;
rc.get('collect:run', function (err, reply) {
if(reply == 'true') {
Config.quit();
process.exit(code=0);
}
});
rc.set('collect:run', 'true');
rc.expire('collect:run', 1);
require('coffee-script/register');
var pages_in_file = {}
_.each( Config.sets, function (set) { pages_in_file[set] = require( Config.source_dir + set )[set]; });
collectPages( pages_in_file );
Config.quit();
}.call(this));