apeman-tmpl
Version:
Template manager for apeman.
78 lines (73 loc) • 2.51 kB
JavaScript
/**
* Logger for list.
* @memberof module:apeman-tmpl/lib/logging
* @constructor ListLogger
*/
;
const colorprint = require('colorprint'),
os = require('os'),
path = require('path'),
textconv = require('textconv'),
iftype = require('iftype'),
colors = colorprint.colors;
module.exports = colorprint.define({
/**
* Log templates.
* @param {object[]} tmpls - Template config.
* @param {object[]} childTmpls - Child templates.
*/
logTemplates: function (tmpls, childTmpls) {
const MIN_NAME_LEN = 28;
let 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)),
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) => {
s.debug(' // Tmpls in "' + childName + '"');
childTmpls[childName].forEach((tmpl) => {
let name = path.relative(cwd, (tmplPath === tmpl.path) ? '' : String(tmpl.path)),
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: function () {
let s = this;
},
_descTemplate: function (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],
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: {/*...*/}`;
}
});