Skip to content

Commit

Permalink
update CLI for declaration merging (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Dec 3, 2024
1 parent fc351a2 commit c5bfd6f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/commands/generate/__snapshots__/types.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
exports[`generate types correctly generates JS SDK types with custom data type 1`] = `
"import { DevCycleJSON } from '@devcycle/js-client-sdk'
declare module '@devcycle/types' {
interface CustomVariableDefinitions extends DVCVariableTypes {}
}
export type CustomData = {
'customString': string
'customEnum'?: |
Expand Down Expand Up @@ -130,6 +134,10 @@ export const DEPRECATED_VAR = 'deprecated-var' as const
exports[`generate types correctly generates JS SDK types with custom data type in strict mode 1`] = `
"import { DevCycleJSON } from '@devcycle/js-client-sdk'
declare module '@devcycle/types' {
interface CustomVariableDefinitions extends DVCVariableTypes {}
}
export type CustomData = {
'customString': string
'customEnum'?: |
Expand Down Expand Up @@ -257,6 +265,10 @@ export const DEPRECATED_VAR = 'deprecated-var' as const
exports[`generate types correctly generates JS SDK types with obfuscated keys 1`] = `
"import { DevCycleJSON } from '@devcycle/js-client-sdk'
declare module '@devcycle/types' {
interface CustomVariableDefinitions extends DVCVariableTypes {}
}
export type CustomData = {
}
Expand Down Expand Up @@ -333,6 +345,10 @@ exports[`generate types correctly generates Next.js SDK types 1`] = `
DevCycleJSON
} from '@devcycle/nextjs-sdk'
declare module '@devcycle/types' {
interface CustomVariableDefinitions extends DVCVariableTypes {}
}
export type CustomData = {
'customString': string
'customEnum'?: |
Expand Down Expand Up @@ -486,6 +502,10 @@ exports[`generate types correctly generates React SDK types 1`] = `
DevCycleJSON
} from '@devcycle/react-client-sdk'
declare module '@devcycle/types' {
interface CustomVariableDefinitions extends DVCVariableTypes {}
}
export type CustomData = {
'customString': string
'customEnum'?: |
Expand Down
28 changes: 28 additions & 0 deletions src/commands/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,23 @@ export default class GenerateTypes extends Base {
react: Flags.boolean({
description: 'Generate types for use with React',
default: false,
deprecated: {
message:
'The React SDK since v1.30.0 does not require this flag. Its types can be augmented automatically',
},
}),
nextjs: Flags.boolean({
description: 'Generate types for use with Next.js',
default: false,
deprecated: {
message:
'The Next.js SDK since v2.7.0 does not require this flag. Its types can be augmented automatically',
},
}),
'no-declaration': Flags.boolean({
description:
'Do not generate a "declare module" statement that automatically overrides SDK types.',
default: false,
}),
'old-repos': Flags.boolean({
description:
Expand Down Expand Up @@ -124,6 +137,7 @@ export default class GenerateTypes extends Base {
project: Project
outputDir: string
includeDeprecationWarnings = true
noDeclaration = false
features: Feature[] = []
customProperties: CustomProperty[]

Expand All @@ -135,6 +149,7 @@ export default class GenerateTypes extends Base {
'include-descriptions': includeDescriptions,
obfuscate,
'output-dir': outputDir,
'no-declaration': noDeclaration,
'include-deprecation-warnings': includeDeprecationWarnings,
} = flags
this.includeDescriptions = includeDescriptions
Expand All @@ -146,6 +161,7 @@ export default class GenerateTypes extends Base {
this.authToken,
this.projectKey,
)
this.noDeclaration = noDeclaration

if (this.project.settings.obfuscation.required) {
if (!this.obfuscate) {
Expand Down Expand Up @@ -230,6 +246,7 @@ export default class GenerateTypes extends Base {

let types =
imports +
this.generateDeclareModuleStatement() +
generateCustomDataType(this.customProperties, strictCustomData) +
(react || next ? reactOverrides : '') +
'export type DVCVariableTypes = {\n' +
Expand All @@ -240,6 +257,17 @@ export default class GenerateTypes extends Base {
return types
}

private generateDeclareModuleStatement() {
if (this.noDeclaration) {
return ''
}
return (
`declare module '@devcycle/types' {\n` +
` interface CustomVariableDefinitions extends DVCVariableTypes {}\n` +
`}\n\n`
)
}

private getTypeDefinitionLine(variable: Variable) {
if (this.obfuscate) {
return ` ${this.getVariableKeyAndType(variable)}`
Expand Down

0 comments on commit c5bfd6f

Please sign in to comment.