diff --git a/src/cli-constant.js b/src/cli-constant.js index b95f1fe63..bb0bfe97e 100644 --- a/src/cli-constant.js +++ b/src/cli-constant.js @@ -1,6 +1,8 @@ "use strict"; -const options = sortAndAddNameKey({ +const normalizer = require("./cli-normalizer"); + +const options = normalizer.normalizeDetailOptions({ "bracket-spacing": { type: "boolean", isFormatOption: true, @@ -188,31 +190,29 @@ const options = sortAndAddNameKey({ } }); -const optionArray = Object.keys(options).map(name => options[name]); - -const booleanOptionNames = optionArray +const booleanOptionNames = options .filter(option => option.isFormatOption && option.type === "boolean") .map(option => option.name); -const stringOptionNames = optionArray +const stringOptionNames = options .filter(option => option.isFormatOption && option.type !== "boolean") .map(option => option.name); const minimistOptions = { - boolean: optionArray + boolean: options .filter(option => !option.isDocsOnly && option.type === "boolean") .map(option => option.name), - string: optionArray + string: options .filter(option => !option.isDocsOnly && option.type !== "boolean") .map(option => option.name), - default: optionArray + default: options .filter(option => option.default !== undefined) .reduce( (current, option) => Object.assign({ [option.name]: option.default }, current), {} ), - alias: optionArray + alias: options .filter(option => option.alias !== undefined) .reduce( (current, option) => @@ -232,7 +232,7 @@ Usage: prettier [opts] [filename ...] Available options: ${indent( - optionArray + options .filter(option => !option.isHidden) .map(createOptionUsage) .join("\n"), @@ -282,16 +282,6 @@ function dedent(str) { return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim(); } -function sortAndAddNameKey(obj) { - return Object.keys(obj) - .sort() - .reduce( - (current, name) => - Object.assign(current, { [name]: Object.assign({ name }, obj[name]) }), - {} - ); -} - module.exports = { booleanOptionNames, stringOptionNames, diff --git a/src/cli-normalizer.js b/src/cli-normalizer.js new file mode 100644 index 000000000..789016db4 --- /dev/null +++ b/src/cli-normalizer.js @@ -0,0 +1,19 @@ +"use strict"; + +function normalizeDetailOptions(detailOptions) { + const names = Object.keys(detailOptions).sort(); + + const normaliezdOptions = names.map(name => + Object.assign({ name }, detailOptions[name]) + ); + + normaliezdOptions.forEach(normalizedOption => { + normaliezdOptions[normalizedOption.name] = normalizedOption; + }); + + return normaliezdOptions; +} + +module.exports = { + normalizeDetailOptions +};