refactor: getOwnPackage util (#409)

This commit is contained in:
Jason Kuhrt 2020-04-07 11:10:03 -04:00 committed by GitHub
parent 3156f9bc46
commit b9df04b883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 16 deletions

View File

@ -8,7 +8,6 @@ import { completeValue, plugin } from "../plugin";
import {
ArgsValue,
GetGen,
getPackageNameForImport,
MaybePromise,
MaybePromiseDeep,
ResultValue,
@ -16,6 +15,7 @@ import {
} from "../typegenTypeHelpers";
import {
eachObj,
getOwnPackage,
isObject,
isPromiseLike,
mapObj,
@ -340,7 +340,7 @@ export const connectionPlugin = (
name: "ConnectionPlugin",
fieldDefTypes: [
printedGenTypingImport({
module: getPackageNameForImport(),
module: getOwnPackage().name,
bindings: ["core", "connectionPluginCore"],
}),
],

View File

@ -29,9 +29,9 @@ import {
} from "./definitions/wrapping";
import { NexusGraphQLSchema } from "./definitions/_types";
import { StringLike } from "./plugin";
import { getPackageNameForImport } from "./typegenTypeHelpers";
import {
eachObj,
getOwnPackage,
GroupedTypes,
groupTypes,
mapObj,
@ -155,7 +155,7 @@ export class TypegenPrinter {
const outputPath = this.typegenInfo.typegenFile;
// For backward compat.
const packName = getPackageNameForImport();
const packName = getOwnPackage().name;
if (!this.printImports[packName]) {
if (
[dynamicInputFields, dynamicOutputFields].some(

View File

@ -1,17 +1,5 @@
import { GraphQLResolveInfo } from "graphql";
export function getPackageNameForImport(): string {
try {
return require("../package.json").name;
} catch (e) {
console.error(
'Failed to get name from package manifest for typegen. Falling back to hardcoded "@nexus/schema". Error was:\n\n'
);
console.error(e);
return "@nexus/schema";
}
}
declare global {
interface NexusGen {}
interface NexusGenCustomInputMethods<TypeName extends string> {}

View File

@ -488,3 +488,7 @@ export function pathToArray(
}
return flattened.reverse();
}
export function getOwnPackage(): { name: string } {
return require("../package.json");
}