refactor: move createUsage to util

This commit is contained in:
ikatyang committed 2017-09-09 12:40:55 +08:00
1 parent 82eaadc03a
commit a7f595ca3e
3 files changed
+70 -77

No files matched your search

-73
View File
@@ -1,73 +0,0 @@
"use strict";
function create(detailOptions) {
const options = detailOptions.filter(option => !option.hidden);
const groupedUsages = options.reduce((current, option) => {
const category = capitalize(option.category || "uncategorized");
const group = (current[category] = current[category] || []);
group.push(createOptionUsage(option));
return current;
}, {});
let usage = "Usage: prettier [opts] [filename ...]\n\n";
usage += Object.keys(groupedUsages)
.sort()
.map(category => {
return `${category} options:\n\n${indent(
groupedUsages[category].join("\n"),
2
)}`;
})
.join("\n\n");
return usage + "\n\n";
}
function createOptionUsage(option) {
const threshold = 25;
let header = `--${option.name}`;
if (option.alias) {
header += ` or -${option.alias}`;
}
switch (option.type) {
case "boolean":
// do nothing
break;
case "choice":
header += ` <${option.choices
.filter(choice => !choice.deprecated)
.map(choice => choice.value)
.join("|")}>`;
break;
default:
header += ` <${option.type}>`;
break;
}
if (header.length >= threshold) {
header += "\n" + " ".repeat(threshold);
} else {
header += " ".repeat(threshold - header.length);
}
return (
header + option.description.replace(/\n/g, "\n" + " ".repeat(threshold))
);
}
function capitalize(str) {
return str.replace(/^[a-z]/, char => char.toUpperCase());
}
function indent(str, spaces) {
return str.replace(/^/gm, " ".repeat(spaces));
}
module.exports = {
create
};
+68 -1
View File
@@ -332,9 +332,76 @@ function formatFiles(argv) {
});
}
function createUsage() {
const options = constant.detailOptions.filter(option => !option.hidden);
const groupedUsages = options.reduce((current, option) => {
const category = capitalize(option.category || "uncategorized");
const group = (current[category] = current[category] || []);
group.push(createOptionUsage(option, 25));
return current;
}, {});
let usage = "Usage: prettier [opts] [filename ...]\n\n";
usage += Object.keys(groupedUsages)
.sort()
.map(category => {
return `${category} options:\n\n${indent(
groupedUsages[category].join("\n"),
2
)}`;
})
.join("\n\n");
return usage + "\n\n";
}
function createOptionUsage(option, threshold) {
let header = `--${option.name}`;
if (option.alias) {
header += ` or -${option.alias}`;
}
switch (option.type) {
case "boolean":
// do nothing
break;
case "choice":
header += ` <${option.choices
.filter(choice => !choice.deprecated)
.map(choice => choice.value)
.join("|")}>`;
break;
default:
header += ` <${option.type}>`;
break;
}
if (header.length >= threshold) {
header += "\n" + " ".repeat(threshold);
} else {
header += " ".repeat(threshold - header.length);
}
return (
header + option.description.replace(/\n/g, "\n" + " ".repeat(threshold))
);
}
function capitalize(str) {
return str.replace(/^[a-z]/, char => char.toUpperCase());
}
function indent(str, spaces) {
return str.replace(/^/gm, " ".repeat(spaces));
}
module.exports = {
logResolvedConfigPathOrDie,
format,
formatStdin,
formatFiles
formatFiles,
createUsage
};
+2 -3
View File
@@ -7,7 +7,6 @@ const constant = require("./cli-constant");
const util = require("./cli-util");
const validator = require("./cli-validator");
const normalizer = require("./cli-normalizer");
const usage = require("./cli-usage");
function run(args) {
const argv = normalizer.normalizeArgv(
@@ -26,7 +25,7 @@ function run(args) {
}
if (argv["help"]) {
console.log(usage.create(constant.detailOptions));
console.log(util.createUsage());
process.exit(0);
}
@@ -40,7 +39,7 @@ function run(args) {
} else if (hasFilePatterns) {
util.formatFiles(argv);
} else {
console.log(usage.create(constant.detailOptions));
console.log(util.createUsage());
process.exit(1);
}
}