From 0087d3a87b13fffd9ad50e8d66fa98f38ade38d9 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Tue, 15 Aug 2023 22:13:28 +0200 Subject: [PATCH] [Tool][Codegen] Add script to post format the generated graphql file Makes it possible to modify the generated graphql file by codegen. --- package.json | 4 +++- tools/scripts/codegenFormatter.ts | 33 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tools/scripts/codegenFormatter.ts diff --git a/package.json b/package.json index db611967..050cf70d 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ "lint": "eslint src --ext .ts,.tsx,.js,.jsx", "createChangelog": "ts-node tools/scripts/createReleaseChanglog.ts", "updateDeps": "yarn outdated && yarn upgrade && yarn syncyarnlock -s -k && yarn && git add package.json yarn.lock && git commit -m \"Update dependencies\"", - "gql:codegen": "graphql-codegen --config gql_codegen.ts" + "gql:codegen-base": "graphql-codegen --config gql_codegen.ts", + "gql:codegen-formatter": "ts-node tools/scripts/codegenFormatter.ts", + "gql:codegen": "yarn gql:codegen-base & yarn gql:codegen-formatter" }, "browserslist": { "production": [ diff --git a/tools/scripts/codegenFormatter.ts b/tools/scripts/codegenFormatter.ts new file mode 100644 index 00000000..8f7722e7 --- /dev/null +++ b/tools/scripts/codegenFormatter.ts @@ -0,0 +1,33 @@ +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +import fs from 'fs'; +import * as path from 'path'; + +const format = (source: string, regex: RegExp, replaceValue: string): string => source.replace(regex, replaceValue); + +const generatedGraphQLFilePath = path.resolve(__dirname, '../../src/lib/graphql/generated/graphql.ts'); +const generatedGraphQLFile = fs.readFileSync(generatedGraphQLFilePath, 'utf8'); + +// add logic to format the codegen generated graphql file + +const fixCursorTyping = format( + generatedGraphQLFile, + /Cursor: \{ input: any; output: any; }/g, + 'Cursor: { input: string; output: string; }', +); + +const fixLongStringTyping = format( + fixCursorTyping, + /LongString: \{ input: any; output: any; }/g, + 'LongString: { input: string; output: string; }', +); + +const fixSubscriptionHookNameSuffix = format(fixLongStringTyping, /SubscriptionSubscription/g, 'Subscription'); + +fs.writeFileSync(generatedGraphQLFilePath, fixSubscriptionHookNameSuffix);