Skip to content
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

DX | 03-06-2024 | Release #118

Merged
merged 11 commits into from
Jun 5, 2024
5 changes: 5 additions & 0 deletions bin/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
(async () => {
const { execute } = require("@contentstack/cli-utilities");
await execute({ type: "cjs", development: true, dir: __dirname });
})();
6 changes: 6 additions & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

(async () => {
const {execute} = require('@contentstack/cli-utilities')
await execute({ type: 'cjs', dir: __dirname })
})()
6,572 changes: 4,157 additions & 2,415 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "contentstack-cli-tsgen",
"description": "Generate TypeScript typings from a Stack.",
"version": "2.3.3",
"version": "2.3.4",
"author": "Michael Davis",
"bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues",
"dependencies": {
"@contentstack/cli-command": "^1.2.17",
"@contentstack/cli-utilities": "^1.5.10",
"@contentstack/cli-utilities": "^1.6.1",
"@gql2ts/from-schema": "^2.0.0-4",
"async": "^3.2.5",
"graphql": "^14.7.0",
Expand All @@ -30,7 +30,7 @@
"globby": "^10.0.2",
"jest": "^26.5.3",
"mocha": "^10.2.0",
"oclif": "^3.7.0",
"oclif": "^3.10.0",
"ts-jest": "^26.4.1",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
Expand Down Expand Up @@ -72,4 +72,4 @@
"tsgen": "TSGEN"
}
}
}
}
4 changes: 2 additions & 2 deletions src/commands/tsgen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Command} from '@contentstack/cli-command'
import {FlagInput, flags} from '@contentstack/cli-utilities'
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'
Expand All @@ -15,7 +15,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL" ',
];

static flags: FlagInput = {
static flags: any = {
'token-alias': flags.string({
char: 'a',
description: 'delivery token alias',
Expand Down
1 change: 1 addition & 0 deletions src/lib/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sanitizePath = (str: string) => str?.replace(/^(\.\.(\/|\\|$))+/, '');
23 changes: 12 additions & 11 deletions src/lib/stack/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import * as fs from 'fs'
import * as http from 'https'
import * as async from 'async'
import * as path from 'path'
import { ContentTypeCollection } from 'contentstack'
import {HttpClient, cliux, configHandler} from '@contentstack/cli-utilities'
import {schemaToInterfaces, generateNamespace} from '@gql2ts/from-schema'
import { ContentTypeCollection } from 'contentstack'
import { HttpClient, cliux, configHandler } from '@contentstack/cli-utilities'
import { schemaToInterfaces, generateNamespace } from '@gql2ts/from-schema'

import {introspectionQuery} from '../../graphQL'
import { introspectionQuery } from '../../graphQL'
import { sanitizePath } from '../helper'

type RegionUrlMap = {
[prop: string]: string;
Expand Down Expand Up @@ -35,7 +36,7 @@ export type StackConnectionConfig = {
token: string;
region: any;
environment: string;
branch?: string|null;
branch?: string | null;
}

const limit = 100
Expand Down Expand Up @@ -83,7 +84,7 @@ export async function stackConnect(client: any, config: StackConnectionConfig, c

if (results.count > limit) {
const additionalQueries = Array.from(
{length: Math.ceil(results.count / limit) - 1},
{ length: Math.ceil(results.count / limit) - 1 },
(_, i) => {
return async.reflect(async () => {
return stack.getContentTypes({
Expand Down Expand Up @@ -183,9 +184,9 @@ export async function generateGraphQLTypeDef(config: StackConnectionConfig, outp
// Generate graphql schema with introspection query
const url = `${GRAPHQL_REGION_URL_MAPPING[config.region]}/${config.apiKey}`
const result = await new HttpClient()
.headers(headers)
.queryParams(query)
.post(url, {query: introspectionQuery})
.headers(headers)
.queryParams(query)
.post(url, { query: introspectionQuery })

cliux.loaderV2('', spinner)

Expand All @@ -197,9 +198,9 @@ export async function generateGraphQLTypeDef(config: StackConnectionConfig, outp
}

//Create and write type def in file
const outputPath = path.resolve(process.cwd(), outputFile)
const outputPath = path.resolve(sanitizePath(process.cwd()), sanitizePath(outputFile))
const dirName = path.dirname(outputPath)
fs.mkdirSync(dirName, {recursive: true})
fs.mkdirSync(dirName, { recursive: true })
fs.writeFileSync(outputPath, schema)

return {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/tsgen/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import * as fs from 'fs'
import * as path from 'path'
import * as prettier from 'prettier'

import {defaultInterfaces} from '../stack/builtins'
import {DocumentationGenerator} from './docgen/doc'
import { defaultInterfaces } from '../stack/builtins'
import { DocumentationGenerator } from './docgen/doc'
import JSDocumentationGenerator from './docgen/jsdoc'
import NullDocumentationGenerator from './docgen/nulldoc'
import tsgenFactory from './factory'
import { sanitizePath } from '../helper'

async function format(definition: string) {
const prettierConfig = await prettier.resolveConfig(process.cwd())
Expand All @@ -18,10 +19,10 @@ async function format(definition: string) {
}

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

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

return outputPath
}
Expand Down
Loading