From 743ba89dbe56b3856be73f70fe1196ac1bbb552d Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Wed, 20 Mar 2019 09:26:59 +0100 Subject: [PATCH 01/11] feat(apollo typescript): fileExtension CLI arg --- .../apollo-codegen-core/src/compiler/index.ts | 1 + .../src/codeGeneration.ts | 15 +++++++++++---- packages/apollo-codegen-typescript/src/helpers.ts | 4 ++++ packages/apollo/src/commands/client/codegen.ts | 2 +- packages/apollo/src/generate.ts | 9 ++++++++- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/apollo-codegen-core/src/compiler/index.ts b/packages/apollo-codegen-core/src/compiler/index.ts index 0a23f7b8a1..1625b377b0 100644 --- a/packages/apollo-codegen-core/src/compiler/index.ts +++ b/packages/apollo-codegen-core/src/compiler/index.ts @@ -42,6 +42,7 @@ export interface CompilerOptions { namespace?: string; generateOperationIds?: boolean; operationIdsPath?: string; + fileExtension?: string; } export interface CompilerContext { diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index ba4666f6d1..ae4285532a 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -26,6 +26,7 @@ import TypescriptGenerator, { TypescriptCompilerOptions } from "./language"; import Printer from "./printer"; +import { DEFAULT_FILE_EXTENSION } from "./helpers"; import { GraphQLType, isListType } from "graphql/type/definition"; import { GraphQLNonNull, @@ -84,6 +85,7 @@ function printGlobalImport( generator: TypescriptAPIGenerator, typesUsed: GraphQLType[], outputPath: string, + fileExtension: string, globalSourcePath: string ) { if (typesUsed.length > 0) { @@ -91,7 +93,7 @@ function printGlobalImport( path.dirname(outputPath), path.join( path.dirname(globalSourcePath), - path.basename(globalSourcePath, ".ts") + path.basename(globalSourcePath, fileExtension) ) ); @@ -118,7 +120,8 @@ export function generateSource(context: CompilerContext) { generatedFiles.push({ sourcePath: operation.filePath, - fileName: `${operation.operationName}.ts`, + fileName: `${operation.operationName}${context.options.fileExtension || + DEFAULT_FILE_EXTENSION}`, content: new TypescriptGeneratedFile(output) }); }); @@ -164,7 +167,8 @@ export function generateLocalSource( const operations = Object.values(context.operations).map(operation => ({ sourcePath: operation.filePath, - fileName: `${operation.operationName}.ts`, + fileName: `${operation.operationName}${context.options.fileExtension || + DEFAULT_FILE_EXTENSION}`, content: (options?: IGeneratedFileOptions) => { generator.fileHeader(); if (options && options.outputPath && options.globalSourcePath) { @@ -172,6 +176,7 @@ export function generateLocalSource( generator, generator.getGlobalTypesUsedForOperation(operation), options.outputPath, + context.options.fileExtension || DEFAULT_FILE_EXTENSION, options.globalSourcePath ); } @@ -183,7 +188,8 @@ export function generateLocalSource( const fragments = Object.values(context.fragments).map(fragment => ({ sourcePath: fragment.filePath, - fileName: `${fragment.fragmentName}.ts`, + fileName: `${fragment.fragmentName}${context.options.fileExtension || + DEFAULT_FILE_EXTENSION}`, content: (options?: IGeneratedFileOptions) => { generator.fileHeader(); if (options && options.outputPath && options.globalSourcePath) { @@ -191,6 +197,7 @@ export function generateLocalSource( generator, generator.getGlobalTypesUsedForFragment(fragment), options.outputPath, + context.options.fileExtension || DEFAULT_FILE_EXTENSION, options.globalSourcePath ); } diff --git a/packages/apollo-codegen-typescript/src/helpers.ts b/packages/apollo-codegen-typescript/src/helpers.ts index dea682708b..506948200c 100644 --- a/packages/apollo-codegen-typescript/src/helpers.ts +++ b/packages/apollo-codegen-typescript/src/helpers.ts @@ -14,6 +14,8 @@ import * as t from "@babel/types"; import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; +const DEFAULT_FILE_EXTENSION = ".ts"; + const builtInScalarMap = { [GraphQLString.name]: t.TSStringKeyword(), [GraphQLInt.name]: t.TSNumberKeyword(), @@ -71,3 +73,5 @@ export function createTypeFromGraphQLTypeFunction( return typeFromGraphQLType; } + +export { DEFAULT_FILE_EXTENSION }; diff --git a/packages/apollo/src/commands/client/codegen.ts b/packages/apollo/src/commands/client/codegen.ts index 5ebbb54919..8d015b52a6 100644 --- a/packages/apollo/src/commands/client/codegen.ts +++ b/packages/apollo/src/commands/client/codegen.ts @@ -90,7 +90,7 @@ export default class Generate extends ClientCommand { // typescript globalTypesFile: flags.string({ description: - 'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path.' + 'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file' }) }; diff --git a/packages/apollo/src/generate.ts b/packages/apollo/src/generate.ts index 144ee79fd9..f8f748e72e 100644 --- a/packages/apollo/src/generate.ts +++ b/packages/apollo/src/generate.ts @@ -25,6 +25,7 @@ import { generateSource as generateScalaSource } from "apollo-codegen-scala"; import { FlowCompilerOptions } from "../../apollo-codegen-flow/lib/language"; import { validateQueryDocument } from "apollo-language-server/lib/errors/validation"; +import { DEFAULT_FILE_EXTENSION as TYPESCRIPT_DEFAULT_FILE_EXTENSION } from "apollo-codegen-typescript/lib/helpers"; export type TargetType = | "json" @@ -38,6 +39,7 @@ export type GenerationOptions = CompilerOptions & LegacyCompilerOptions & FlowCompilerOptions & { globalTypesFile?: string; + fileExtension?: string; rootPath?: string; }; @@ -154,7 +156,12 @@ export default function generate( } const globalSourcePath = - options.globalTypesFile || path.join(outputPath, "globalTypes.ts"); + options.globalTypesFile || + path.join( + outputPath, + `globalTypes${options.fileExtension || + TYPESCRIPT_DEFAULT_FILE_EXTENSION}` + ); outFiles[globalSourcePath] = { output: generatedGlobalFile.fileContents }; From 47bbcc0afd7437aa110180158e8eced06465c3eb Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Thu, 28 Mar 2019 17:21:03 +0100 Subject: [PATCH 02/11] add flag description --- packages/apollo/src/commands/client/codegen.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/apollo/src/commands/client/codegen.ts b/packages/apollo/src/commands/client/codegen.ts index 8a2da892ed..b3eb92a516 100644 --- a/packages/apollo/src/commands/client/codegen.ts +++ b/packages/apollo/src/commands/client/codegen.ts @@ -91,6 +91,10 @@ export default class Generate extends ClientCommand { globalTypesFile: flags.string({ description: 'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file' + }), + fileExtension: flags.string({ + description: + 'By default, TypeScript will output ".ts" files. Set "fileExtension" to specify a different file extension' }) }; From 479f2139bf5886eed7ff6926e0e62263bfe52afa Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Mon, 8 Apr 2019 14:13:38 +0200 Subject: [PATCH 03/11] add failing test --- .../client/__tests__/generate.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/apollo/src/commands/client/__tests__/generate.test.ts b/packages/apollo/src/commands/client/__tests__/generate.test.ts index 28491c389f..d4ee0e307f 100644 --- a/packages/apollo/src/commands/client/__tests__/generate.test.ts +++ b/packages/apollo/src/commands/client/__tests__/generate.test.ts @@ -191,6 +191,26 @@ describe("client:codegen", () => { expect(fs.readFileSync("API.ts").toString()).toMatchSnapshot(); }); + test + .fs({ + "schema.json": fullSchemaJsonString, + "queryOne.graphql": simpleQuery.toString(), + "my.config.js": defaultConfig + }) + .command([ + "client:codegen", + "--config=my.config.js", + "--target=typescript", + "--fileExtension=.d.ts", + "outDirectory" + ]) + .it("writes types for typescript with a custom extension", () => { + const [filePath] = fs.readdirSync("./outDirectory"); + const file = fs.readFileSync(`./outDirectory/${filePath}`).toString(); + + expect(file).toMatchSnapshot(); + }); + test .fs({ "schema.json": fullSchemaJsonString, From fab2ae0e2a69828e868d718bea6d7dafbad7c308 Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Wed, 10 Apr 2019 10:43:18 +0200 Subject: [PATCH 04/11] fix passing flag --- packages/apollo/src/commands/client/codegen.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/apollo/src/commands/client/codegen.ts b/packages/apollo/src/commands/client/codegen.ts index b3eb92a516..1ec291f012 100644 --- a/packages/apollo/src/commands/client/codegen.ts +++ b/packages/apollo/src/commands/client/codegen.ts @@ -197,7 +197,8 @@ export default class Generate extends ClientCommand { flags.mergeInFieldsFromFragmentSpreads, useFlowExactObjects: flags.useFlowExactObjects, useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes, - globalTypesFile: flags.globalTypesFile + globalTypesFile: flags.globalTypesFile, + fileExtension: flags.fileExtension } ); }; From 85a87e7a6dd094560e9a8a78eb6aa865d294dd5e Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Wed, 10 Apr 2019 10:54:04 +0200 Subject: [PATCH 05/11] fix descriptions --- .../apollo/src/commands/client/__tests__/generate.test.ts | 3 ++- packages/apollo/src/commands/client/codegen.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/apollo/src/commands/client/__tests__/generate.test.ts b/packages/apollo/src/commands/client/__tests__/generate.test.ts index d4ee0e307f..247c6a064b 100644 --- a/packages/apollo/src/commands/client/__tests__/generate.test.ts +++ b/packages/apollo/src/commands/client/__tests__/generate.test.ts @@ -201,11 +201,12 @@ describe("client:codegen", () => { "client:codegen", "--config=my.config.js", "--target=typescript", - "--fileExtension=.d.ts", + "--fileExtension=d.ts", "outDirectory" ]) .it("writes types for typescript with a custom extension", () => { const [filePath] = fs.readdirSync("./outDirectory"); + expect(filePath.endsWith(".d.ts")).toBeTruthy(); const file = fs.readFileSync(`./outDirectory/${filePath}`).toString(); expect(file).toMatchSnapshot(); diff --git a/packages/apollo/src/commands/client/codegen.ts b/packages/apollo/src/commands/client/codegen.ts index 1ec291f012..9e00824689 100644 --- a/packages/apollo/src/commands/client/codegen.ts +++ b/packages/apollo/src/commands/client/codegen.ts @@ -90,11 +90,11 @@ export default class Generate extends ClientCommand { // typescript globalTypesFile: flags.string({ description: - 'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file' + 'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file, for example "d.ts" will output "globalTypes.d.ts"' }), fileExtension: flags.string({ description: - 'By default, TypeScript will output ".ts" files. Set "fileExtension" to specify a different file extension' + 'By default, TypeScript will output "ts" files. Set "fileExtension" to specify a different file extension, for example "d.ts"' }) }; From 3e3f5c4a8d22b1da26430ffffa6004070c795be3 Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Wed, 10 Apr 2019 10:57:03 +0200 Subject: [PATCH 06/11] adapt code and generate snapshots --- .../src/codeGeneration.ts | 8 ++-- .../apollo-codegen-typescript/src/helpers.ts | 2 +- .../__snapshots__/generate.test.ts.snap | 47 +++++++++++++++++++ packages/apollo/src/generate.ts | 2 +- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index 9d3201b7e0..0745c5cfc4 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -93,7 +93,7 @@ function printGlobalImport( path.dirname(outputPath), path.join( path.dirname(globalSourcePath), - path.basename(globalSourcePath, fileExtension) + path.basename(globalSourcePath, `.${fileExtension}`) ) ); @@ -120,7 +120,7 @@ export function generateSource(context: CompilerContext) { generatedFiles.push({ sourcePath: operation.filePath, - fileName: `${operation.operationName}${context.options.fileExtension || + fileName: `${operation.operationName}.${context.options.fileExtension || DEFAULT_FILE_EXTENSION}`, content: new TypescriptGeneratedFile(output) }); @@ -167,7 +167,7 @@ export function generateLocalSource( const operations = Object.values(context.operations).map(operation => ({ sourcePath: operation.filePath, - fileName: `${operation.operationName}${context.options.fileExtension || + fileName: `${operation.operationName}.${context.options.fileExtension || DEFAULT_FILE_EXTENSION}`, content: (options?: IGeneratedFileOptions) => { generator.fileHeader(); @@ -188,7 +188,7 @@ export function generateLocalSource( const fragments = Object.values(context.fragments).map(fragment => ({ sourcePath: fragment.filePath, - fileName: `${fragment.fragmentName}${context.options.fileExtension || + fileName: `${fragment.fragmentName}.${context.options.fileExtension || DEFAULT_FILE_EXTENSION}`, content: (options?: IGeneratedFileOptions) => { generator.fileHeader(); diff --git a/packages/apollo-codegen-typescript/src/helpers.ts b/packages/apollo-codegen-typescript/src/helpers.ts index 506948200c..1084bfba4c 100644 --- a/packages/apollo-codegen-typescript/src/helpers.ts +++ b/packages/apollo-codegen-typescript/src/helpers.ts @@ -14,7 +14,7 @@ import * as t from "@babel/types"; import { CompilerOptions } from "apollo-codegen-core/lib/compiler"; -const DEFAULT_FILE_EXTENSION = ".ts"; +const DEFAULT_FILE_EXTENSION = "ts"; const builtInScalarMap = { [GraphQLString.name]: t.TSStringKeyword(), diff --git a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap index dc212fa1da..60415281b8 100644 --- a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap +++ b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap @@ -421,3 +421,50 @@ export interface SimpleQuery { //============================================================== " `; + +exports[`client:codegen writes typescript types for query with client-side data when client schema in js file 2`] = ` +"/* tslint:disable */ +/* eslint-disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: SimpleQuery +// ==================================================== + +export interface SimpleQuery_complexLocalState { + __typename: \\"LocalType\\"; + someData: string; +} + +export interface SimpleQuery_serverSideField { + __typename: \\"ServerField\\"; + serverData: string; + addedLocalData: string; +} + +export interface SimpleQuery_localServerModel { + __typename: \\"ServerField\\"; + serverData: string; +} + +export interface SimpleQuery { + hello: string; + localState: string; + complexLocalState: SimpleQuery_complexLocalState; + serverSideField: SimpleQuery_serverSideField; + localServerModel: SimpleQuery_localServerModel; +} + +/* tslint:disable */ +/* eslint-disable */ +// This file was automatically generated and should not be edited. + +//============================================================== +// START Enums and Input Objects +//============================================================== + +//============================================================== +// END Enums and Input Objects +//============================================================== +" +`; diff --git a/packages/apollo/src/generate.ts b/packages/apollo/src/generate.ts index f8f748e72e..195f5d0b38 100644 --- a/packages/apollo/src/generate.ts +++ b/packages/apollo/src/generate.ts @@ -159,7 +159,7 @@ export default function generate( options.globalTypesFile || path.join( outputPath, - `globalTypes${options.fileExtension || + `globalTypes.${options.fileExtension || TYPESCRIPT_DEFAULT_FILE_EXTENSION}` ); outFiles[globalSourcePath] = { From e3e6449b6837243969a0fd9faa0eccd28d0ca5ad Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Mon, 13 May 2019 16:21:22 +0200 Subject: [PATCH 07/11] fix tests --- .../__tests__/__snapshots__/generate.test.ts.snap | 15 +++++++++++++++ .../commands/client/__tests__/generate.test.ts | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap index 60415281b8..aa72333b34 100644 --- a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap +++ b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap @@ -328,6 +328,21 @@ export interface SimpleQuery { " `; +exports[`client:codegen writes types for typescript with a custom extension 1`] = ` +"/* tslint:disable */ +/* eslint-disable */ +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: SimpleQuery +// ==================================================== + +export interface SimpleQuery { + hello: string; +} +" +`; + exports[`client:codegen writes typescript types for query with client-side data when client schema in graphql file 1`] = ` "/* tslint:disable */ /* eslint-disable */ diff --git a/packages/apollo/src/commands/client/__tests__/generate.test.ts b/packages/apollo/src/commands/client/__tests__/generate.test.ts index 247c6a064b..7126d4d77d 100644 --- a/packages/apollo/src/commands/client/__tests__/generate.test.ts +++ b/packages/apollo/src/commands/client/__tests__/generate.test.ts @@ -201,7 +201,8 @@ describe("client:codegen", () => { "client:codegen", "--config=my.config.js", "--target=typescript", - "--fileExtension=d.ts", + "--fileExtension=.d.ts", + "--outputFlat", "outDirectory" ]) .it("writes types for typescript with a custom extension", () => { From e873041f0acebf72786ec31bfd72b8bf842d5427 Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Mon, 13 May 2019 16:27:54 +0200 Subject: [PATCH 08/11] remove obsolete snapshot --- .../__snapshots__/generate.test.ts.snap | 47 ------------------- 1 file changed, 47 deletions(-) diff --git a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap index aa72333b34..c2f39db3c6 100644 --- a/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap +++ b/packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap @@ -436,50 +436,3 @@ export interface SimpleQuery { //============================================================== " `; - -exports[`client:codegen writes typescript types for query with client-side data when client schema in js file 2`] = ` -"/* tslint:disable */ -/* eslint-disable */ -// This file was automatically generated and should not be edited. - -// ==================================================== -// GraphQL query operation: SimpleQuery -// ==================================================== - -export interface SimpleQuery_complexLocalState { - __typename: \\"LocalType\\"; - someData: string; -} - -export interface SimpleQuery_serverSideField { - __typename: \\"ServerField\\"; - serverData: string; - addedLocalData: string; -} - -export interface SimpleQuery_localServerModel { - __typename: \\"ServerField\\"; - serverData: string; -} - -export interface SimpleQuery { - hello: string; - localState: string; - complexLocalState: SimpleQuery_complexLocalState; - serverSideField: SimpleQuery_serverSideField; - localServerModel: SimpleQuery_localServerModel; -} - -/* tslint:disable */ -/* eslint-disable */ -// This file was automatically generated and should not be edited. - -//============================================================== -// START Enums and Input Objects -//============================================================== - -//============================================================== -// END Enums and Input Objects -//============================================================== -" -`; From aafe9505b0468601c6a8b70018364847393a732c Mon Sep 17 00:00:00 2001 From: Adrien HARNAY Date: Mon, 5 Aug 2019 15:42:31 +0200 Subject: [PATCH 09/11] trigger ci From 9290178e22020c8e8940299291cb0642600686b7 Mon Sep 17 00:00:00 2001 From: Jake Dawkins Date: Thu, 15 Aug 2019 19:25:44 -0400 Subject: [PATCH 10/11] Update generate.test.ts --- .../apollo/src/commands/client/__tests__/generate.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/apollo/src/commands/client/__tests__/generate.test.ts b/packages/apollo/src/commands/client/__tests__/generate.test.ts index b6142bd089..5533c59464 100644 --- a/packages/apollo/src/commands/client/__tests__/generate.test.ts +++ b/packages/apollo/src/commands/client/__tests__/generate.test.ts @@ -201,15 +201,15 @@ describe("client:codegen", () => { "client:codegen", "--config=my.config.js", "--target=typescript", - "--fileExtension=.d.ts", + "--fileExtension=d.ts", "--outputFlat", "outDirectory" ]) .it("writes types for typescript with a custom extension", () => { - const [filePath] = fs.readdirSync("./outDirectory"); - expect(filePath.endsWith(".d.ts")).toBeTruthy(); + const files = fs.readdirSync("./outDirectory"); + const filePath = files.find(f => f === "SimpleQuery.d.ts"); + expect(filePath).toBeDefined(); const file = fs.readFileSync(`./outDirectory/${filePath}`).toString(); - expect(file).toMatchSnapshot(); }); From 5cebffd28f2550f9355939b5e28164068d41629d Mon Sep 17 00:00:00 2001 From: Jake Date: Mon, 19 Aug 2019 10:09:25 -0400 Subject: [PATCH 11/11] update flag name to tsFileExtension --- packages/apollo-codegen-core/src/compiler/index.ts | 4 +++- .../src/codeGeneration.ts | 14 +++++++------- .../src/commands/client/__tests__/generate.test.ts | 2 +- packages/apollo/src/commands/client/codegen.ts | 6 +++--- packages/apollo/src/generate.ts | 4 ++-- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/apollo-codegen-core/src/compiler/index.ts b/packages/apollo-codegen-core/src/compiler/index.ts index 9457474767..9821ce4c47 100644 --- a/packages/apollo-codegen-core/src/compiler/index.ts +++ b/packages/apollo-codegen-core/src/compiler/index.ts @@ -42,7 +42,9 @@ export interface CompilerOptions { namespace?: string; generateOperationIds?: boolean; operationIdsPath?: string; - fileExtension?: string; + // this option is only implemented in the ts codegen, so we name it + // `ts` fileExtension for now. + tsFileExtension?: string; useReadOnlyTypes?: boolean; } diff --git a/packages/apollo-codegen-typescript/src/codeGeneration.ts b/packages/apollo-codegen-typescript/src/codeGeneration.ts index 7732c4db38..033b2e1e09 100644 --- a/packages/apollo-codegen-typescript/src/codeGeneration.ts +++ b/packages/apollo-codegen-typescript/src/codeGeneration.ts @@ -83,7 +83,7 @@ function printGlobalImport( generator: TypescriptAPIGenerator, typesUsed: GraphQLType[], outputPath: string, - fileExtension: string, + tsFileExtension: string, globalSourcePath: string ) { if (typesUsed.length > 0) { @@ -91,7 +91,7 @@ function printGlobalImport( path.dirname(outputPath), path.join( path.dirname(globalSourcePath), - path.basename(globalSourcePath, `.${fileExtension}`) + path.basename(globalSourcePath, `.${tsFileExtension}`) ) ); @@ -118,7 +118,7 @@ export function generateSource(context: CompilerContext) { generatedFiles.push({ sourcePath: operation.filePath, - fileName: `${operation.operationName}.${context.options.fileExtension || + fileName: `${operation.operationName}.${context.options.tsFileExtension || DEFAULT_FILE_EXTENSION}`, content: new TypescriptGeneratedFile(output) }); @@ -165,7 +165,7 @@ export function generateLocalSource( const operations = Object.values(context.operations).map(operation => ({ sourcePath: operation.filePath, - fileName: `${operation.operationName}.${context.options.fileExtension || + fileName: `${operation.operationName}.${context.options.tsFileExtension || DEFAULT_FILE_EXTENSION}`, content: (options?: IGeneratedFileOptions) => { generator.fileHeader(); @@ -174,7 +174,7 @@ export function generateLocalSource( generator, generator.getGlobalTypesUsedForOperation(operation), options.outputPath, - context.options.fileExtension || DEFAULT_FILE_EXTENSION, + context.options.tsFileExtension || DEFAULT_FILE_EXTENSION, options.globalSourcePath ); } @@ -186,7 +186,7 @@ export function generateLocalSource( const fragments = Object.values(context.fragments).map(fragment => ({ sourcePath: fragment.filePath, - fileName: `${fragment.fragmentName}.${context.options.fileExtension || + fileName: `${fragment.fragmentName}.${context.options.tsFileExtension || DEFAULT_FILE_EXTENSION}`, content: (options?: IGeneratedFileOptions) => { generator.fileHeader(); @@ -195,7 +195,7 @@ export function generateLocalSource( generator, generator.getGlobalTypesUsedForFragment(fragment), options.outputPath, - context.options.fileExtension || DEFAULT_FILE_EXTENSION, + context.options.tsFileExtension || DEFAULT_FILE_EXTENSION, options.globalSourcePath ); } diff --git a/packages/apollo/src/commands/client/__tests__/generate.test.ts b/packages/apollo/src/commands/client/__tests__/generate.test.ts index 5533c59464..6185056b6b 100644 --- a/packages/apollo/src/commands/client/__tests__/generate.test.ts +++ b/packages/apollo/src/commands/client/__tests__/generate.test.ts @@ -201,7 +201,7 @@ describe("client:codegen", () => { "client:codegen", "--config=my.config.js", "--target=typescript", - "--fileExtension=d.ts", + "--tsFileExtension=d.ts", "--outputFlat", "outDirectory" ]) diff --git a/packages/apollo/src/commands/client/codegen.ts b/packages/apollo/src/commands/client/codegen.ts index 11a7496fc8..2f2137af52 100644 --- a/packages/apollo/src/commands/client/codegen.ts +++ b/packages/apollo/src/commands/client/codegen.ts @@ -98,9 +98,9 @@ export default class Generate extends ClientCommand { description: 'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file, for example "d.ts" will output "globalTypes.d.ts"' }), - fileExtension: flags.string({ + tsFileExtension: flags.string({ description: - 'By default, TypeScript will output "ts" files. Set "fileExtension" to specify a different file extension, for example "d.ts"' + 'By default, TypeScript will output "ts" files. Set "tsFileExtension" to specify a different file extension, for example "d.ts"' }) }; @@ -205,7 +205,7 @@ export default class Generate extends ClientCommand { useReadOnlyTypes: flags.useReadOnlyTypes || flags.useFlowReadOnlyTypes, globalTypesFile: flags.globalTypesFile, - fileExtension: flags.fileExtension + tsFileExtension: flags.tsFileExtension } ); }; diff --git a/packages/apollo/src/generate.ts b/packages/apollo/src/generate.ts index 6f89018587..62a6a96a9c 100644 --- a/packages/apollo/src/generate.ts +++ b/packages/apollo/src/generate.ts @@ -39,7 +39,7 @@ export type GenerationOptions = CompilerOptions & LegacyCompilerOptions & FlowCompilerOptions & { globalTypesFile?: string; - fileExtension?: string; + tsFileExtension?: string; rootPath?: string; }; @@ -159,7 +159,7 @@ export default function generate( options.globalTypesFile || path.join( outputPath, - `globalTypes.${options.fileExtension || + `globalTypes.${options.tsFileExtension || TYPESCRIPT_DEFAULT_FILE_EXTENSION}` ); outFiles[globalSourcePath] = {