-
Notifications
You must be signed in to change notification settings - Fork 627
/
metalsmith
executable file
·113 lines (90 loc) · 2.05 KB
/
metalsmith
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
#!/usr/bin/env node
/**
* Add backwards compatibility for Node 0.10.
*/
require('gnode');
/**
* Dependencies.
*/
var chalk = require('chalk');
var exists = require('fs').existsSync;
var Metalsmith = require('..');
var resolve = require('path').resolve;
/**
* Config.
*/
var config = resolve(process.cwd(), 'metalsmith.json');
if (!exists(config)) fatal('could not find a "metalsmith.json" configuration file.');
try {
var json = require(config);
} catch (e) {
fatal('it seems like "metalsmith.json" is malformed.');
}
/**
* Metalsmith.
*/
var metalsmith = new Metalsmith(process.cwd());
if (json.source) metalsmith.source(json.source);
if (json.destination) metalsmith.destination(json.destination);
if (json.metadata) metalsmith.metadata(json.metadata);
if (json.clean != null) metalsmith.clean(json.clean);
/**
* Plugins.
*/
normalize(json.plugins).forEach(function(plugin){
for (var name in plugin) {
var opts = plugin[name];
var localPath = resolve(process.cwd(), 'node_modules', name);
var fn;
try {
fn = exists(localPath) ? require(localPath) : require(name);
} catch (e) {
fatal('failed to require plugin "' + name + '".');
}
metalsmith.use(fn(opts));
}
});
/**
* Build.
*/
metalsmith.build(function(err){
if (err) return fatal(err.message);
log('successfully built to ' + metalsmith.destination());
});
/**
* Log an error and then exit the process.
*
* @param {String} message
*/
function fatal(msg){
console.error();
console.error(chalk.red(' Metalsmith') + chalk.gray(' · ') + msg);
console.error();
process.exit(1);
}
/**
* Log a `message`.
*
* @param {String} message
*/
function log(message){
console.log();
console.log(chalk.gray(' Metalsmith · ') + message);
console.log();
}
/**
* Normalize an `obj` of plugins.
*
* @param {Array or Object} obj
* @return {Array}
*/
function normalize(obj){
if (obj instanceof Array) return obj;
var ret = [];
for (var key in obj) {
var plugin = {};
plugin[key] = obj[key];
ret.push(plugin);
}
return ret;
}