UNPKG

grunt-gray-matter

Version:

A Grunt task for extracting data header from file contents using Gray Matter

74 lines (57 loc) 2.41 kB
const { read } = require('gray-matter') const { cyan, red } = require('chalk') const set = require('lodash.set') module.exports = ({ registerMultiTask, log, verbose, file: { write }, util: { pluralize } }) => registerMultiTask('grayMatter', 'Extract data from specified files with Gray Matter', function () { const options = this.options({ baseDir: '', preprocessPath: undefined, preprocessMatterData: undefined, preprocessData: undefined, replacer: null, space: 2, parser: undefined, eval: false, lang: undefined, delims: undefined }) if (!this.files.length) return log.error('No files specified.') const { preprocessPath, preprocessMatterData, preprocessData } = options let processedFiles = 0 let errors = 0 this.files.forEach((file) => { const { dest, src } = file let data = {} if (!src.length) return log.error(`No source files specified for ${cyan(dest)}.`) if (!dest) return log.error('No dest file specified') src.forEach((filepath) => { let matter try { matter = read(filepath, options) } catch (error) { errors++ return log.error(`Error in ${filepath}:\n\n${error}`) } let matterData = matter.data let path = filepath.replace(options.baseDir, '') if (typeof preprocessPath === 'function') { path = preprocessPath.call(file, path, filepath) } if (typeof preprocessMatterData === 'function') { matterData = preprocessMatterData.call(file, matterData, path, filepath) } set(data, path, matterData) processedFiles++ verbose.ok(`\nProcessed: ${cyan(filepath)}`) }) if (typeof preprocessData === 'function') { data = preprocessData.call(this, data) } write(dest, JSON.stringify(data, options.replacer, options.space)) verbose.ok(`File ${cyan(dest)} created`) }) const processedMessage = `${cyan(processedFiles)} ${pluralize(processedFiles, 'file/files')} processed` const failedMessage = `${errors ? `, ${red(errors)} failed` : ''}` log[errors ? 'error' : 'ok'](processedMessage + failedMessage) if (errors) throw new Error(`${errors} ${pluralize(errors, 'error/errors')} has been encountered during parsing Gray Matter.\nSee log above for details.`) })