-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 1.72 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
// ------------------------------------
// #POSTHTML - LOAD CONFIG - INDEX
// ------------------------------------
'use strict'
const assign = Object.assign
const config = require('cosmiconfig')
const loadPlugins = require('./lib/plugins.js')
const loadOptions = require('posthtml-load-options/lib/options.js')
/**
* @author Michael Ciniawsky (@michael-ciniawsky) <[email protected]>
* @description Autoload Config for PostHTML
* @license MIT
*
* @module posthtml-load-config
* @version 1.0.0
*
* @requires comsiconfig
* @requires posthtml-load-options
* @requires posthtml-load-plugins
*
* @method posthtmlrc
*
* @param {Object} ctx Context
* @param {String} path Config Directory
* @param {Object} options Config Options
*
* @return {Promise} config PostHTML Plugins, PostHTML Options
*/
module.exports = function posthtmlrc (ctx, path, options) {
const defaults = { cwd: process.cwd(), env: process.env.NODE_ENV }
ctx = assign(defaults, ctx)
path = path || ctx.cwd
options = assign({}, options)
if (ctx.env === undefined) {
process.env.NODE_ENV = 'development'
}
return config('posthtml', options)
.load(path)
.then((result) => {
if (!result) {
throw new Error('PostHTML Config could not be loaded. Please check your PostHTML Config.');
}
return result ? result.config : {}
})
.then((config) => {
if (typeof config === 'function') {
config = config(ctx)
} else {
config = assign(config, ctx)
}
if (!config.plugins) {
config.plugins = []
}
return {
plugins: loadPlugins(config),
options: loadOptions(config)
}
})
.catch((err) => {
console.log(err)
})
}