makestatic-clean-output
Version:
Removes the output directory
62 lines (55 loc) • 1.61 kB
JavaScript
/**
* Plugin to remove the output directory, makes sure your build is pristine.
*
* This plugin is activated when the `clean` boolean option is set.
*
* By default it will remove the entire `output` directory however if the
* configuration option `clean` is set to an array of glob patterns then
* only the files matching the configured glob patterns are removed.
*
* @class CleanOutput
*
* @see /docs/api/core-standard/ Core Standard
*/
class CleanOutput {
/**
* Removes the output directory.
*
* @function after
* @param {Object} context the processing context.
*
* @throws Error if the unlink operation fails.
*
* @returns a promise that resolves when the output directory is removed.
*/
after (context) {
const when = require('when')
const clean = require('rimraf')
const log = context.log
let files = [context.options.output]
let glob = false
// NOTE: use the raw config list so it doesn't
// NOTE: conflict with the cli --clean boolean
if (Array.isArray(context.config.raw.clean)) {
files = context.config.raw.clean
glob = true
}
// not enabled
if (!context.options.clean) {
return when.resolve()
}
return when.map(files, (file) => {
log.info('[clean] %s', file)
return when.promise((resolve, reject) => {
clean(file, {glob: glob}, (err) => {
/* istanbul ignore next: not going to mock this error */
if (err) {
return reject(err)
}
resolve()
})
})
})
}
}
module.exports = CleanOutput