nyx_server
Version:
Node内容发布
76 lines (64 loc) • 1.77 kB
JavaScript
/* global __dirname */
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');
var colors = require('./colors');
var format = function (data) {
if (Array.isArray(data)) {
return data;
}
if (typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean') {
return [data];
}
return data;
};
var createPadded = function (count) {
return (new Array(count + 1)).join(' ');
};
var getMax = function (list) {
var max = 0;
list.forEach(function (key) {
if (key.length > max) {
max = key.length;
}
});
return max;
};
module.exports = {
summary: function (commands, themeUrl) {
themeUrl = themeUrl || path.resolve(__dirname, './ejs/help-summary.ejs');
var template = fs.readFileSync(themeUrl);
var max = getMax(Object.keys(commands));
var datas = [];
Object.keys(commands).forEach(function (key) {
datas.push({
name: key,
space: createPadded(max - key.length),
info: commands[key].optimist && commands[key].optimist().info()
});
});
eval((ejs.render(template.toString(), { datas: datas })));
},
detailed: function (optimist, themeUrl) {
themeUrl = themeUrl || path.resolve(__dirname, './ejs/help-detailed.ejs');
var template = fs.readFileSync(themeUrl);
var datas = {
usage: optimist.usage(),
properties: []
};
var p;
for (var key in optimist.options) {
p = optimist.options[key];
datas.properties.push({
name: key,
type: format(p.type),
required: p.require,
alias: p.alias,
enum: p.enum,
default: p.default,
info: p.describe
});
}
eval(ejs.render(template.toString(), { datas: datas }));
}
};