-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.js
50 lines (43 loc) · 1.05 KB
/
config.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
/*!
* Pop - Configuration module.
* Copyright 2011 Alex R. Young
* MIT Licensed
*/
/**
* Config file reader.
*
* @param {String} Config file name
* @return {Object} Parsed JavaScript object
*/
function readConfigFile(file) {
var defaults = {
perPage: 20
, port: 4000
, output: '_site/'
};
function applyDefaults(config) {
for (var key in defaults) {
config[key] = config[key] || defaults[key];
}
if (config.url) config.url = config.url.replace(/\/$/, '');
return config;
}
try {
var data = fs.readFileSync(file, 'utf8');
return applyDefaults(JSON.parse(data));
} catch(exception) {
if (exception.code === 'EBADF') {
log.error('No _config.json file in this directory. Is this a Pop site?');
process.exit(1);
} else {
log.info('Error reading config:', exception.message);
throw(exception);
}
}
}
/**
* Module dependencies and additional config variables.
*/
var fs = require(__dirname + '/graceful')
, log = require(__dirname + '/log');
module.exports = readConfigFile;