Merge pull request #817 from quicktype/fix-extension-lang

Fix picking a language when using quicktype as a library
This commit is contained in:
Mark Probst 2018-04-23 13:26:04 -04:00 коммит произвёл GitHub
Родитель 171c3409d0 ab98f6a051
Коммит 010b769515
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 27 добавлений и 18 удалений

Просмотреть файл

@ -31,8 +31,11 @@ export const all: TargetLanguage[] = [
new RubyTargetLanguage()
];
export function languageNamed(name: string): TargetLanguage | undefined {
const maybeTargetLanguage = find(all, l => includes(l.names, name) || l.displayName === name);
export function languageNamed(name: string, targetLanguages?: TargetLanguage[]): TargetLanguage | undefined {
if (targetLanguages === undefined) {
targetLanguages = all;
}
const maybeTargetLanguage = find(targetLanguages, l => includes(l.names, name) || l.displayName === name);
if (maybeTargetLanguage !== undefined) return maybeTargetLanguage;
return find(all, l => l.extension === name);
return find(targetLanguages, l => l.extension === name);
}

Просмотреть файл

@ -236,7 +236,7 @@ function inferTopLevel(options: Partial<CLIOptions>): string {
return "TopLevel";
}
export function inferCLIOptions(opts: Partial<CLIOptions>, defaultLanguage?: string): CLIOptions {
function inferCLIOptions(opts: Partial<CLIOptions>, targetLanguage: TargetLanguage | undefined): CLIOptions {
let srcLang = opts.srcLang;
if (opts.graphqlSchema !== undefined || opts.graphqlIntrospect !== undefined) {
messageAssert(srcLang === undefined || srcLang === "graphql", ErrorMessage.DriverSourceLangMustBeGraphQL);
@ -248,13 +248,16 @@ export function inferCLIOptions(opts: Partial<CLIOptions>, defaultLanguage?: str
srcLang = withDefault<string>(srcLang, "json");
}
if (defaultLanguage === undefined) {
defaultLanguage = defaultDefaultTargetLanguageName;
}
const lang = opts.lang !== undefined ? opts.lang : inferLang(opts, defaultLanguage);
const language = languageNamed(lang);
if (language === undefined) {
return messageError(ErrorMessage.DriverUnknownOutputLanguage, { lang });
let language: TargetLanguage;
if (targetLanguage !== undefined) {
language = targetLanguage;
} else {
const languageName = opts.lang !== undefined ? opts.lang : inferLang(opts, defaultDefaultTargetLanguageName);
const maybeLanguage = languageNamed(languageName);
if (maybeLanguage === undefined) {
return messageError(ErrorMessage.DriverUnknownOutputLanguage, { lang: languageName });
}
language = maybeLanguage;
}
/* tslint:disable:strict-boolean-expressions */
@ -513,10 +516,8 @@ const sectionsAfterRenderers: UsageSection[] = [
];
export function parseCLIOptions(argv: string[], targetLanguage?: TargetLanguage): CLIOptions {
const defaultLanguage = targetLanguage === undefined ? defaultDefaultTargetLanguageName : targetLanguage.names[0];
if (argv.length === 0) {
return inferCLIOptions({ help: true }, defaultLanguage);
return inferCLIOptions({ help: true }, targetLanguage);
}
const targetLanguages = targetLanguage === undefined ? defaultTargetLanguages.all : [targetLanguage];
@ -526,7 +527,7 @@ export function parseCLIOptions(argv: string[], targetLanguage?: TargetLanguage)
// because there are renderer-specific options. But we only know which renderer
// is selected after we've parsed the options. Hence, we parse the options
// twice. This is the first parse to get the renderer:
const incompleteOptions = inferCLIOptions(parseOptions(optionDefinitions, argv, true), defaultLanguage);
const incompleteOptions = inferCLIOptions(parseOptions(optionDefinitions, argv, true), targetLanguage);
if (targetLanguage === undefined) {
targetLanguage = getTargetLanguage(incompleteOptions.lang);
}
@ -534,7 +535,7 @@ export function parseCLIOptions(argv: string[], targetLanguage?: TargetLanguage)
// Use the global options as well as the renderer options from now on:
const allOptionDefinitions = _.concat(optionDefinitions, rendererOptionDefinitions);
// This is the parse that counts:
return inferCLIOptions(parseOptions(allOptionDefinitions, argv, false), defaultLanguage);
return inferCLIOptions(parseOptions(allOptionDefinitions, argv, false), targetLanguage);
}
// Parse the options in argv and split them into global options and renderer options,
@ -768,8 +769,13 @@ export async function makeQuicktypeOptions(
leadingComments = telemetry.TELEMETRY_HEADER.split("\n").concat(leadingComments);
}
const lang = languageNamed(options.lang, targetLanguages);
if (lang === undefined) {
return messageError(ErrorMessage.DriverUnknownOutputLanguage, { lang: options.lang });
}
return {
lang: options.lang,
lang,
sources,
inferMaps: !options.noMaps,
inferEnums: !options.noEnums,
@ -829,7 +835,7 @@ export async function main(args: string[] | Partial<CLIOptions>) {
if (Array.isArray(args)) {
cliOptions = parseCLIOptions(args);
} else {
cliOptions = inferCLIOptions(args, defaultDefaultTargetLanguageName);
cliOptions = inferCLIOptions(args, undefined);
}
if (cliOptions.telemetry !== undefined) {