Skip to content

Commit 8d35942

Browse files
committed
feat: integrated types-generator NPM package & removed old test files
1 parent b7510c0 commit 8d35942

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+72
-4200
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dependencies": {
88
"@contentstack/cli-command": "^1.3.1",
99
"@contentstack/cli-utilities": "^1.7.3",
10-
"@gql2ts/from-schema": "^2.0.0-4",
10+
"@contentstack/types-generator": "^2.0.2",
1111
"async": "^3.2.6",
1212
"fancy-test": "^3.0.16",
1313
"graphql": "^14.7.0",

src/commands/tsgen.ts

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import {Command} from '@contentstack/cli-command'
22
import {flags} from '@contentstack/cli-utilities'
3-
import {getGlobalFields, stackConnect, StackConnectionConfig, generateGraphQLTypeDef} from '../lib/stack/client'
4-
import {ContentType} from '../lib/stack/schema'
5-
import tsgenRunner from '../lib/tsgen/runner'
3+
import * as path from 'path'
4+
import * as fs from 'fs'
5+
import {sanitizePath} from '../lib/helper'
6+
import {generateTS, graphqlTS} from '@contentstack/types-generator'
7+
import {StackConnectionConfig} from '../types'
8+
9+
function createOutputPath(outputFile: string) {
10+
const outputPath = path.resolve(sanitizePath(process.cwd()), sanitizePath(outputFile))
11+
const dirName = path.dirname(outputPath)
12+
13+
fs.mkdirSync(dirName, {recursive: true})
14+
15+
return outputPath
16+
}
617

718
export default class TypeScriptCodeGeneratorCommand extends Command {
8-
static description = 'generate TypeScript typings from a Stack';
19+
static description = 'Generate TypeScript typings from a Stack';
920

1021
static examples = [
1122
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"',
@@ -78,11 +89,13 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
7889
const token = this.getToken(flags['token-alias'])
7990
const prefix = flags.prefix
8091
const includeDocumentation = flags.doc
81-
const outputPath = flags.output
92+
const filePath = flags.output
8293
const branch = flags.branch
8394
const includeSystemFields = flags['include-system-fields']
8495
const namespace = flags.namespace
8596

97+
const outputPath = createOutputPath(filePath)
98+
8699
if (token.type !== 'delivery') {
87100
this.warn('Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.')
88101
}
@@ -91,38 +104,61 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
91104
this.error('Please provide an output path.', {exit: 2})
92105
}
93106

107+
let region = ''
108+
switch (this.region.name) {
109+
case 'NA':
110+
region = 'US'
111+
break
112+
case 'AZURE-NA':
113+
region = 'AZURE_NA'
114+
break
115+
case 'AZURE-EU':
116+
region = 'AZURE_EU'
117+
break
118+
case 'GCP-NA':
119+
region = 'GCP_NA'
120+
break
121+
default:
122+
region = this.region.name.toUpperCase()
123+
}
124+
94125
const config: StackConnectionConfig = {
95126
apiKey: token.apiKey,
96127
token: token.token,
97-
region: (this.region.name === 'NA') ? 'us' : this.region.name.toLowerCase(),
128+
region: region,
98129
environment: token.environment || '',
99-
branch: branch || null,
130+
branch: branch || undefined,
100131
}
101132

133+
// Generate the GraphQL schema TypeScript definitions
102134
if (flags['api-type'] === 'graphql') {
103-
const result = await generateGraphQLTypeDef(config, outputPath, namespace)
104-
if (result) {
105-
this.log(`Successfully added the GraphQL schema type definitions to '${result.outputPath}'.`)
106-
} else {
107-
this.log('No schema found in the stack! Please use a valid stack.')
135+
try {
136+
const result = await graphqlTS({...config, namespace: namespace})
137+
138+
fs.writeFileSync(outputPath, result)
139+
this.log(`Successfully added the GraphQL schema type definitions to '${outputPath}'.`)
140+
} catch (error: any) {
141+
this.error(error.error_message, {exit: 1})
108142
}
109143
} else {
110-
const [client, globalFields] = await Promise.all([stackConnect(this.deliveryAPIClient.Stack, config, this.cdaHost), getGlobalFields(config, this.cdaHost)])
111-
112-
let schemas: ContentType[] = []
113-
if (client.types?.length) {
114-
if ((globalFields as any)?.global_fields?.length) {
115-
schemas = schemas.concat((globalFields as any).global_fields as ContentType)
116-
schemas = schemas.map(schema => ({
117-
...schema,
118-
schema_type: 'global_field',
119-
}))
120-
}
121-
schemas = schemas.concat(client.types)
122-
const result = await tsgenRunner(outputPath, schemas, prefix, includeDocumentation, includeSystemFields)
123-
this.log(`Wrote ${result.definitions} Content Types to '${result.outputPath}'.`)
124-
} else {
125-
this.log('No Content Types exist in the Stack.')
144+
// Generate the Content Types TypeScript definitions
145+
try {
146+
const result = await generateTS({
147+
...config,
148+
tokenType: 'delivery',
149+
includeDocumentation: includeDocumentation,
150+
prefix,
151+
systemFields: includeSystemFields,
152+
})
153+
154+
fs.writeFileSync(outputPath, result)
155+
156+
// -- TODO : Add count support for the number of Content Types generated
157+
this.log(`Successfully added the Content Types to '${outputPath}'.`)
158+
159+
// this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
160+
} catch (error: any) {
161+
this.error(error.error_message, {exit: 1})
126162
}
127163
}
128164
} catch (error: any) {

src/graphQL/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/graphQL/queries.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/lib/stack/builtins.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)