Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"oclif-typescript"
],
"rules": {
"unicorn/prefer-module": "off",
"unicorn/no-abusive-eslint-disable": "off",
"@typescript-eslint/no-use-before-define": "off"
}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/dist
/tmp
/yarn.lock
/logs
node_modules
.vscode/
oclif.manifest.json
.env
2,899 changes: 1,628 additions & 1,271 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"dependencies": {
"@contentstack/cli-command": "^1.3.1",
"@contentstack/cli-utilities": "^1.7.3",
"@gql2ts/from-schema": "^2.0.0-4",
"@contentstack/types-generator": "^2.0.2",
"async": "^3.2.6",
"dotenv": "^16.4.5",
"fancy-test": "^3.0.16",
"graphql": "^14.7.0",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -66,7 +67,7 @@
"prepack": "rm -rf lib && tsc -b && oclif manifest && oclif readme",
"test": "jest --testPathPattern=tests",
"version": "oclif readme && git add README.md",
"test:unit": "mocha --forbid-only -r ts-node/register \"test/**/*.test.ts\""
"test:integration": "jest --testPathPattern=tests/integration"
},
"csdxConfig": {
"shortCommandName": {
Expand Down
92 changes: 64 additions & 28 deletions src/commands/tsgen.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import {Command} from '@contentstack/cli-command'
import {flags} from '@contentstack/cli-utilities'
import {getGlobalFields, stackConnect, StackConnectionConfig, generateGraphQLTypeDef} from '../lib/stack/client'
import {ContentType} from '../lib/stack/schema'
import tsgenRunner from '../lib/tsgen/runner'
import * as path from 'path'
import * as fs from 'fs'
import {sanitizePath} from '../lib/helper'
import {generateTS, graphqlTS} from '@contentstack/types-generator'
import {StackConnectionConfig} from '../types'

function createOutputPath(outputFile: string) {
const outputPath = path.resolve(sanitizePath(process.cwd()), sanitizePath(outputFile))
const dirName = path.dirname(outputPath)

fs.mkdirSync(dirName, {recursive: true})

return outputPath
}

export default class TypeScriptCodeGeneratorCommand extends Command {
static description = 'generate TypeScript typings from a Stack';
static description = 'Generate TypeScript typings from a Stack';

static examples = [
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"',
Expand Down Expand Up @@ -78,11 +89,13 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
const token = this.getToken(flags['token-alias'])
const prefix = flags.prefix
const includeDocumentation = flags.doc
const outputPath = flags.output
const filePath = flags.output
const branch = flags.branch
const includeSystemFields = flags['include-system-fields']
const namespace = flags.namespace

const outputPath = createOutputPath(filePath)

if (token.type !== 'delivery') {
this.warn('Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.')
}
Expand All @@ -91,38 +104,61 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
this.error('Please provide an output path.', {exit: 2})
}

let region = ''
switch (this.region.name) {
case 'NA':
region = 'US'
break
case 'AZURE-NA':
region = 'AZURE_NA'
break
case 'AZURE-EU':
region = 'AZURE_EU'
break
case 'GCP-NA':
region = 'GCP_NA'
break
default:
region = this.region.name.toUpperCase()
}

const config: StackConnectionConfig = {
apiKey: token.apiKey,
token: token.token,
region: (this.region.name === 'NA') ? 'us' : this.region.name.toLowerCase(),
region: region,
environment: token.environment || '',
branch: branch || null,
branch: branch || undefined,
}

// Generate the GraphQL schema TypeScript definitions
if (flags['api-type'] === 'graphql') {
const result = await generateGraphQLTypeDef(config, outputPath, namespace)
if (result) {
this.log(`Successfully added the GraphQL schema type definitions to '${result.outputPath}'.`)
} else {
this.log('No schema found in the stack! Please use a valid stack.')
try {
const result = await graphqlTS({...config, namespace: namespace})

fs.writeFileSync(outputPath, result)
this.log(`Successfully added the GraphQL schema type definitions to '${outputPath}'.`)
} catch (error: any) {
this.error(error.error_message, {exit: 1})
}
} else {
const [client, globalFields] = await Promise.all([stackConnect(this.deliveryAPIClient.Stack, config, this.cdaHost), getGlobalFields(config, this.cdaHost)])

let schemas: ContentType[] = []
if (client.types?.length) {
if ((globalFields as any)?.global_fields?.length) {
schemas = schemas.concat((globalFields as any).global_fields as ContentType)
schemas = schemas.map(schema => ({
...schema,
schema_type: 'global_field',
}))
}
schemas = schemas.concat(client.types)
const result = await tsgenRunner(outputPath, schemas, prefix, includeDocumentation, includeSystemFields)
this.log(`Wrote ${result.definitions} Content Types to '${result.outputPath}'.`)
} else {
this.log('No Content Types exist in the Stack.')
// Generate the Content Types TypeScript definitions
try {
const result = await generateTS({
...config,
tokenType: 'delivery',
includeDocumentation: includeDocumentation,
prefix,
systemFields: includeSystemFields,
})

fs.writeFileSync(outputPath, result)

// -- TODO : Add count support for the number of Content Types generated
this.log(`Successfully added the Content Types to '${outputPath}'.`)

// this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
} catch (error: any) {
this.error(error.error_message, {exit: 1})
}
}
} catch (error: any) {
Expand Down
1 change: 0 additions & 1 deletion src/graphQL/index.ts

This file was deleted.

98 changes: 0 additions & 98 deletions src/graphQL/queries.ts

This file was deleted.

58 changes: 0 additions & 58 deletions src/lib/stack/builtins.ts

This file was deleted.

Loading