feat: use prettier api to load config (#460)

This commit is contained in:
Jason Kuhrt 2020-07-01 23:23:17 -04:00 committed by GitHub
parent 9c8e7762fa
commit ee7c37118c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 25 deletions

View File

@ -89,7 +89,7 @@
"jest": "^26.0.1",
"jest-watch-typeahead": "^0.6.0",
"lint-staged": "^7.3.0",
"prettier": "^1.19.1",
"prettier": "^2.0.5",
"sort-package-json": "^1.22.1",
"ts-jest": "^26.0.0",
"ts-node": "^7.0.1",

View File

@ -225,8 +225,16 @@ export interface BuilderConfig {
outputPath: string
) => TypegenInfo | PromiseLike<TypegenInfo>;
/**
* Either an absolute path to a .prettierrc file, or an object
* with relevant Prettier rules to be used on the generated output
* Adjust the Prettier options used while running prettier over
* the generated output.
*
* Can be an absolute path to a Prettier config file like
* .prettierrc or package.json with "prettier" field, or an object
* of Prettier options.
*
* If provided, you must have prettier available as an importable dep
* in your project.
*
*/
prettierConfig?: string | object;
/**
@ -1651,10 +1659,12 @@ export function makeSchema(config: SchemaConfig): NexusGraphQLSchema {
typegenPromise
.then(() => {
console.log(`Generated Artifacts:
TypeScript Types ==> ${typegenConfig.outputs.typegen ||
"(not enabled)"}
GraphQL Schema ==> ${typegenConfig.outputs.schema ||
"(not enabled)"}`);
TypeScript Types ==> ${
typegenConfig.outputs.typegen || "(not enabled)"
}
GraphQL Schema ==> ${
typegenConfig.outputs.schema || "(not enabled)"
}`);
process.exit(0);
})
.catch((e) => {

View File

@ -930,8 +930,9 @@ const getTypeNames = (
if (isConnectionFieldExtended(fieldConfig)) {
connectionName = `${parentTypeName}${upperFirst(fieldName)}_Connection`;
} else {
connectionName = `${pluginConfig.typePrefix ||
""}${targetTypeName}Connection`;
connectionName = `${
pluginConfig.typePrefix || ""
}${targetTypeName}Connection`;
}
// If we have modified the "edge" at all, then we need
@ -964,10 +965,7 @@ const isEdgeFieldExtended = (fieldConfig: ConnectionFieldConfig) => {
};
const upperFirst = (fieldName: string) => {
return fieldName
.slice(0, 1)
.toUpperCase()
.concat(fieldName.slice(1));
return fieldName.slice(0, 1).toUpperCase().concat(fieldName.slice(1));
};
// Add some sanity checking beyond the normal type checks.

View File

@ -116,7 +116,7 @@ export const fieldAuthorizePlugin = (
}
}
// The authorize wrapping resolver.
return function(root, args, ctx, info, next) {
return function (root, args, ctx, info, next) {
let toComplete;
try {
toComplete = authorize(root, args, ctx, info);

View File

@ -1,10 +1,13 @@
import path from "path";
import type * as Prettier from "prettier";
export type TypegenFormatFn = (
content: string,
type: "types" | "schema"
) => string | Promise<string>;
// todo use Prettier.Options type instead of just `object`
// but will this force us to make prettier a dep then since that type would be user-visible?
export function typegenFormatPrettier(
prettierConfig: string | object
): TypegenFormatFn {
@ -24,6 +27,9 @@ export function typegenFormatPrettier(
);
return content;
}
let prettierConfigResolved: Prettier.Options;
if (typeof prettierConfig === "string") {
/* istanbul ignore if */
if (!path.isAbsolute(prettierConfig)) {
@ -34,13 +40,15 @@ export function typegenFormatPrettier(
);
return content;
}
const fs = require("fs") as typeof import("fs");
const util = require("util") as typeof import("util");
const readFile = util.promisify(fs.readFile);
prettierConfig = JSON.parse(await readFile(prettierConfig, "utf8"));
prettierConfigResolved = (await prettier.resolveConfig("ignore_this", {
config: prettierConfig,
}))!; // non-null assert b/c config file is explicitly passed
} else {
prettierConfigResolved = prettierConfig;
}
return prettier.format(content, {
...(prettierConfig as object),
...prettierConfigResolved,
parser: type === "types" ? "typescript" : "graphql",
});
};

View File

@ -228,8 +228,9 @@ export class TypegenPrinter {
val
)};`;
}
return ` ${key}${val.value.typeDefinition ||
`(...args: any): void`}`;
return ` ${key}${
val.value.typeDefinition || `(...args: any): void`
}`;
})
)
.concat([` }`, `}`])
@ -255,8 +256,9 @@ export class TypegenPrinter {
val
)};`;
}
return ` ${key}${val.value.typeDefinition ||
`(...args: any): void`}`;
return ` ${key}${
val.value.typeDefinition || `(...args: any): void`
}`;
})
)
.concat([` }`, `}`])

View File

@ -62,7 +62,7 @@ export function suggestionList(
}
}
return Object.keys(optionsByDistance).sort(function(a, b) {
return Object.keys(optionsByDistance).sort(function (a, b) {
return optionsByDistance[a] - optionsByDistance[b];
});
}

View File

@ -4390,11 +4390,16 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
prettier@^1.16.0, prettier@^1.19.1:
prettier@^1.16.0:
version "1.19.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
prettier@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==
pretty-format@^23.6.0:
version "23.6.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-23.6.0.tgz#5eaac8eeb6b33b987b7fe6097ea6a8a146ab5760"