makestatic-core
Version:
Generic file processing library
83 lines (67 loc) • 1.6 kB
JavaScript
const merge = require('safe-merge')
const mm = require('micromatch')
const defaults = require('./options')
class Config {
constructor (opts = {}) {
this._raw = merge(defaults, opts)
}
get context () {
return this.options.root
}
/**
* Raw user supplied webpack configuration merged
* with the defaults.
*/
get raw () {
return this._raw
}
/**
* Custom options that may be used to control processing, typically
* supplied on the command line.
*/
get argv () {
return this._argv
}
/**
* Combined computed options.
*/
get options () {
return this._options
}
/**
* Set the computed options.
*/
setComputedOptions (argv = {}) {
// merge input options
let opts = merge(this._raw, argv)
// resolve paths using standard root
if (opts.input && !opts.root) {
opts.root = opts.input
}
this._argv = argv
this._options = opts
let k, v
// pre-compile patterns
if (this._options.matchers) {
for (k in this._options.matchers) {
v = this._options.matchers[k]
if (typeof v === 'string') {
this._options.matchers[k] = mm.makeRe(v)
}
}
}
if (Array.isArray(this._options.ignore)) {
this._options.ignore = this._options.ignore.map((v) => {
if (typeof v === 'string') {
v = mm.makeRe(v)
}
if (!(v instanceof RegExp)) {
throw new Error(`ignore pattern should be a regexp or string glob`)
}
return v
})
}
return opts
}
}
module.exports = Config