refactor(cli): extract validate-related functions

master
ikatyang 2017-09-07 10:18:20 +08:00
parent 87503e5ef7
commit d139006c98
3 changed files with 33 additions and 22 deletions

View File

@ -312,8 +312,8 @@ function eachFilename(argv, patterns, callback) {
}
}
function formatFiles(argv, filepatterns) {
eachFilename(argv, filepatterns, (filename, options) => {
function formatFiles(argv) {
eachFilename(argv, argv.__filePatterns, (filename, options) => {
if (argv["write"]) {
// Don't use `console.log` here since we need to replace this line.
process.stdout.write(filename);

17
src/cli-validator.js Normal file
View File

@ -0,0 +1,17 @@
"use strict";
function validateArgv(argv) {
if (argv["write"] && argv["debug-check"]) {
console.error("Cannot use --write and --debug-check together.");
process.exit(1);
}
if (argv["find-config-path"] && argv.__filePatterns.length) {
console.error("Cannot use --find-config-path with multiple files");
process.exit(1);
}
}
module.exports = {
validateArgv
};

View File

@ -5,43 +5,37 @@ const minimist = require("minimist");
const prettier = eval("require")("../index");
const constant = require("./cli-constant");
const util = require("./cli-util");
const validator = require("./cli-validator");
function run(args) {
const argv = minimist(args, constant.options);
argv.__args = args;
argv.__filePatterns = argv["_"];
validator.validateArgv(argv);
if (argv["version"]) {
console.log(prettier.version);
process.exit(0);
}
const filepatterns = argv["_"];
const stdin = argv["stdin"] || (!filepatterns.length && !process.stdin.isTTY);
if (argv["write"] && argv["debug-check"]) {
console.error("Cannot use --write and --debug-check together.");
process.exit(1);
}
if (argv["find-config-path"] && filepatterns.length) {
console.error("Cannot use --find-config-path with multiple files");
process.exit(1);
}
if (
argv["help"] ||
(!filepatterns.length && !stdin && !argv["find-config-path"])
) {
if (argv["help"]) {
console.log(constant.usage);
process.exit(argv["help"] ? 0 : 1);
process.exit(0);
}
const hasFilePatterns = argv.__filePatterns.length !== 0;
const useStdin = argv["stdin"] || (!hasFilePatterns && !process.stdin.isTTY);
if (argv["find-config-path"]) {
util.resolveConfig(argv["find-config-path"]);
} else if (stdin) {
} else if (useStdin) {
util.formatStdin(argv);
} else if (hasFilePatterns) {
util.formatFiles(argv);
} else {
util.formatFiles(argv, filepatterns);
console.log(constant.usage);
process.exit(1);
}
}