From 63dc8f2054e27b944f7d8dc59db8afa85760a127 Mon Sep 17 00:00:00 2001 From: Charly POLY <1252066+charlypoly@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:19:46 +0100 Subject: [PATCH] refactor: remove `DetailledError`, not supported by Listr renderer (#8525) Co-authored-by: github-actions[bot] --- ...degen_flutter-freezed-8525-dependencies.md | 5 ++++ .changeset/great-mails-yawn.md | 9 ++++++ packages/graphql-codegen-cli/src/cli.ts | 15 ++++------ packages/graphql-codegen-cli/src/codegen.ts | 28 ++++++------------- packages/graphql-codegen-cli/src/config.ts | 14 ++++------ packages/graphql-codegen-cli/src/plugins.ts | 8 ++---- .../src/utils/cli-error.ts | 4 +-- .../graphql-codegen-cli/tests/codegen.spec.ts | 8 +++--- packages/graphql-codegen-core/src/codegen.ts | 7 ++--- .../src/execute-plugin.ts | 12 ++++---- .../plugins/dart/flutter-freezed/package.json | 2 +- .../visitor-plugin-common/src/enum-values.ts | 13 ++++----- .../src/resolve-document-imports.ts | 13 ++++----- packages/utils/plugins-helpers/src/errors.ts | 11 -------- packages/utils/plugins-helpers/src/index.ts | 1 - patches/listr2+4.0.5.patch | 13 +++++++++ yarn.lock | 16 ----------- 17 files changed, 74 insertions(+), 105 deletions(-) create mode 100644 .changeset/@graphql-codegen_flutter-freezed-8525-dependencies.md create mode 100644 .changeset/great-mails-yawn.md delete mode 100644 packages/utils/plugins-helpers/src/errors.ts create mode 100644 patches/listr2+4.0.5.patch diff --git a/.changeset/@graphql-codegen_flutter-freezed-8525-dependencies.md b/.changeset/@graphql-codegen_flutter-freezed-8525-dependencies.md new file mode 100644 index 00000000000..55004163dcb --- /dev/null +++ b/.changeset/@graphql-codegen_flutter-freezed-8525-dependencies.md @@ -0,0 +1,5 @@ +--- +"@graphql-codegen/flutter-freezed": patch +--- +dependencies updates: + - Updated dependency [`@graphql-codegen/visitor-plugin-common@2.13.0` ↗︎](https://www.npmjs.com/package/@graphql-codegen/visitor-plugin-common/v/2.13.0) (from `2.12.0`, in `dependencies`) diff --git a/.changeset/great-mails-yawn.md b/.changeset/great-mails-yawn.md new file mode 100644 index 00000000000..008f995ca5c --- /dev/null +++ b/.changeset/great-mails-yawn.md @@ -0,0 +1,9 @@ +--- +'@graphql-codegen/cli': patch +'@graphql-codegen/core': patch +'@graphql-codegen/visitor-plugin-common': patch +'@graphql-codegen/near-operation-file-preset': patch +'@graphql-codegen/plugin-helpers': patch +--- + +remove `DetailledError`, not supported by Listr renderer diff --git a/packages/graphql-codegen-cli/src/cli.ts b/packages/graphql-codegen-cli/src/cli.ts index 5a8374ba0cb..11362959656 100644 --- a/packages/graphql-codegen-cli/src/cli.ts +++ b/packages/graphql-codegen-cli/src/cli.ts @@ -2,7 +2,6 @@ import { generate } from './generate-and-save.js'; import { init } from './init/index.js'; import { createContext } from './config.js'; import { lifecycleHooks } from './hooks.js'; -import { DetailedError } from '@graphql-codegen/plugin-helpers'; export async function runCli(cmd: string): Promise { await ensureGraphQlPackage(); @@ -33,14 +32,12 @@ export async function ensureGraphQlPackage() { try { await import('graphql'); } catch (e) { - throw new DetailedError( - `Unable to load "graphql" package. Please make sure to install "graphql" as a dependency!`, - ` - To install "graphql", run: - yarn add graphql - Or, with NPM: - npm install --save graphql -` + throw new Error( + `Unable to load "graphql" package. Please make sure to install "graphql" as a dependency! \n + To install "graphql", run: + yarn add graphql + Or, with NPM: + npm install --save graphql` ); } } diff --git a/packages/graphql-codegen-cli/src/codegen.ts b/packages/graphql-codegen-cli/src/codegen.ts index 65f4b6f3a46..6905ae43e3f 100644 --- a/packages/graphql-codegen-cli/src/codegen.ts +++ b/packages/graphql-codegen-cli/src/codegen.ts @@ -1,12 +1,10 @@ import { - DetailedError, Types, CodegenPlugin, normalizeOutputParam, normalizeInstanceOrArray, normalizeConfig, getCachedDocumentNodeFromSchema, - isDetailedError, } from '@graphql-codegen/plugin-helpers'; import { codegen } from '@graphql-codegen/core'; @@ -51,7 +49,7 @@ const makeDefaultLoader = (from: string) => { }; }; -type Ctx = { errors: DetailedError[] | Error[] }; +type Ctx = { errors: Error[] }; function createCache(): (namespace: string, key: string, factory: () => Promise) => Promise { const cache = new Map>(); @@ -123,9 +121,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom const generateKeys = Object.keys(config.generates || {}); if (generateKeys.length === 0) { - throw new DetailedError( - 'Invalid Codegen Configuration!', - ` + throw new Error( + `Invalid Codegen Configuration! \n Please make sure that your codegen config file contains the "generates" field, with a specification for the plugins you need. It should looks like that: @@ -136,8 +133,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom my-file.ts: - plugin1 - plugin2 - - plugin3 - ` + - plugin3` ); } @@ -145,9 +141,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom const output = (generates[filename] = normalizeOutputParam(config.generates[filename])); if (!output.preset && (!output.plugins || output.plugins.length === 0)) { - throw new DetailedError( - 'Invalid Codegen Configuration!', - ` + throw new Error( + `Invalid Codegen Configuration! \n Please make sure that your codegen config file has defined plugins list for output "${filename}". It should looks like that: @@ -173,9 +168,8 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom (generates[filename].schema as unknown as any[]).length === 0) ) ) { - throw new DetailedError( - 'Invalid Codegen Configuration!', - ` + throw new Error( + `Invalid Codegen Configuration! \n Please make sure that your codegen config file contains either the "schema" field or every generated file has its own "schema" field. @@ -419,11 +413,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom } if (executedContext.errors.length > 0) { - const errors = executedContext.errors.map(subErr => - isDetailedError(subErr) - ? `${subErr.message} for "${subErr.source}"${subErr.details}` - : subErr.message || subErr.toString() - ); + const errors = executedContext.errors.map(subErr => subErr.message || subErr.toString()); const newErr = new AggregateError(executedContext.errors, `${errors.join('\n\n')}`); // Best-effort to all stack traces for debugging newErr.stack = `${newErr.stack}\n\n${executedContext.errors.map(subErr => subErr.stack).join('\n\n')}`; diff --git a/packages/graphql-codegen-cli/src/config.ts b/packages/graphql-codegen-cli/src/config.ts index 0657e075c4e..80f5327b2fe 100644 --- a/packages/graphql-codegen-cli/src/config.ts +++ b/packages/graphql-codegen-cli/src/config.ts @@ -2,7 +2,6 @@ import { cosmiconfig, defaultLoaders } from 'cosmiconfig'; import { TypeScriptLoader } from 'cosmiconfig-typescript-loader'; import { resolve } from 'path'; import { - DetailedError, Types, Profiler, createProfiler, @@ -156,8 +155,7 @@ export async function loadContext(configFilePath?: string): Promise 0; diff --git a/packages/graphql-codegen-cli/tests/codegen.spec.ts b/packages/graphql-codegen-cli/tests/codegen.spec.ts index 5277755eeee..c09420af63f 100644 --- a/packages/graphql-codegen-cli/tests/codegen.spec.ts +++ b/packages/graphql-codegen-cli/tests/codegen.spec.ts @@ -112,7 +112,7 @@ describe('Codegen Executor', () => { throw new Error(SHOULD_NOT_THROW_STRING); } catch (e) { expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING); - expect(e.message).toBe('Invalid Codegen Configuration!'); + expect(e.message).toMatch('Invalid Codegen Configuration!'); } }); @@ -143,7 +143,7 @@ describe('Codegen Executor', () => { expect(output.length).toBe(1); } catch (e) { expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING); - expect(e.message).not.toBe('Invalid Codegen Configuration!'); + expect(e.message).not.toMatch('Invalid Codegen Configuration!'); } }); @@ -159,7 +159,7 @@ describe('Codegen Executor', () => { throw new Error(SHOULD_NOT_THROW_STRING); } catch (e) { - expect(e.message).toBe('Invalid Codegen Configuration!'); + expect(e.message).toMatch('Invalid Codegen Configuration!'); expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING); } }); @@ -177,7 +177,7 @@ describe('Codegen Executor', () => { throw new Error(SHOULD_NOT_THROW_STRING); } catch (e) { expect(e.message).not.toBe(SHOULD_NOT_THROW_STRING); - expect(e.message).toBe('Invalid Codegen Configuration!'); + expect(e.message).toMatch('Invalid Codegen Configuration!'); } }); diff --git a/packages/graphql-codegen-core/src/codegen.ts b/packages/graphql-codegen-core/src/codegen.ts index cc56c23217d..96566d63eb3 100644 --- a/packages/graphql-codegen-core/src/codegen.ts +++ b/packages/graphql-codegen-core/src/codegen.ts @@ -1,5 +1,4 @@ import { - DetailedError, Types, isComplexPluginOutput, federationSpec, @@ -283,10 +282,8 @@ function validateDuplicateDocuments(files: Types.DocumentFile[]) { .join(''); const definitionKindName = kind.replace('Definition', '').toLowerCase(); - throw new DetailedError( - `Not all ${definitionKindName}s have an unique name: ${duplicated.join(', ')}`, - ` - Not all ${definitionKindName}s have an unique name + throw new Error( + `Not all ${definitionKindName}s have an unique name: ${duplicated.join(', ')}: \n ${list} ` ); diff --git a/packages/graphql-codegen-core/src/execute-plugin.ts b/packages/graphql-codegen-core/src/execute-plugin.ts index 020cb967966..d3b8281b02a 100644 --- a/packages/graphql-codegen-core/src/execute-plugin.ts +++ b/packages/graphql-codegen-core/src/execute-plugin.ts @@ -1,4 +1,4 @@ -import { DetailedError, Types, CodegenPlugin, Profiler, createNoopProfiler } from '@graphql-codegen/plugin-helpers'; +import { Types, CodegenPlugin, Profiler, createNoopProfiler } from '@graphql-codegen/plugin-helpers'; import { DocumentNode, GraphQLSchema, buildASTSchema } from 'graphql'; export interface ExecutePluginOptions { @@ -17,9 +17,8 @@ export interface ExecutePluginOptions { export async function executePlugin(options: ExecutePluginOptions, plugin: CodegenPlugin): Promise { if (!plugin || !plugin.plugin || typeof plugin.plugin !== 'function') { - throw new DetailedError( - `Invalid Custom Plugin "${options.name}"`, - ` + throw new Error( + `Invalid Custom Plugin "${options.name}" \n Plugin ${options.name} does not export a valid JS object with "plugin" function. Make sure your custom plugin is written in the following form: @@ -54,9 +53,8 @@ export async function executePlugin(options: ExecutePluginOptions, plugin: Codeg `Plugin ${options.name} validate` ); } catch (e) { - throw new DetailedError( - `Plugin "${options.name}" validation failed:`, - ` + throw new Error( + `Plugin "${options.name}" validation failed: \n ${e.message} ` ); diff --git a/packages/plugins/dart/flutter-freezed/package.json b/packages/plugins/dart/flutter-freezed/package.json index 8978d1876b7..03b7cbac22a 100644 --- a/packages/plugins/dart/flutter-freezed/package.json +++ b/packages/plugins/dart/flutter-freezed/package.json @@ -15,7 +15,7 @@ "dependencies": { "@graphql-codegen/plugin-helpers": "^2.6.0", "@graphql-codegen/schema-ast": "^2.5.0", - "@graphql-codegen/visitor-plugin-common": "2.12.0", + "@graphql-codegen/visitor-plugin-common": "2.13.0", "auto-bind": "~4.0.0", "tslib": "~2.4.0", "change-case-all": "1.0.14" diff --git a/packages/plugins/other/visitor-plugin-common/src/enum-values.ts b/packages/plugins/other/visitor-plugin-common/src/enum-values.ts index 57c7bfcd348..08411540952 100644 --- a/packages/plugins/other/visitor-plugin-common/src/enum-values.ts +++ b/packages/plugins/other/visitor-plugin-common/src/enum-values.ts @@ -1,6 +1,5 @@ import { EnumValuesMap, ParsedEnumValuesMap } from './types.js'; import { GraphQLSchema, isEnumType, GraphQLEnumType } from 'graphql'; -import { DetailedError } from '@graphql-codegen/plugin-helpers'; import { parseMapper } from './mappers.js'; function escapeString(str: string) { @@ -37,9 +36,9 @@ export function parseEnumValues({ const invalidMappings = Object.keys(mapOrStr).filter(gqlName => !allEnums.includes(gqlName)); if (invalidMappings.length > 0) { - throw new DetailedError( - `Invalid 'enumValues' mapping!`, - `The following types does not exist in your GraphQL schema: ${invalidMappings.join(', ')}` + throw new Error( + `Invalid 'enumValues' mapping! \n + The following types does not exist in your GraphQL schema: ${invalidMappings.join(', ')}` ); } @@ -74,9 +73,9 @@ export function parseEnumValues({ }, }; } - throw new DetailedError( - `Invalid "enumValues" configuration`, - `Enum "${gqlIdentifier}": expected string or object (with enum values mapping)` + throw new Error( + `Invalid "enumValues" configuration \n + Enum "${gqlIdentifier}": expected string or object (with enum values mapping)` ); }, {} as ParsedEnumValuesMap); } diff --git a/packages/presets/near-operation-file/src/resolve-document-imports.ts b/packages/presets/near-operation-file/src/resolve-document-imports.ts index 5fd46f5ab93..05b9a512296 100644 --- a/packages/presets/near-operation-file/src/resolve-document-imports.ts +++ b/packages/presets/near-operation-file/src/resolve-document-imports.ts @@ -1,4 +1,4 @@ -import { isUsingTypes, Types, DetailedError } from '@graphql-codegen/plugin-helpers'; +import { isUsingTypes, Types } from '@graphql-codegen/plugin-helpers'; import { generateImportStatement, ImportSource, @@ -88,13 +88,10 @@ export function resolveDocumentImports( externalFragments, }; } catch (e) { - throw new DetailedError( - `Unable to validate GraphQL document!`, - ` - File ${documentFile.location} caused error: - ${e.message || e.toString()} - `, - documentFile.location + throw new Error( + `Unable to validate GraphQL document! \n + File ${documentFile.location} caused error: + ${e.message || e.toString()}` ); } }); diff --git a/packages/utils/plugins-helpers/src/errors.ts b/packages/utils/plugins-helpers/src/errors.ts deleted file mode 100644 index c052591e5c9..00000000000 --- a/packages/utils/plugins-helpers/src/errors.ts +++ /dev/null @@ -1,11 +0,0 @@ -export class DetailedError extends Error { - constructor(public message: string, public details: string, public source?: string) { - super(message); - Object.setPrototypeOf(this, DetailedError.prototype); - Error.captureStackTrace(this, DetailedError); - } -} - -export function isDetailedError(error: any): error is DetailedError { - return error.details; -} diff --git a/packages/utils/plugins-helpers/src/index.ts b/packages/utils/plugins-helpers/src/index.ts index 633d90d7dba..223d0eccbe3 100644 --- a/packages/utils/plugins-helpers/src/index.ts +++ b/packages/utils/plugins-helpers/src/index.ts @@ -3,7 +3,6 @@ export * from './types.js'; export * from './utils.js'; export * from './helpers.js'; export * from './federation.js'; -export * from './errors.js'; export * from './getCachedDocumentNodeFromSchema.js'; export * from './oldVisit.js'; export * from './profiler.js'; diff --git a/patches/listr2+4.0.5.patch b/patches/listr2+4.0.5.patch new file mode 100644 index 00000000000..0fb17569482 --- /dev/null +++ b/patches/listr2+4.0.5.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/listr2/dist/index.cjs b/node_modules/listr2/dist/index.cjs +index 97af302..8715bd9 100644 +--- a/node_modules/listr2/dist/index.cjs ++++ b/node_modules/listr2/dist/index.cjs +@@ -406,7 +406,7 @@ var _DefaultRenderer = class { + } + str = `${icon} ${str}`; + let parsedStr; +- let columns = process.stdout.columns ? process.stdout.columns : 80; ++ let columns = process.stdout.columns ? process.stdout.columns : process.platform === 'win32' ? 1000 : 80; + columns = columns - level * this.options.indentation - 2; + switch (this.options.formatOutput) { + case "truncate": diff --git a/yarn.lock b/yarn.lock index 6f6abc211ce..573abcf2649 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1672,22 +1672,6 @@ "@graphql-tools/load" "6.2.4" tslib "2.0.2" -"@graphql-codegen/visitor-plugin-common@2.12.0": - version "2.12.0" - resolved "https://registry.yarnpkg.com/@graphql-codegen/visitor-plugin-common/-/visitor-plugin-common-2.12.0.tgz#49b055c5c2c5c0890f2226ce9e84bb73dfd83801" - integrity sha512-ULhFgOY05U+dlPsXkPT2wSBaAZMtEuHPL5Q1u8xwBdHWHi/uT4L5zEcBx+If/f6UWrOw7ufjEM0L7XSupe1iCA== - dependencies: - "@graphql-codegen/plugin-helpers" "^2.6.0" - "@graphql-tools/optimize" "^1.3.0" - "@graphql-tools/relay-operation-optimizer" "^6.5.0" - "@graphql-tools/utils" "^8.8.0" - auto-bind "~4.0.0" - change-case-all "1.0.14" - dependency-graph "^0.11.0" - graphql-tag "^2.11.0" - parse-filepath "^1.0.2" - tslib "~2.4.0" - "@graphql-tools/apollo-engine-loader@7.3.13", "@graphql-tools/apollo-engine-loader@^7.3.6": version "7.3.13" resolved "https://registry.yarnpkg.com/@graphql-tools/apollo-engine-loader/-/apollo-engine-loader-7.3.13.tgz#090bf03a99a5aaf8580826125346cb7441b54d94"