-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat: server logic for create from React component #24881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
astone123
merged 37 commits into
feature/create-from-react-component
from
astone123/create-from-react-server
Dec 5, 2022
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
6c24bfa
feat: WIP server logic for create from React component
astone123 2948dd7
feat: add more tests; error handling
astone123 73cb71f
feat: PR feedback [run CI]
astone123 0dbd568
Resolve conflicts
astone123 0f4e836
feat: try committing snapshot cache changes [run ci]
astone123 d763e1f
feat: try re-generating snapshot [run ci]
astone123 a2626df
fix build
ryanthemanuel 6eabeb3
regenerate cache on darwin
ryanthemanuel 6ba1695
update caches
ryanthemanuel 9389037
Revert "feat: try re-generating snapshot [run ci]"
astone123 d4e4c7d
Merge branch 'feature/create-from-react-component' into astone123/cre…
lmiller1990 a48491d
fix typing error
lmiller1990 00e7819
types
lmiller1990 df027ff
fix test
lmiller1990 b32560f
chore: try using [email protected]
lmiller1990 89b823a
update test
lmiller1990 2f3ffcb
regen linux snapshot
lmiller1990 a631284
update snapshots for darwin
lmiller1990 dbb5b3e
re-gen linux snapshot
lmiller1990 4ad474c
Merge branch 'astone123/create-from-react-server' of https://github.c…
lmiller1990 c0382fa
yarn install
lmiller1990 7456bb7
update snapshots
lmiller1990 fadb8d9
update snapshot metadata
lmiller1990 6d502b1
update snapshots due to babel deps changing slightly
lmiller1990 f60ac0a
make react docgen a dep
lmiller1990 bd398ed
update tests
lmiller1990 9510a85
revert
lmiller1990 77a4942
snapshots again??
lmiller1990 f9e8af7
revert
lmiller1990 68d5c87
update
lmiller1990 281da0a
update
lmiller1990 e4410b1
try change snapshot
lmiller1990 f12efca
change snap
lmiller1990 e857b8f
update snap
lmiller1990 c070f96
feat: remove unnecessary ts-ignore
astone123 cd65233
feat: add more test cases
astone123 9449d66
feat: create CodegenActions; other minor refactors
astone123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
1 change: 0 additions & 1 deletion
1
packages/config/test/__babel_fixtures__/adding-component/destructure-require-ts/output.ts
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,141 @@ | ||
import type { NexusGenObjects, NexusGenUnions } from '@packages/graphql/src/gen/nxs.gen' | ||
import assert from 'assert' | ||
import path from 'path' | ||
import type { DataContext } from '..' | ||
import { SpecOptions, codeGenerator } from '../codegen' | ||
import templates from '../codegen/templates' | ||
import type { CodeGenType } from '../gen/graphcache-config.gen' | ||
import { WizardFrontendFramework, WIZARD_FRAMEWORKS } from '@packages/scaffold-config' | ||
import { parse as parseReactComponent, resolver as reactDocgenResolvers } from 'react-docgen' | ||
|
||
interface ReactComponentDescriptor { | ||
displayName: string | ||
} | ||
|
||
export class CodegenActions { | ||
constructor (private ctx: DataContext) {} | ||
|
||
async getReactComponentsFromFile (filePath: string): Promise<ReactComponentDescriptor[]> { | ||
try { | ||
const src = await this.ctx.fs.readFile(filePath, 'utf8') | ||
|
||
const result = parseReactComponent(src, reactDocgenResolvers.findAllExportedComponentDefinitions) | ||
// types appear to be incorrect in [email protected] | ||
// TODO: update when 6.0.0 stable is out for fixed types. | ||
const defs = (Array.isArray(result) ? result : [result]) as ReactComponentDescriptor[] | ||
|
||
return defs | ||
} catch (err) { | ||
this.ctx.debug(err) | ||
|
||
return [] | ||
} | ||
} | ||
|
||
async codeGenSpec (codeGenCandidate: string, codeGenType: CodeGenType, componentName?: string): Promise<NexusGenUnions['GeneratedSpecResult']> { | ||
const project = this.ctx.currentProject | ||
|
||
assert(project, 'Cannot create spec without currentProject.') | ||
|
||
const getCodeGenPath = () => { | ||
return codeGenType === 'e2e' | ||
? this.ctx.path.join( | ||
project, | ||
codeGenCandidate, | ||
) | ||
: codeGenCandidate | ||
} | ||
|
||
const codeGenPath = getCodeGenPath() | ||
|
||
const { specPattern = [] } = await this.ctx.project.specPatterns() | ||
|
||
const newSpecCodeGenOptions = new SpecOptions({ | ||
codeGenPath, | ||
codeGenType, | ||
framework: this.getWizardFrameworkFromConfig(), | ||
isDefaultSpecPattern: await this.ctx.project.getIsDefaultSpecPattern(), | ||
specPattern, | ||
currentProject: this.ctx.currentProject, | ||
specs: this.ctx.project.specs, | ||
componentName, | ||
}) | ||
|
||
let codeGenOptions = await newSpecCodeGenOptions.getCodeGenOptions() | ||
|
||
const codeGenResults = await codeGenerator( | ||
{ templateDir: templates[codeGenOptions.templateKey], target: codeGenOptions.overrideCodeGenDir || path.parse(codeGenPath).dir }, | ||
codeGenOptions, | ||
) | ||
|
||
if (!codeGenResults.files[0] || codeGenResults.failed[0]) { | ||
throw (codeGenResults.failed[0] || 'Unable to generate spec') | ||
} | ||
|
||
const [newSpec] = codeGenResults.files | ||
|
||
const cfg = await this.ctx.project.getConfig() | ||
|
||
if (cfg && this.ctx.currentProject) { | ||
const testingType = (codeGenType === 'component') ? 'component' : 'e2e' | ||
|
||
await this.ctx.actions.project.setSpecsFoundBySpecPattern({ | ||
projectRoot: this.ctx.currentProject, | ||
testingType, | ||
specPattern: cfg.specPattern ?? [], | ||
configSpecPattern: cfg.specPattern ?? [], | ||
excludeSpecPattern: cfg.excludeSpecPattern, | ||
additionalIgnorePattern: cfg.additionalIgnorePattern, | ||
}) | ||
} | ||
|
||
return { | ||
status: 'valid', | ||
file: { absolute: newSpec.file, contents: newSpec.content }, | ||
description: 'Generated spec', | ||
} | ||
} | ||
|
||
get defaultE2EPath () { | ||
const projectRoot = this.ctx.currentProject | ||
|
||
assert(projectRoot, `Cannot create e2e directory without currentProject.`) | ||
|
||
return path.join(projectRoot, 'cypress', 'e2e') | ||
} | ||
|
||
async scaffoldIntegration (): Promise<NexusGenObjects['ScaffoldedFile'][]> { | ||
const projectRoot = this.ctx.currentProject | ||
|
||
assert(projectRoot, `Cannot create spec without currentProject.`) | ||
|
||
const results = await codeGenerator( | ||
{ templateDir: templates['scaffoldIntegration'], target: this.defaultE2EPath }, | ||
{}, | ||
) | ||
|
||
if (results.failed.length) { | ||
throw new Error(`Failed generating files: ${results.failed.map((e) => `${e}`)}`) | ||
} | ||
|
||
return results.files.map(({ status, file, content }) => { | ||
return { | ||
status: (status === 'add' || status === 'overwrite') ? 'valid' : 'skipped', | ||
file: { absolute: file, contents: content }, | ||
description: 'Generated spec', | ||
} | ||
}) | ||
} | ||
|
||
getWizardFrameworkFromConfig (): WizardFrontendFramework | undefined { | ||
const config = this.ctx.lifecycleManager.loadedConfigFile | ||
|
||
// If devServer is a function, they are using a custom dev server. | ||
if (!config?.component?.devServer || typeof config?.component?.devServer === 'function') { | ||
return undefined | ||
} | ||
|
||
// @ts-ignore - because of the conditional above, we know that devServer isn't a function | ||
return WIZARD_FRAMEWORKS.find((framework) => framework.configFramework === config?.component?.devServer.framework) | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.