This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
71 lines (58 loc) · 1.78 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
var debug = require('debug')('metalsmith-assets');
var fs = require('fs');
var path = require('path');
var readdir = require('recursive-readdir');
var Mode = require('stat-mode');
var merge = require('merge');
var each = require('async').each;
/**
* Expose `assets`.
*/
module.exports = assets;
/**
* Default plugin options
*/
var defaults = {
source: './public',
destination: '.'
};
/**
* Metalsmith plugin to include static assets.
*
* @param {Object} options (optional)
* @property {String} source Path to copy static assets from (relative to working directory). Defaults to './public'
* @property {String} destination Path to copy static assets to (relative to destination directory). Defaults to '.'
* @return {Function}
*/
function assets(options) {
options = merge({}, defaults, options);
return function (files, metalsmith, done) {
var src = metalsmith.path(options.source);
var dest = options.destination;
debug('pulling files from '+src);
debug('and putting in '+dest);
// copied almost line for line from https://github.com/segmentio/metalsmith/blob/master/lib/index.js
readdir(src, function (err, arr) {
if (err) return done(err);
debug(arr.length+' files found.');
each(arr, read, function (err) {
debug(arr.length+' files copied.');
done(err, files);
});
});
function read(file, done) {
var name = path.join(dest, path.relative(src, file));
fs.stat(file, function (err, stats) {
if (err) return done(err);
fs.readFile(file, function (err, buffer) {
if (err) return done(err);
var file = {};
file.contents = buffer;
file.mode = Mode(stats).toOctal();
files[name] = file;
done();
});
});
}
};
}