From a7f595ca3eb3efa7b98ad46148c88807fc44bf7f Mon Sep 17 00:00:00 2001 From: ikatyang Date: Sat, 9 Sep 2017 12:40:55 +0800 Subject: [PATCH] refactor: move createUsage to util --- src/cli-usage.js | 73 ------------------------------------------------ src/cli-util.js | 69 ++++++++++++++++++++++++++++++++++++++++++++- src/cli.js | 5 ++-- 3 files changed, 70 insertions(+), 77 deletions(-) delete mode 100644 src/cli-usage.js diff --git a/src/cli-usage.js b/src/cli-usage.js deleted file mode 100644 index eb96736e5..000000000 --- a/src/cli-usage.js +++ /dev/null @@ -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 -}; diff --git a/src/cli-util.js b/src/cli-util.js index ae2e4f319..c5270198a 100644 --- a/src/cli-util.js +++ b/src/cli-util.js @@ -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 }; diff --git a/src/cli.js b/src/cli.js index 52ceaa3f2..f40623bcc 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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); } }