Skip to content

Commit d340a14

Browse files
committed
Merge branch 'development' into fix/staging
2 parents 632d2bc + ddc113d commit d340a14

6 files changed

Lines changed: 76 additions & 64 deletions

File tree

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"rules": {
77
"unicorn/prefer-module": "off",
88
"unicorn/no-abusive-eslint-disable": "off",
9-
"@typescript-eslint/no-use-before-define": "off"
9+
"@typescript-eslint/no-use-before-define": "off",
10+
"node/no-missing-import": "off"
1011
}
1112
}

.github/workflows/node.js.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ jobs:
2525
node-version: ${{ matrix.node-version }}
2626
- run: npm ci
2727
- run: npm run build --if-present
28-
# - run: npm test
28+
- run: npm i -g @contentstack/cli
29+
- run: csdx config:set:region ${{secrets.REGION}}
30+
- run: csdx auth:tokens:add -a ${{ secrets.TOKEN_ALIAS }} --delivery -k ${{ secrets.APIKEY }} --token ${{ secrets.DELIVERYKEY }} -e ${{ secrets.ENVIRONMENT }}
31+
- run: npm run prepack
32+
- run: csdx plugins:link
33+
- run: npm test
34+
env:
35+
TOKEN_ALIAS: ${{ secrets.TOKEN_ALIAS }}
36+
- run: csdx plugins:unlink

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "contentstack-cli-tsgen",
33
"description": "Generate TypeScript typings from a Stack.",
4-
"version": "3.1.0",
4+
"version": "3.1.1",
55
"author": "Michael Davis",
66
"bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues",
77
"dependencies": {

src/commands/tsgen.ts

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { Command } from "@contentstack/cli-command";
2-
import { flags } from "@contentstack/cli-utilities";
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";
1+
import {Command} from '@contentstack/cli-command'
2+
import {flags} from '@contentstack/cli-utilities'
3+
import * as path from 'path'
4+
import * as fs from 'fs'
5+
import {generateTS, graphqlTS} from '@contentstack/types-generator'
6+
import {sanitizePath} from '../lib/helper'
7+
import {StackConnectionConfig} from '../types'
88

99
function createOutputPath(outputFile: string) {
1010
const outputPath = path.resolve(
1111
sanitizePath(process.cwd()),
1212
sanitizePath(outputFile),
13-
);
14-
const dirName = path.dirname(outputPath);
13+
)
14+
const dirName = path.dirname(outputPath)
1515

16-
fs.mkdirSync(dirName, { recursive: true });
16+
fs.mkdirSync(dirName, {recursive: true})
1717

18-
return outputPath;
18+
return outputPath
1919
}
2020

