/
index.js
219 lines (187 loc) · 4.43 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var clone = require('clone');
var defaults = require('defaults');
var each = require('async').each;
var front = require('front-matter');
var fs = require('fs-extra');
var Mode = require('stat-mode');
var noop = function(){};
var path = require('path');
var readdir = require('recursive-readdir');
var rm = require('rimraf');
var utf8 = require('is-utf8');
var Ware = require('ware');
/**
* Expose `Metalsmith`.
*/
module.exports = Metalsmith;
/**
* Initialize a new `Metalsmith` builder with a working `dir`.
*
* @param {String} dir
*/
function Metalsmith(dir){
if (!(this instanceof Metalsmith)) return new Metalsmith(dir);
this.dir = path.resolve(dir);
this.ware = new Ware();
this.data = {};
this.source('src');
this.destination('build');
this.clean(true);
}
/**
* Add a `plugin` to the middleware stack.
*
* @param {Function} plugin
* @return {Metalsmith}
*/
Metalsmith.prototype.use = function(plugin){
this.ware.use(plugin);
return this;
};
/**
* Get or set the global `metadata` to pass to templates.
*
* @param {Object} metadata
* @return {Object or Metalsmith}
*/
Metalsmith.prototype.metadata = function(metadata){
if (!arguments.length) return this.data;
this.data = clone(metadata);
return this;
};
/**
* Get or set the source directory.
*
* @param {String} path
* @return {String or Metalsmith}
*/
Metalsmith.prototype.source = function(path){
if (!arguments.length) return this.join(this._src);
this._src = path;
return this;
};
/**
* Get or set the destination directory.
*
* @param {String} path
* @return {String or Metalsmith}
*/
Metalsmith.prototype.destination = function(path){
if (!arguments.length) return this.join(this._dest);
this._dest = path;
return this;
};
/**
* Get or set whether the destination directory will be removed before writing.
* @param {Boolean} clean
* @return {Boolean or Metalsmith}
*/
Metalsmith.prototype.clean = function(clean){
if (!arguments.length) return this._clean;
this._clean = clean;
return this;
};
/**
* Join path `strs` with the working directory.
*
* @param {String} strs...
* @return {String}
*/
Metalsmith.prototype.join = function(){
var strs = [].slice.call(arguments);
strs.unshift(this.dir);
return path.join.apply(path, strs);
};
/**
* Build with the current settings to the dest directory.
*
* @param {Function} fn
*/
Metalsmith.prototype.build = function(fn){
fn = fn || noop;
var self = this;
this.read(function(err, files){
if (err) return fn(err);
self.run(files, function(err, files){
if (err) return fn(err);
self.write(files, function(err){
fn(err, files);
});
});
});
};
/**
* Run a set of `files` through the middleware stack.
*
* @param {Object} files
* @param {Function} fn
*/
Metalsmith.prototype.run = function(files, fn){
this.ware.run(files, this, fn);
};
/**
* Read the source directory, parsing front matter and call `fn(files)`.
*
* @param {Function} fn
* @api private
*/
Metalsmith.prototype.read = function(fn){
var files = {};
var src = this.source();
readdir(src, function(err, arr){
if (err) return fn(err);
each(arr, read, function(err){
fn(err, files);
});
});
function read(file, done){
var name = 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 = {};
if (utf8(buffer)) {
var parsed = front(buffer.toString());
file = parsed.attributes;
file.contents = new Buffer(parsed.body);
} else {
file.contents = buffer;
}
file.mode = Mode(stats).toOctal();
files[name] = file;
done();
});
});
}
};
/**
* Write a dictionary of `files` to the dest directory.
*
* @param {Object} files
* @param {Function} fn
* @api private
*/
Metalsmith.prototype.write = function(files, fn){
var dest = this.destination();
if (this.clean()) {
rm(dest, function(err){
if (err) return fn(err);
writeAll();
});
} else {
writeAll();
}
function writeAll(){
each(Object.keys(files), write, fn);
}
function write(file, done){
var data = files[file];
var out = path.join(dest, file);
return fs.outputFile(out, data.contents, function(err){
if (err) done(err);
if (!data.mode) return done();
fs.chmod(out, data.mode, done);
});
}
};