Skip to content

Commit

Permalink
feat(index): config.file, improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Jan 11, 2017
1 parent 39563c4 commit a6c32fd
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
// ------------------------------------
// #POSTCSS - LOAD CONFIG - INDEX
// # POSTCSS - LOAD CONFIG - INDEX
// ------------------------------------

'use strict'

var resolve = require('path').resolve

var config = require('cosmiconfig')
var assign = require('object-assign')

var loadOptions = require('postcss-load-options/lib/options.js')
var loadPlugins = require('postcss-load-plugins/lib/plugins.js')

/**
* Autoload Config for PostCSS
*
* @author Michael Ciniawsky (@michael-ciniawsky) <[email protected]>
* @description Autoload Config for PostCSS
* @license MIT
*
* @module postcss-load-config
* @version 1.0.0
* @version 1.1.0
*
* @requires comsiconfig
* @requires object-assign
Expand All @@ -28,47 +32,38 @@ var loadPlugins = require('postcss-load-plugins/lib/plugins.js')
* @param {String} path Config Directory
* @param {Object} options Config Options
*
* @return {Promise} config PostCSS Plugins, PostCSS Options
* @return {Promise} config PostCSS Config
*/
module.exports = function postcssrc (ctx, path, options) {
var defaults = { cwd: process.cwd(), env: process.env.NODE_ENV }
ctx = assign({ cwd: process.cwd(), env: process.env.NODE_ENV }, ctx)

path = path ? resolve(path) : process.cwd()

ctx = assign(defaults, ctx)
path = path || process.cwd()
options = options || {}
options = assign({}, options)

if (ctx.env === undefined) {
process.env.NODE_ENV = 'development'
}
if (!ctx.env) process.env.NODE_ENV = 'development'

var file

return config('postcss', options)
.load(path)
.then(function (result) {
if (!result) {
console.log(
'PostCSS Config could not be loaded. Please check your PostCSS Config.'
)
}
if (!result) throw Error('No PostCSS Config found in: ' + path)

file = result ? result.filepath : ''

return result ? result.config : {}
})
.then(function (config) {
if (typeof config === 'function') {
config = config(ctx)
} else {
config = assign(config, ctx)
}
if (typeof config === 'function') config = config(ctx)
else config = assign(config, ctx)

if (!config.plugins) {
config.plugins = []
}
if (!config.plugins) config.plugins = []

return {
plugins: loadPlugins(config),
options: loadOptions(config)
options: loadOptions(config),
file: file
}
})
.catch(function (err) {
console.log(err)
})
}

0 comments on commit a6c32fd

Please sign in to comment.