-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b32c59
commit de5544c
Showing
7 changed files
with
197 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { log, note, outro, spinner } from '@clack/prompts'; | ||
import path from 'path'; | ||
import pc from 'picocolors'; | ||
import { ClassDeclaration, StructureKind, SyntaxKind } from 'ts-morph'; | ||
|
||
import { selectMultiplePluginClasses } from '../../../shared/shared-prompts'; | ||
import { createFile, getRelativeImportPath, getTsMorphProject } from '../../../utilities/ast-utils'; | ||
import { addNpmScriptToPackageJson, installRequiredPackages } from '../../../utilities/package-utils'; | ||
|
||
export async function addCodegen(providedPluginClass?: ClassDeclaration) { | ||
let pluginClasses = providedPluginClass ? [providedPluginClass] : []; | ||
let project = providedPluginClass?.getProject(); | ||
if (!pluginClasses.length || !project) { | ||
const projectSpinner = spinner(); | ||
projectSpinner.start('Analyzing project...'); | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
project = getTsMorphProject(); | ||
projectSpinner.stop('Project analyzed'); | ||
pluginClasses = await selectMultiplePluginClasses(project, 'Add codegen cancelled'); | ||
} | ||
|
||
const installSpinner = spinner(); | ||
installSpinner.start(`Installing dependencies...`); | ||
try { | ||
await installRequiredPackages(project, [ | ||
{ | ||
pkg: '@graphql-codegen/cli', | ||
isDevDependency: true, | ||
}, | ||
{ | ||
pkg: '@graphql-codegen/typescript', | ||
isDevDependency: true, | ||
}, | ||
]); | ||
} catch (e: any) { | ||
log.error(`Failed to install dependencies: ${e.message as string}.`); | ||
} | ||
installSpinner.stop('Dependencies installed'); | ||
|
||
const configSpinner = spinner(); | ||
configSpinner.start('Configuring codegen file...'); | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
||
const tempProject = getTsMorphProject({ skipAddingFilesFromTsConfig: true }); | ||
const codegenFile = createFile(tempProject, path.join(__dirname, 'templates/codegen.template.ts')); | ||
const codegenConfig = codegenFile | ||
.getVariableDeclaration('config') | ||
?.getChildrenOfKind(SyntaxKind.ObjectLiteralExpression)[0]; | ||
if (!codegenConfig) { | ||
throw new Error('Could not find the config variable in the template codegen file'); | ||
} | ||
const generatesProp = codegenConfig | ||
.getProperty('generates') | ||
?.getFirstChildByKind(SyntaxKind.ObjectLiteralExpression); | ||
if (!generatesProp) { | ||
throw new Error('Could not find the generates property in the template codegen file'); | ||
} | ||
const rootDir = tempProject.getDirectory('.'); | ||
if (!rootDir) { | ||
throw new Error('Could not find the root directory of the project'); | ||
} | ||
for (const pluginClass of pluginClasses) { | ||
const relativePluginPath = getRelativeImportPath({ | ||
from: pluginClass.getSourceFile(), | ||
to: rootDir, | ||
}); | ||
const generatedTypesPath = `${path.dirname(relativePluginPath)}/gql/generated.ts`; | ||
generatesProp | ||
.addProperty({ | ||
name: `'${generatedTypesPath}'`, | ||
kind: StructureKind.PropertyAssignment, | ||
initializer: `{ plugins: ['typescript'] }`, | ||
}) | ||
.formatText(); | ||
} | ||
codegenFile.move(path.join(rootDir.getPath(), 'codegen.ts')); | ||
|
||
addNpmScriptToPackageJson(tempProject, 'codegen', 'graphql-codegen --config codegen.ts'); | ||
|
||
configSpinner.stop('Configured codegen file'); | ||
|
||
project.saveSync(); | ||
|
||
const nextSteps = [ | ||
`You can run codegen by doing the following:`, | ||
`1. Ensure your dev server is running`, | ||
`2. Run "npm run codegen"`, | ||
]; | ||
note(nextSteps.join('\n')); | ||
|
||
if (!providedPluginClass) { | ||
outro('✅ Codegen setup complete!'); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/cli/src/commands/add/codegen/templates/codegen.template.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { CodegenConfig } from '@graphql-codegen/cli'; | ||
|
||
const config: CodegenConfig = { | ||
overwrite: true, | ||
// This assumes your server is running on the standard port | ||
// and with the default admin API path. Adjust accordingly. | ||
schema: 'http://localhost:3000/admin-api', | ||
config: { | ||
// This tells codegen that the `Money` scalar is a number | ||
scalars: { Money: 'number' }, | ||
// This ensures generated enums do not conflict with the built-in types. | ||
namingConvention: { enumValues: 'keep' }, | ||
}, | ||
generates: {}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters