/
index.js
163 lines (148 loc) · 4.94 KB
/
index.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*jslint node: true, nomen: true, stupid: true */
'use strict';
var Q = require('q'),
fs = require('fs.extra'),
crypto = require('crypto'),
path = require('path'),
exec = Q.nfbind(require('child_process').exec);
var staticfile = require('./dist/browserify');
/**
* Present the same Q interface for both sync/non-sync versions of fs commands.
*/
function fsCmd(cmd, sync) {
var d, args = Array.prototype.slice.call(arguments, 2);
if (sync) {
d = Q.defer();
try {
d.resolve(fs[cmd + 'Sync'].apply(fs, args));
} catch (e) {
d.reject(e);
}
return d.promise;
}
return Q.ninvoke.apply(Q, [fs, cmd].concat(args));
}
/**
* Recursively crawl a directory for all of it's files.
*/
function crawlDirectoryWithFS(parent, sync) {
return fsCmd('readdir', sync, parent).then(function (things) {
return Q.all(things.map(function (file) {
file = path.join(parent, file);
return Q.all([file, fsCmd('stat', sync, file)]);
}));
}).then(function (stats) {
return Q.all(stats.map(function (args) {
var file = args[0], stat = args[1];
if (stat.isDirectory()) {
return crawlDirectoryWithFS(file, sync);
}
return file;
}));
}).then(function (grouped) {
return [].concat.apply([], grouped);
});
}
/**
* Crawl a directory for all of it's files!
*/
function crawlDirectory(parent, sync) {
if (sync) {
return crawlDirectory(parent, sync);
}
/* Use `find` if available, because it's fast! */
return exec('find ' + parent + ' -not -type d').spread(function (stdin) {
return stdin.split('\n').filter(function (file) {
return !!file;
});
}).fail(function () {
return crawlDirectoryWithFS(parent, sync);
});
}
/**
* Rewrite a assets to have the hash in their filename.
*
* I should add options to NOT ignore dotfiles, and also an options to move
* instead of copy files.
*
* @param {Object} opts
* @param {String} opts.input Directory of static files to rewrite
* @param {String} opts.output Directory where to put rewritten files, can
* be the same as the input dir
* @param {Array<String>} [opts.files] List of files to rewrite instead of
* scanning the input directory
* @param {String} [opts.move] Renames files instead of copying them
* @param {Boolean} [opts.sync] Synchronous file modification
* @param {Function} [opts.hash] Hash functions
* @param {Function} [cb(err, hashes)] Callback function
*/
staticfile.rewrite = function (opts, cb) {
var hashes = {},
promise;
opts.sync = false; // Q wraps things with a nextTick, which is preventing
// me from doing this :/
opts.hash = opts.hash || function (data) {
return crypto.createHash('sha1').update(data).digest('hex');
};
/* Rewrite directory */
if (opts.files) {
promise = Q.fcall(function () {
return opts.files.map(function (file) {
return path.join(path.resolve(process.cwd(), opts.input), file);
});
});
} else {
promise = crawlDirectory(opts.input, opts.sync);
}
promise = promise.then(function (files) {
return Q.all(files.map(function (file) {
var dir = path.dirname(file),
rel = path.relative(opts.input, file);
return fsCmd('readFile', opts.sync, file).then(function (data) {
return opts.hash(data);
}).then(function (hash) {
hashes[rel] = hash;
/* Rename */
if (opts.move) {
return fsCmd(
'rename',
opts.sync,
file,
staticfile.affix(file, hash)
);
}
/* Copy */
return fsCmd(
'mkdirp',
opts.sync,
path.dirname(path.join(opts.output, rel))
).then(function () {
return fsCmd(
'copy',
opts.sync,
file,
path.join(opts.output, staticfile.affix(rel, hash))
).fail(function () {
/**
* Surpress error, don't overwrite. May be good to
* make this an option, though.
*/
return Q.defer().resolve();
});
});
});
}));
}).then(function () {
staticfile.add(hashes);
return hashes;
});
if (!cb) {
return promise;
}
promise.then(function () {
cb(false, hashes);
}).fail(function (e) {
cb(e);
});
};
module.exports = staticfile;