2121
export default class TypeScriptCodeGeneratorCommand extends Command {
22-
static description = "Generate TypeScript typings from a Stack";
22+
static description = 'Generate TypeScript typings from a Stack';
2323

2424
static examples = [
2525
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"',
@@ -30,132 +30,135 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
3030
];
3131

3232
static flags: any = {
33-
"token-alias": flags.string({
34-
char: "a",
35-
description: "delivery token alias",
33+
'token-alias': flags.string({
34+
char: 'a',
35+
description: 'delivery token alias',
3636
hidden: false,
3737
multiple: false,
3838
required: true,
3939
}),
4040

4141
output: flags.string({
42-
char: "o",
43-
description: "full path to output",
42+
char: 'o',
43+
description: 'full path to output',
4444
hidden: false,
4545
multiple: false,
4646
required: true,
4747
}),
4848

4949
prefix: flags.string({
50-
char: "p",
50+
char: 'p',
5151
description: 'interface prefix, e.g. "I"',
5252
hidden: false,
5353
multiple: false,
54-
default: "",
54+
default: '',
5555
required: false,
5656
}),
5757

5858
doc: flags.boolean({
59-
char: "d",
60-
description: "include documentation comments",
59+
char: 'd',
60+
description: 'include documentation comments',
6161
default: true,
6262
allowNo: true,
6363
}),
6464

6565
branch: flags.string({
66-
description: "branch",
66+
description: 'branch',
6767
hidden: false,
6868
multiple: false,
6969
}),
7070

71-
"include-system-fields": flags.boolean({
72-
description: "include system fields in generated types",
71+
'include-system-fields': flags.boolean({
72+
description: 'include system fields in generated types',
7373
default: false,
7474
}),
7575

76-
"api-type": flags.string({
77-
default: "rest",
76+
'api-type': flags.string({
77+
default: 'rest',
7878
multiple: false,
79-
options: ["rest", "graphql"],
79+
options: ['rest', 'graphql'],
8080
description:
81-
"[Optional] Please enter an API type to generate the type definitions.",
81+
'[Optional] Please enter an API type to generate the type definitions.',
8282
}),
8383

8484
namespace: flags.string({
8585
description:
86-
"[Optional]Please enter a namespace for the GraphQL API type to organize the generated types.",
86+
'[Optional]Please enter a namespace for the GraphQL API type to organize the generated types.',
8787
}),
8888
};
8989

9090
async run() {
9191
try {
92-
const { flags } = await this.parse(TypeScriptCodeGeneratorCommand);
92+
const {flags} = await this.parse(TypeScriptCodeGeneratorCommand)
9393

94-
const token = this.getToken(flags["token-alias"]);
95-
const prefix = flags.prefix;
96-
const includeDocumentation = flags.doc;
97-
const filePath = flags.output;
98-
const branch = flags.branch;
99-
const includeSystemFields = flags["include-system-fields"];
100-
const namespace = flags.namespace;
94+
const token = this.getToken(flags['token-alias'])
95+
const prefix = flags.prefix
96+
const includeDocumentation = flags.doc
97+
const filePath = flags.output
98+
const branch = flags.branch
99+
const includeSystemFields = flags['include-system-fields']
100+
const namespace = flags.namespace
101101

102-
const outputPath = createOutputPath(filePath);
102+
const outputPath = createOutputPath(filePath)
103103

104-
if (token.type !== "delivery") {
104+
if (token.type !== 'delivery') {
105105
this.warn(
106-
"Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.",
107-
);
106+
'Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.',
107+
)
108108
}
109109

110110
if (!outputPath || !outputPath.trim()) {
111-
this.error("Please provide an output path.", { exit: 2 });
111+
this.error('Please provide an output path.', {exit: 2})
112112
}
113113

114114
const config: StackConnectionConfig = {
115115
apiKey: token.apiKey,
116116
token: token.token,
117117
region:
118-
this.region.name === "NA" ? "us" : this.region.name.toLowerCase(),
119-
environment: token.environment || "",
118+
this.region.name === 'NA' ? 'us' : this.region.name.toLowerCase(),
119+
environment: token.environment || '',
120120
branch: branch || undefined,
121121
host: this.cdaHost,
122-
};
122+
}
123123

124124
// Generate the GraphQL schema TypeScript definitions
125-
if (flags["api-type"] === "graphql") {
125+
if (flags['api-type'] === 'graphql') {
126126
try {
127-
const result = await graphqlTS({ ...config, namespace: namespace });
127+
if (config.region === 'us') {
128+
config.region = 'US'
129+
}
130+
const result = await graphqlTS({...config, namespace: namespace})
128131

129-
fs.writeFileSync(outputPath, result);
132+
fs.writeFileSync(outputPath, result)
130133
this.log(
131134
`Successfully added the GraphQL schema type definitions to '${outputPath}'.`,
132-
);
135+
)
133136
} catch (error: any) {
134-
this.error(error.error_message, { exit: 1 });
137+
this.error(error.error_message, {exit: 1})
135138
}
136139
} else {
137140
// Generate the Content Types TypeScript definitions
138141
try {
139142
const result = await generateTS({
140143
...config,
141-
tokenType: "delivery",
144+
tokenType: 'delivery',
142145
includeDocumentation: includeDocumentation,
143146
prefix,
144147
systemFields: includeSystemFields,
145-
});
148+
})
146149

147-
fs.writeFileSync(outputPath, result);
150+
fs.writeFileSync(outputPath, result)
148151

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

152155
// this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
153156
} catch (error: any) {
154-
this.error(error.error_message, { exit: 1 });
157+
this.error(error.error_message, {exit: 1})
155158
}
156159
}
157160
} catch (error: any) {
158-
this.error(error as any, { exit: 1 });
161+
this.error(error as any, {exit: 1})
159162
}
160163
}
161164
}

tests/integration/tsgen.integration.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("Integration Test for tsgen command", () => {
2828

2929
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
3030
expect(generatedContent).toContain("interface"); // Verify TypeScript interface presence
31-
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Multi-line comment block check
31+
expect(generatedContent).toMatch(/(?:\/\*\*.*?\*\/\s*)?export/); // Multi-line comment block check
3232
});
3333

3434
// Test case 2: Generate TypeScript types with a prefix applied
@@ -61,7 +61,7 @@ describe("Integration Test for tsgen command", () => {
6161
});
6262
}
6363

64-
expect(generatedContent).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Multi-line comment block check
64+
expect(generatedContent).toMatch(/(?:\/\*\*.*?\*\/\s*)?export/); // Multi-line comment block check
6565
});
6666

6767
// Test case 3: Generate TypeScript types without documentation comments
@@ -75,7 +75,7 @@ describe("Integration Test for tsgen command", () => {
7575
expect(fs.existsSync(outputFilePath)).toBeTruthy();
7676

7777
const generatedContent = fs.readFileSync(outputFilePath, "utf8");
78-
expect(generatedContent).not.toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Ensure no multi-line comments
78+
expect(generatedContent).toMatch(/(?:\/\*\*.*?\*\/\s*)?export/); // Ensure no multi-line comments
7979
});
8080

8181
// Test case 4: Generate TypeScript types with system fields

0 commit comments

Comments
 (0)