Skip to content

Commit 1613385

Browse files
Merge pull request #118 from contentstack/staging
DX | 03-06-2024 | Release
2 parents 50d26e6 + ab6d2ab commit 1613385

File tree

8 files changed

+4192
-2436
lines changed

8 files changed

+4192
-2436
lines changed

bin/dev.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
(async () => {
3+
const { execute } = require("@contentstack/cli-utilities");
4+
await execute({ type: "cjs", development: true, dir: __dirname });
5+
})();

bin/run.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
3+
(async () => {
4+
const {execute} = require('@contentstack/cli-utilities')
5+
await execute({ type: 'cjs', dir: __dirname })
6+
})()

package-lock.json

Lines changed: 4157 additions & 2415 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "contentstack-cli-tsgen",
33
"description": "Generate TypeScript typings from a Stack.",
4-
"version": "2.3.3",
4+
"version": "2.3.4",
55
"author": "Michael Davis",
66
"bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues",
77
"dependencies": {
88
"@contentstack/cli-command": "^1.2.17",
9-
"@contentstack/cli-utilities": "^1.5.10",
9+
"@contentstack/cli-utilities": "^1.6.1",
1010
"@gql2ts/from-schema": "^2.0.0-4",
1111
"async": "^3.2.5",
1212
"graphql": "^14.7.0",
@@ -30,7 +30,7 @@
3030
"globby": "^10.0.2",
3131
"jest": "^26.5.3",
3232
"mocha": "^10.2.0",
33-
"oclif": "^3.7.0",
33+
"oclif": "^3.10.0",
3434
"ts-jest": "^26.4.1",
3535
"ts-node": "^10.9.1",
3636
"typescript": "^4.8.4"
@@ -72,4 +72,4 @@
7272
"tsgen": "TSGEN"
7373
}
7474
}
75-
}
75+
}

src/commands/tsgen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Command} from '@contentstack/cli-command'
2-
import {FlagInput, flags} from '@contentstack/cli-utilities'
2+
import {flags} from '@contentstack/cli-utilities'
33
import {getGlobalFields, stackConnect, StackConnectionConfig, generateGraphQLTypeDef} from '../lib/stack/client'
44
import {ContentType} from '../lib/stack/schema'
55
import tsgenRunner from '../lib/tsgen/runner'
@@ -15,7 +15,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
1515
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL" ',
1616
];
1717

18-
static flags: FlagInput = {
18+
static flags: any = {
1919
'token-alias': flags.string({
2020
char: 'a',
2121
description: 'delivery token alias',

src/lib/helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sanitizePath = (str: string) => str?.replace(/^(\.\.(\/|\\|$))+/, '');

src/lib/stack/client.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import * as fs from 'fs'
22
import * as http from 'https'
33
import * as async from 'async'
44
import * as path from 'path'
5-
import { ContentTypeCollection } from 'contentstack'
6-
import {HttpClient, cliux, configHandler} from '@contentstack/cli-utilities'
7-
import {schemaToInterfaces, generateNamespace} from '@gql2ts/from-schema'
5+
import { ContentTypeCollection } from 'contentstack'
6+
import { HttpClient, cliux, configHandler } from '@contentstack/cli-utilities'
7+
import { schemaToInterfaces, generateNamespace } from '@gql2ts/from-schema'
88

9-
import {introspectionQuery} from '../../graphQL'
9+
import { introspectionQuery } from '../../graphQL'
10+
import { sanitizePath } from '../helper'
1011

1112
type RegionUrlMap = {
1213
[prop: string]: string;
@@ -35,7 +36,7 @@ export type StackConnectionConfig = {
3536
token: string;
3637
region: any;
3738
environment: string;
38-
branch?: string|null;
39+
branch?: string | null;
3940
}
4041

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

8485
if (results.count > limit) {
8586
const additionalQueries = Array.from(
86-
{length: Math.ceil(results.count / limit) - 1},
87+
{ length: Math.ceil(results.count / limit) - 1 },
8788
(_, i) => {
8889
return async.reflect(async () => {
8990
return stack.getContentTypes({
@@ -183,9 +184,9 @@ export async function generateGraphQLTypeDef(config: StackConnectionConfig, outp
183184
// Generate graphql schema with introspection query
184185
const url = `${GRAPHQL_REGION_URL_MAPPING[config.region]}/${config.apiKey}`
185186
const result = await new HttpClient()
186-
.headers(headers)
187-
.queryParams(query)
188-
.post(url, {query: introspectionQuery})
187+
.headers(headers)
188+
.queryParams(query)
189+
.post(url, { query: introspectionQuery })
189190

190191
cliux.loaderV2('', spinner)
191192

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

199200
//Create and write type def in file
200-
const outputPath = path.resolve(process.cwd(), outputFile)
201+
const outputPath = path.resolve(sanitizePath(process.cwd()), sanitizePath(outputFile))
201202
const dirName = path.dirname(outputPath)
202-
fs.mkdirSync(dirName, {recursive: true})
203+
fs.mkdirSync(dirName, { recursive: true })
203204
fs.writeFileSync(outputPath, schema)
204205

205206
return {

src/lib/tsgen/runner.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import * as fs from 'fs'
22
import * as path from 'path'
33
import * as prettier from 'prettier'
44

5-
import {defaultInterfaces} from '../stack/builtins'
6-
import {DocumentationGenerator} from './docgen/doc'
5+
import { defaultInterfaces } from '../stack/builtins'
6+
import { DocumentationGenerator } from './docgen/doc'
77
import JSDocumentationGenerator from './docgen/jsdoc'
88
import NullDocumentationGenerator from './docgen/nulldoc'
99
import tsgenFactory from './factory'
10+
import { sanitizePath } from '../helper'
1011

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

2021
function createOutputPath(outputFile: string) {
21-
const outputPath = path.resolve(process.cwd(), outputFile)
22+
const outputPath = path.resolve(sanitizePath(process.cwd()), sanitizePath(outputFile))
2223
const dirName = path.dirname(outputPath)
2324

24-
fs.mkdirSync(dirName, {recursive: true})
25+
fs.mkdirSync(dirName, { recursive: true })
2526

2627
return outputPath
2728
}

0 commit comments

Comments
 (0)