makestatic-core
Version:
Generic file processing library
313 lines (277 loc) • 6.34 kB
JavaScript
const FileList = require('./files')
// NOTE: we want this to be the ENV when the process runs
// NOTE: as it may be changed later by the cli or configuration
const ENV = process.env.NODE_ENV
const LEVELS = [
'debug',
'info',
'warn',
'error'
]
const PREFIX = {
'debug': {name: 'DEBUG'},
'warn': {name: ' WARN'},
'info': {name: ' INFO'},
'time': {name: ' TIME'},
'error': {name: 'ERROR'}
}
const DEFAULT = 'info'
class Log {
constructor (level) {
LEVELS.forEach((lvl) => {
this[lvl] = (message, ...params) => {
return this.log(lvl, message, ...params)
}
})
this.level = level || DEFAULT
}
get level () {
return this._level
}
set level (val) {
if (val && !~LEVELS.indexOf(val)) {
val = DEFAULT
}
this._level = val
}
log (level, message, ...params) {
const index = LEVELS.indexOf(this.level)
if (LEVELS.indexOf(level) < index) {
return false
}
let method = console[level]
if (!method) {
method = console.info
}
/* istanbul ignore next: suppress output in tests */
if (ENV !== 'test') {
let prefix = PREFIX[level].name + ' | '
method(prefix + message, ...params)
}
}
time (label) {
label = PREFIX.time.name + ' | ' + label
console.time(label)
return () => {
/* istanbul ignore next: suppress output in tests */
if (ENV !== 'test') {
console.timeEnd(label)
}
}
}
}
/**
* Encapsulates the processing context information.
*
* @class Context
*/
class Context {
/**
* Configure the information encapsulated by this context.
*
* @constructor Context
* @param {Object} lifecycle reference to the main lifecycle.
* @param {Object} config reference to the configuration.
*/
constructor (lifecycle, config) {
this._lifecycle = lifecycle
this._config = config
this._log = new Log(config.options.level)
this._list = new FileList(this, this.options.files)
this._data = {}
}
/**
* Name of the currently executing phase.
*
* @property {String} phase
* @member Context
* @readonly
*/
get phase () {
return this.lifecycle.phase
}
/**
* List of phases that have already executed.
*
* @property {Array} phases
* @member Context
* @readonly
*/
get phases () {
return this.lifecycle.phases
}
/**
* Alias for `FileList.getFile`.
*
* @function getFile
* @member Context
*
* @param {String|Object} opts file creation options.
*
* @returns a File instance.
*/
getFile (opts) {
return this._list.getFile(opts)
}
/**
* Get the main lifecycle runner.
*
* @member Context
* @property {Object} lifecycle reference to the main lifecycle.
* @readonly
*/
get lifecycle () {
return this._lifecycle
}
/**
* Get the log helper.
*
* @member Context
* @property {Object} log utility for logging messages.
* @readonly
*/
get log () {
return this._log
}
/**
* Get the configuration.
*
* @member Context
* @property {Object} config the application configuration.
* @readonly
*/
get config () {
return this._config
}
/**
* Get the computed options encapsulated by the `config`.
*
* @member Context
* @property {Object} options the computed options.
* @readonly
*/
get options () {
return this._config.options
}
/**
* Get the file list manager.
*
* @member Context
* @property {Object} list the file list manager.
* @readonly
*/
get list () {
return this._list
}
/**
* Get the output file list.
*
* @member Context
* @property {Array} files list of output files.
* @readonly
*/
get files () {
return this._list.files
}
/**
* Map of output assets.
*
* Compatible with the webpack asset map.
*
* @member Context
* @property {Object} assets map of output file assets.
*/
get assets () {
return this._list.assets
}
set assets (val) {
this._list.assets = val
}
/**
* HTTP client agent.
*
* Many plugins need to fetch resources from the network so it makes sense
* for them to use a common API for loading network resources and allowing
* resources to be cached which makes consecutive builds much faster.
*
* Propagated when the `http-cache` plugin is used. The `core-standard`
* package will load the `http-cache` plugin automatically.
*
* @member Context
* @property {Object} agent HTTP client implementation.
*/
get agent () {
return this._agent
}
set agent (val) {
this._agent = val
}
/**
* Application resource graph.
*
* Propagated when the `graph-resources` plugin is used.
*
* @member Context
* @property {Object} graph map of application files and resources.
*/
get graph () {
return this._graph
}
set graph (val) {
this._graph = val
}
/**
* Application manifest.
*
* Propagated when the `manifest` plugin is used.
*
* @member Context
* @property {Object} manifest map of output files and checksums.
*/
get manifest () {
return this._manifest
}
set manifest (val) {
this._manifest = val
}
/**
* Application sitemap.
*
* Propagated when the `sitemap` plugin is used.
*
* @member Context
* @property {Object} sitemap map of output files and checksums.
*/
get sitemap () {
return this._sitemap
}
set sitemap (val) {
this._sitemap = val
}
/**
* Property that allows plugins to assign arbitrary data to the processing
* context that may be used by other plugins.
*
* @member Context
* @property {Object} data map of custom plugin data.
* @readonly
*/
get data () {
return this._data
}
/**
* Reference to the TreeAdapter class.
*
* Parsers should use this class to provide a consistent API for
* parsing documents to abstract syntax trees with the benefits
* provided by the tree adapter implementation.
*
* @member Context
* @property {Function} TreeAdapter reference to the tree adapter class.
* @readonly
*/
get TreeAdapter () {
return require('./tree-adapter')
}
}
module.exports = Context