1 | //- JavaScript source code
|
2 |
|
3 | //- katamari.js ~~
|
4 | //
|
5 | // This is adapted from a previously unpublished NPM module I wrote.
|
6 | //
|
7 | // ~~ (c) SRW, 11 Dec 2012
|
8 | // ~~ last updated 04 Nov 2013
|
9 |
|
10 | (function () {
|
11 | ;
|
12 |
|
13 | // Pragmas
|
14 |
|
15 | /*jshint maxparams: 2, quotmark: single, strict: true */
|
16 |
|
17 | /*jslint indent: 4, maxlen: 80, node: true */
|
18 |
|
19 | // Declarations
|
20 |
|
21 | var avar, fs, ignore_patterns, mime_types, path, roll_up, unroll;
|
22 |
|
23 | // Definitions
|
24 |
|
25 | avar = require('quanah').avar;
|
26 |
|
27 | fs = require('fs');
|
28 |
|
29 | ignore_patterns = [
|
30 | /^[.]/
|
31 | ];
|
32 |
|
33 | mime_types = {
|
34 | '': 'text/plain',
|
35 | '.appcache': 'text/cache-manifest',
|
36 | '.css': 'text/css',
|
37 | '.html': 'text/html; charset=utf-8',
|
38 | '.ico': 'image/x-icon',
|
39 | '.jpg': 'image/jpeg',
|
40 | '.js': 'text/javascript',
|
41 | '.json': 'application/json',
|
42 | '.manifest': 'text/cache-manifest',
|
43 | '.png': 'image/png',
|
44 | '.txt': 'text/plain',
|
45 | '.webapp': 'application/x-web-app-manifest+json',
|
46 | '.xml': 'application/xml'
|
47 | };
|
48 |
|
49 | path = require('path');
|
50 |
|
51 | roll_up = function (public_html, json_file) {
|
52 | // This function needs documentation.
|
53 | var dirpath, y;
|
54 | dirpath = path.normalize(public_html);
|
55 | y = avar({val: {}});
|
56 | y.Q(function (evt) {
|
57 | // This function checks to see that the input argument is actually a
|
58 | // directory.
|
59 | fs.lstat(dirpath, function (err, stats) {
|
60 | // This function needs documentation.
|
61 | if (err !== null) {
|
62 | return evt.fail(err);
|
63 | }
|
64 | if (stats.isDirectory() === false) {
|
65 | return evt.fail('"' + dirpath + '" is not a directory.');
|
66 | }
|
67 | return evt.exit();
|
68 | });
|
69 | return;
|
70 | }).Q(function (evt) {
|
71 | // This function reads all files located in the top level of `dirpath`
|
72 | // into memory as the properties of an object.
|
73 | fs.readdir(dirpath, function (err, files) {
|
74 | // This function needs documentation.
|
75 | if (err !== null) {
|
76 | return evt.fail(err);
|
77 | }
|
78 | var count, remaining;
|
79 | count = function () {
|
80 | // This function needs documentation.
|
81 | remaining -= 1;
|
82 | if (remaining === 0) {
|
83 | return evt.exit();
|
84 | }
|
85 | return;
|
86 | };
|
87 | remaining = files.length;
|
88 | files.forEach(function (name) {
|
89 | // This function needs documentation.
|
90 | var flag, filepath, i, n;
|
91 | flag = (name === json_file);
|
92 | n = ignore_patterns.length;
|
93 | for (i = 0; (flag === false) && (i < n); i += 1) {
|
94 | flag = ignore_patterns[i].test(name);
|
95 | }
|
96 | if (flag === true) {
|
97 | return count();
|
98 | }
|
99 | filepath = path.join(dirpath, name);
|
100 | fs.lstat(filepath, function (err, stats) {
|
101 | // This function needs documentation.
|
102 | if (err !== null) {
|
103 | return evt.fail(err);
|
104 | }
|
105 | /*
|
106 | if (stats.isDirectory()) {
|
107 | // Recurse ...
|
108 | roll_up(filepath).Q(function (evt) {
|
109 | // This function isn't working correctly yet ...
|
110 | var key, temp, x;
|
111 | x = this.val;
|
112 | for (key in x) {
|
113 | if (x.hasOwnProperty(key)) {
|
114 | console.log(filepath, '-->', key);
|
115 | temp = path.resolve(filepath, key);
|
116 | y.val[temp] = x[key];
|
117 | }
|
118 | }
|
119 | count();
|
120 | return evt.exit();
|
121 | }).on('error', function (message) {
|
122 | // This function needs documentation.
|
123 | return evt.fail(message);
|
124 | });
|
125 | return;
|
126 | }
|
127 | */
|
128 | if (stats.isFile() === false) {
|
129 | return count();
|
130 | }
|
131 | fs.readFile(filepath, function (err, file) {
|
132 | // This function needs documentation.
|
133 | if (err !== null) {
|
134 | return evt.fail(err);
|
135 | }
|
136 | var temp = {
|
137 | buffer: file,
|
138 | last_modified: (new Date()).toGMTString(),
|
139 | mime_type: mime_types[path.extname(name)]
|
140 | };
|
141 | if (temp.mime_type === undefined) {
|
142 | temp.mime_type = 'application/octet-stream';
|
143 | }
|
144 | y.val['/' + name] = temp;
|
145 | return count();
|
146 | });
|
147 | return;
|
148 | });
|
149 | return;
|
150 | });
|
151 | return;
|
152 | });
|
153 | return;
|
154 | }).Q(function (evt) {
|
155 | // This function writes `y.val` as JSON to `json_file` if the second
|
156 | // input argument was a string.
|
157 | if (typeof json_file !== 'string') {
|
158 | console.log('Not a string.');
|
159 | return evt.exit();
|
160 | }
|
161 | var filepath, key, obj;
|
162 | filepath = path.normalize(json_file);
|
163 | obj = y.val;
|
164 | for (key in obj) {
|
165 | // This function needs documentation.
|
166 | if (obj.hasOwnProperty(key)) {
|
167 | try {
|
168 | obj[key].base64 = obj[key].buffer.toString('base64');
|
169 | delete obj[key].buffer;
|
170 | } catch (err) {
|
171 | console.error(err, key);
|
172 | }
|
173 | }
|
174 | }
|
175 | fs.writeFile(filepath, JSON.stringify(obj), function (err) {
|
176 | // This function needs documentation.
|
177 | if (err !== null) {
|
178 | return evt.fail(err);
|
179 | }
|
180 | console.log('Saved to "' + filepath + '".');
|
181 | return evt.exit();
|
182 | });
|
183 | return;
|
184 | }).on('error', function (message) {
|
185 | // This is a default error handler that can be overwritten.
|
186 | console.error('Error:', message);
|
187 | return;
|
188 | });
|
189 | return y;
|
190 | };
|
191 |
|
192 | unroll = function (filename) {
|
193 | // This function needs documentation.
|
194 | var key, x, y;
|
195 | x = require(filename);
|
196 | y = {};
|
197 | for (key in x) {
|
198 | if (x.hasOwnProperty(key)) {
|
199 | y[key] = {
|
200 | buffer: new Buffer(x[key].base64, 'base64'),
|
201 | last_modified: x[key].last_modified,
|
202 | last_mod_date: new Date(x[key].last_modified),
|
203 | mime_type: x[key].mime_type
|
204 | };
|
205 | }
|
206 | }
|
207 | return y;
|
208 | };
|
209 |
|
210 | // Out-of-scope definitions
|
211 |
|
212 | exports.roll_up = roll_up;
|
213 |
|
214 | exports.unroll = unroll;
|
215 |
|
216 | // That's all, folks!
|
217 |
|
218 | return;
|
219 |
|
220 | }());
|
221 |
|
222 | //- vim:set syntax=javascript:
|