UNPKG

apeman-tmpl

Version:
79 lines (73 loc) 2.19 kB
/** * Logger for list. * @memberof module:apeman-tmpl/lib/logging * @constructor ListLogger */ 'use strict' const colorprint = require('colorprint') const path = require('path') const textconv = require('textconv') const iftype = require('iftype') const { colors } = colorprint module.exports = colorprint.define({ /** * Log templates. * @param {object[]} tmpls - Template config. * @param {object[]} childTmpls - Child templates. */ logTemplates (tmpls, childTmpls) { const MIN_NAME_LEN = 28 const s = this s.debug('') s.debug(' Available templates:') s.debug('') let cwd = process.cwd() let tmplPath; [].concat(tmpls).forEach((tmpl) => { let name = path.relative(cwd, (tmplPath === tmpl.path) ? '' : String(tmpl.path)) let desc = s._descTemplate(tmpl) tmplPath = tmpl.path s.debug(` ${colors.black.bold(textconv.pad(name, MIN_NAME_LEN))}${colors.blackBright(desc)}`) }) s.debug('') Object.keys(childTmpls || {}) .forEach((childName) => { let found = childTmpls[ childName ] s.debug(` // Tmpls in "${childName}" (${found.length} hits)`) found.forEach((tmpl) => { let name = path.relative(cwd, (tmplPath === tmpl.path) ? '' : String(tmpl.path)) let desc = s._descTemplate(tmpl) tmplPath = tmpl.path s.debug(` ${colors.black.bold(textconv.pad(name, MIN_NAME_LEN))}${colors.blackBright(desc)}`) }) s.debug('') }) s.debug('') }, logInstruction () { }, _descTemplate (tmpl) { const MAX_LEN = 32 let data = { force: false, mode: '644', tmpl: '' } Object.keys(tmpl).forEach((key) => { switch (key) { case 'path': case 'mkdirp': return default: break } let val = tmpl[ key ] let isStr = iftype(val).isString() let desc = textconv.chop(textconv.inline(String(val), { trim: true }), MAX_LEN, { ellipsis: true }) data[ key ] = isStr ? `'${desc}'` : desc }) return `// force: ${data.force}, mode: ${data.mode}, tmpl: ${data.tmpl}, data: {/* ... */}` } })