refactor: use getter to show warning
parent
b13a104a7b
commit
98c45bb421
|
@ -60,7 +60,8 @@ const options = normalizer.normalizeDetailOptions({
|
||||||
// Deprecated in 0.0.10
|
// Deprecated in 0.0.10
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
isHidden: true,
|
isHidden: true,
|
||||||
isFormatOption: true
|
isFormatOption: true,
|
||||||
|
deprecated: "Use `--parser flow` instead."
|
||||||
},
|
},
|
||||||
help: {
|
help: {
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
|
@ -111,7 +112,8 @@ const options = normalizer.normalizeDetailOptions({
|
||||||
type: "choice",
|
type: "choice",
|
||||||
isFormatOption: true,
|
isFormatOption: true,
|
||||||
choices: ["flow", "babylon", "typescript", "postcss", "json", "graphql"],
|
choices: ["flow", "babylon", "typescript", "postcss", "json", "graphql"],
|
||||||
description: "Specify which parse to use. Defaults to babylon."
|
description: "Specify which parse to use. Defaults to babylon.",
|
||||||
|
getter: (value, argv) => (argv["flow-parser"] ? "flow" : value)
|
||||||
},
|
},
|
||||||
"print-width": {
|
"print-width": {
|
||||||
type: "int",
|
type: "int",
|
||||||
|
|
|
@ -4,7 +4,26 @@ function normalizeDetailOptions(detailOptions) {
|
||||||
const names = Object.keys(detailOptions).sort();
|
const names = Object.keys(detailOptions).sort();
|
||||||
|
|
||||||
const normaliezdOptions = names.map(name =>
|
const normaliezdOptions = names.map(name =>
|
||||||
Object.assign({ name }, detailOptions[name])
|
Object.assign(
|
||||||
|
{
|
||||||
|
name,
|
||||||
|
_getValue: (value, argv) => {
|
||||||
|
const option = detailOptions[name];
|
||||||
|
if (value && option.deprecated) {
|
||||||
|
let warning = `\`--${name}\` is deprecated.`;
|
||||||
|
if (typeof option.deprecated === "string") {
|
||||||
|
warning += ` ${option.deprecated}`;
|
||||||
|
}
|
||||||
|
console.warn(warning);
|
||||||
|
}
|
||||||
|
if (typeof option.getter === "function") {
|
||||||
|
return option.getter(value, argv);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
detailOptions[name]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
normaliezdOptions.forEach(normalizedOption => {
|
normaliezdOptions.forEach(normalizedOption => {
|
||||||
|
@ -14,6 +33,19 @@ function normalizeDetailOptions(detailOptions) {
|
||||||
return normaliezdOptions;
|
return normaliezdOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeArgv(argv, detailOptions) {
|
||||||
|
const normalizedArgv = { _: argv["_"] };
|
||||||
|
|
||||||
|
detailOptions.forEach(option => {
|
||||||
|
Object.defineProperty(normalizedArgv, option.name, {
|
||||||
|
get: () => option._getValue(argv[option.name], normalizedArgv)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return normalizedArgv;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
normalizeArgv,
|
||||||
normalizeDetailOptions
|
normalizeDetailOptions
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,6 +15,7 @@ const cleanAST = require("./clean-ast").cleanAST;
|
||||||
const resolver = require("./resolve-config");
|
const resolver = require("./resolve-config");
|
||||||
const constant = require("./cli-constant");
|
const constant = require("./cli-constant");
|
||||||
const validator = require("./cli-validator");
|
const validator = require("./cli-validator");
|
||||||
|
const normalizer = require("./cli-normalizer");
|
||||||
const apiDefaultOptions = require("./options").defaults;
|
const apiDefaultOptions = require("./options").defaults;
|
||||||
|
|
||||||
function getOptions(argv) {
|
function getOptions(argv) {
|
||||||
|
@ -31,21 +32,10 @@ function getOptions(argv) {
|
||||||
jsxBracketSameLine: argv["jsx-bracket-same-line"],
|
jsxBracketSameLine: argv["jsx-bracket-same-line"],
|
||||||
filepath: argv["stdin-filepath"],
|
filepath: argv["stdin-filepath"],
|
||||||
trailingComma: getTrailingComma(argv),
|
trailingComma: getTrailingComma(argv),
|
||||||
parser: getParserOption(argv)
|
parser: argv["parser"]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getParserOption(argv) {
|
|
||||||
// For backward compatibility. Deprecated in 0.0.10
|
|
||||||
/* istanbul ignore if */
|
|
||||||
if (argv["flow-parser"]) {
|
|
||||||
console.warn("`--flow-parser` is deprecated. Use `--parser flow` instead.");
|
|
||||||
return "flow";
|
|
||||||
}
|
|
||||||
|
|
||||||
return argv["parser"];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getIntOption(argv, optionName) {
|
function getIntOption(argv, optionName) {
|
||||||
const value = argv[optionName];
|
const value = argv[optionName];
|
||||||
|
|
||||||
|
@ -201,15 +191,18 @@ function getOptionsForFile(argv, filePath) {
|
||||||
|
|
||||||
function parseArgsToOptions(args, overrideDefaults) {
|
function parseArgsToOptions(args, overrideDefaults) {
|
||||||
return getOptions(
|
return getOptions(
|
||||||
minimist(args, {
|
normalizer.normalizeArgv(
|
||||||
boolean: constant.booleanOptionNames,
|
minimist(args, {
|
||||||
string: constant.stringOptionNames,
|
boolean: constant.booleanOptionNames,
|
||||||
default: Object.assign(
|
string: constant.stringOptionNames,
|
||||||
{},
|
default: Object.assign(
|
||||||
dashifyObject(apiDefaultOptions),
|
{},
|
||||||
dashifyObject(overrideDefaults)
|
dashifyObject(apiDefaultOptions),
|
||||||
)
|
dashifyObject(overrideDefaults)
|
||||||
})
|
)
|
||||||
|
}),
|
||||||
|
constant.options
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,14 @@ const prettier = eval("require")("../index");
|
||||||
const constant = require("./cli-constant");
|
const constant = require("./cli-constant");
|
||||||
const util = require("./cli-util");
|
const util = require("./cli-util");
|
||||||
const validator = require("./cli-validator");
|
const validator = require("./cli-validator");
|
||||||
|
const normalizer = require("./cli-normalizer");
|
||||||
|
|
||||||
function run(args) {
|
function run(args) {
|
||||||
const argv = minimist(args, constant.minimistOptions);
|
const argv = normalizer.normalizeArgv(
|
||||||
|
minimist(args, constant.minimistOptions),
|
||||||
|
constant.options
|
||||||
|
);
|
||||||
|
|
||||||
argv.__args = args;
|
argv.__args = args;
|
||||||
argv.__filePatterns = argv["_"];
|
argv.__filePatterns = argv["_"];
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue