Skip to content

Commit

Permalink
Merge branch 'refactor/options' into feat/support-options
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Dec 12, 2017
2 parents e32ac72 + 65c27e2 commit 74b0503
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 666 deletions.
461 changes: 167 additions & 294 deletions src/cli-constant.js

Large diffs are not rendered by default.

138 changes: 11 additions & 127 deletions src/cli-util.js
Expand Up @@ -15,11 +15,15 @@ const prettier = eval("require")("../index");
const cleanAST = require("./clean-ast").cleanAST;
const resolver = require("./resolve-config");
const constant = require("./cli-constant");
const validator = require("./cli-validator");
const optionsNormalizer = require("./options-normalizer");
const apiDefaultOptions = require("./options").defaults;
const errors = require("./errors");
const logger = require("./cli-logger");
const thirdParty = require("./third-party");
const optionInfos = require("./support").getSupportInfo(null, {
showDeprecated: true,
showUnreleased: true
}).options;

const OPTION_USAGE_THRESHOLD = 25;
const CHOICE_USAGE_MARGIN = 3;
Expand Down Expand Up @@ -178,7 +182,8 @@ function getOptionsForFile(argv, filepath) {
{ filepath },
applyConfigPrecedence(
argv,
options && normalizeConfig("api", options, constant.detailedOptionMap)
options &&
optionsNormalizer.normalizeApiOptions(options, optionInfos, { logger })
)
);

Expand All @@ -191,8 +196,7 @@ function getOptionsForFile(argv, filepath) {

function parseArgsToOptions(argv, overrideDefaults) {
return getOptions(
normalizeConfig(
"cli",
optionsNormalizer.normalizeCliOptions(
minimist(
argv.__args,
Object.assign({
Expand All @@ -205,7 +209,8 @@ function parseArgsToOptions(argv, overrideDefaults) {
)
})
),
{ warning: false }
constant.detailedOptions,
{ logger: false }
)
);
}
Expand Down Expand Up @@ -589,132 +594,11 @@ function groupBy(array, getKey) {
}, Object.create(null));
}

/** @param {'api' | 'cli'} type */
function normalizeConfig(type, rawConfig, options) {
if (type === "api" && rawConfig === null) {
return null;
}

options = options || {};

const consoleWarn =
options.warning === false ? () => {} : logger.warn.bind(logger);

const normalized = {};

Object.keys(rawConfig).forEach(rawKey => {
const rawValue = rawConfig[rawKey];

const key = type === "cli" ? rawKey : dashify(rawKey);

if (type === "cli" && key === "_") {
normalized[rawKey] = rawValue;
return;
}

if (type === "cli" && key.length === 1) {
// do nothing with alias
return;
}

const option = constant.detailedOptionMap[key];

// unknown option
if (option === undefined) {
if (type === "api") {
consoleWarn(`Ignored unknown option: ${rawKey}`);
} else {
const optionName = rawValue === false ? `no-${rawKey}` : rawKey;
consoleWarn(`Ignored unknown option: --${optionName}`);
}
return;
}

const value = getValue(rawValue, option);

if (option.exception !== undefined) {
if (typeof option.exception === "function") {
if (option.exception(value)) {
normalized[rawKey] = value;
return;
}
} else {
if (value === option.exception) {
normalized[rawKey] = value;
return;
}
}
}

try {
switch (option.type) {
case "int":
validator.validateIntOption(type, value, option);
normalized[rawKey] = Number(value);
break;
case "choice":
validator.validateChoiceOption(type, value, option);
normalized[rawKey] = value;
break;
default:
normalized[rawKey] = value;
break;
}
} catch (error) {
logger.error(error.message);
process.exit(2);
}
});

return normalized;

function getOptionName(option) {
return type === "cli" ? `--${option.name}` : camelCase(option.name);
}

function getRedirectName(option, choice) {
return type === "cli"
? `--${option.name}=${choice.redirect}`
: `{ ${camelCase(option.name)}: ${JSON.stringify(choice.redirect)} }`;
}

function getValue(rawValue, option) {
const optionName = getOptionName(option);
if (rawValue && option.deprecated) {
let warning = `\`${optionName}\` is deprecated.`;
if (typeof option.deprecated === "string") {
warning += ` ${option.deprecated}`;
}
consoleWarn(warning);
}

const value = option.getter(rawValue, rawConfig);

if (option.type === "choice") {
const choice = option.choices.find(choice => choice.value === rawValue);
if (choice !== undefined && choice.deprecated) {
const warningDescription =
rawValue === ""
? "without an argument"
: `with value \`${rawValue}\``;
const redirectName = getRedirectName(option, choice);
consoleWarn(
`\`${optionName}\` ${warningDescription} is deprecated. Prettier now treats it as: \`${redirectName}\`.`
);
return choice.redirect;
}
}

return value;
}
}

module.exports = {
logResolvedConfigPathOrDie,
format,
formatStdin,
formatFiles,
createUsage,
createDetailedUsage,
normalizeConfig
createDetailedUsage
};
55 changes: 0 additions & 55 deletions src/cli-validator.js

This file was deleted.

96 changes: 57 additions & 39 deletions src/cli.js
Expand Up @@ -4,59 +4,77 @@ const minimist = require("minimist");

const prettier = eval("require")("../index");
const constant = require("./cli-constant");
const normalizer = require("./options-normalizer");
const util = require("./cli-util");
const validator = require("./cli-validator");
const logger = require("./cli-logger");

function run(args) {
const rawArgv = minimist(args, constant.minimistOptions);
try {
const rawArgv = minimist(args, constant.minimistOptions);

process.env[logger.ENV_LOG_LEVEL] =
rawArgv["loglevel"] || constant.detailedOptionMap["loglevel"].default;
process.env[logger.ENV_LOG_LEVEL] =
rawArgv["loglevel"] || constant.detailedOptionMap["loglevel"].default;

const argv = util.normalizeConfig("cli", rawArgv);
const argv = normalizer.normalizeCliOptions(
rawArgv,
constant.detailedOptions,
{ logger }
);

logger.debug(`normalized argv: ${JSON.stringify(argv)}`);
logger.debug(`normalized argv: ${JSON.stringify(argv)}`);

argv.__args = args;
argv.__filePatterns = argv["_"];
argv.__args = args;
argv.__filePatterns = argv["_"];

validator.validateArgv(argv);
if (argv["write"] && argv["debug-check"]) {
logger.error("Cannot use --write and --debug-check together.");
process.exit(1);
}

if (argv["version"]) {
console.log(prettier.version);
process.exit(0);
}
if (argv["find-config-path"] && argv.__filePatterns.length) {
logger.error("Cannot use --find-config-path with multiple files");
process.exit(1);
}

if (argv["help"] !== undefined) {
console.log(
typeof argv["help"] === "string" && argv["help"] !== ""
? util.createDetailedUsage(argv["help"])
: util.createUsage()
);
process.exit(0);
}
if (argv["version"]) {
console.log(prettier.version);
process.exit(0);
}

if (argv["support-info"]) {
console.log(
prettier.format(JSON.stringify(prettier.getSupportInfo()), {
parser: "json"
})
);
process.exit(0);
}
if (argv["help"] !== undefined) {
console.log(
typeof argv["help"] === "string" && argv["help"] !== ""
? util.createDetailedUsage(argv["help"])
: util.createUsage()
);
process.exit(0);
}

if (argv["support-info"]) {
console.log(
prettier.format(JSON.stringify(prettier.getSupportInfo()), {
parser: "json"
})
);
process.exit(0);
}

const hasFilePatterns = argv.__filePatterns.length !== 0;
const useStdin = argv["stdin"] || (!hasFilePatterns && !process.stdin.isTTY);
const hasFilePatterns = argv.__filePatterns.length !== 0;
const useStdin =
argv["stdin"] || (!hasFilePatterns && !process.stdin.isTTY);

if (argv["find-config-path"]) {
util.logResolvedConfigPathOrDie(argv["find-config-path"]);
} else if (useStdin) {
util.formatStdin(argv);
} else if (hasFilePatterns) {
util.formatFiles(argv);
} else {
console.log(util.createUsage());
if (argv["find-config-path"]) {
util.logResolvedConfigPathOrDie(argv["find-config-path"]);
} else if (useStdin) {
util.formatStdin(argv);
} else if (hasFilePatterns) {
util.formatFiles(argv);
} else {
console.log(util.createUsage());
process.exit(1);
}
} catch (error) {
logger.error(error.message);
process.exit(1);
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/deprecated.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/options-descriptor.js
@@ -0,0 +1,22 @@
"use strict";

function apiDescriptor(name, value) {
return arguments.length === 1
? JSON.stringify(name)
: `\`{ ${apiDescriptor(name)}: ${JSON.stringify(value)} }\``;
}

function cliDescriptor(name, value) {
return value === false
? `\`--no-${name}\``
: value === true || arguments.length === 1
? `\`--${name}\``
: value === ""
? `\`--${name}\` without an argument`
: `\`--${name}=${value}\``;
}

module.exports = {
apiDescriptor,
cliDescriptor
};

0 comments on commit 74b0503

Please sign in to comment.