Skip to content

Commit

Permalink
Merge pull request #53 from Contentstack-Solutions/staging
Browse files Browse the repository at this point in the history
Merge staging to master
  • Loading branch information
aman19K authored Oct 25, 2023
2 parents ff88164 + 4ca631d commit 6eef9ee
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "contentstack-cli-tsgen",
"description": "Generate TypeScript typings from a Stack.",
"version": "2.1.7",
"version": "2.1.8",
"author": "Michael Davis",
"bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues",
"dependencies": {
Expand Down
8 changes: 7 additions & 1 deletion src/commands/tsgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
hidden: false,
multiple: false,
}),

'include-system-fields': flags.boolean({
description: 'include system fields in generated types',
default: false,
}),
};

async run() {
Expand All @@ -62,6 +67,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
const includeDocumentation = flags.doc
const outputPath = flags.output
const branch = flags.branch
const includeSystemFields = flags['include-system-fields']

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 Down Expand Up @@ -91,7 +97,7 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
}))
}
schemas = schemas.concat(client.types)
const result = await tsgenRunner(outputPath, schemas, prefix, includeDocumentation)
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.')
Expand Down
72 changes: 47 additions & 25 deletions src/lib/stack/builtins.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
// File and Link fields are additional, non-scalar, data types within a stack.
export const defaultInterfaces = (prefix = '') => [
`export interface ${prefix}File {
uid: string;
created_at: string;
updated_at: string;
created_by: string;
updated_by: string;
content_type: string;
file_size: string;
tags: string[];
filename: string;
url: string;
ACL: any[];
is_dir: boolean;
parent_uid: string;
_version: number;
title: string;
publish_details: {
export const defaultInterfaces = (prefix = '', systemFields = false) => {
const defaultInterfaces = [
`export interface ${prefix}PublishDetails {
environment: string;
locale: string;
time: string;
user: string;
};
}`,
`export interface ${prefix}Link {
title: string;
href: string;
}`,
]
}`,
`export interface ${prefix}File {
uid: string;
created_at: string;
updated_at: string;
created_by: string;
updated_by: string;
content_type: string;
file_size: string;
tags: string[];
filename: string;
url: string;
ACL: any[];
is_dir: boolean;
parent_uid: string;
_version: number;
title: string;
publish_details: ${prefix}PublishDetails;
}`,
`export interface ${prefix}Link {
title: string;
href: string;
}`
]
if (systemFields) {
defaultInterfaces.push(
`export interface ${prefix}SystemFields {
uid?: string;
created_at?: string;
updated_at?: string;
created_by?: string;
updated_by?: string;
_content_type_uid?: string;
tags?: string[];
ACL?: any[];
_version?: number;
_in_progress?: boolean;
locale?: string;
publish_details?: ${prefix}PublishDetails[];
title?: string;
}`)
}
return defaultInterfaces;
}
8 changes: 7 additions & 1 deletion src/lib/stack/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Block = {
export type GlobalField = {
reference_to: string;
schema: Schema;
schema_type?: string;
_version?: number
} & FieldOptions;

Expand All @@ -50,7 +51,8 @@ export type Field = GlobalField &
ReferenceField &
GroupField &
EnumField &
BlockField;
BlockField &
{ field_metadata: FieldMetaData };
export type Schema = Array<Field>;

export type ContentType = {
Expand All @@ -61,3 +63,7 @@ export type ContentType = {
data_type?: string;
schema_type?: string;
} & Identifier;

export type FieldMetaData = {
ref_multiple?: boolean;
}
15 changes: 11 additions & 4 deletions src/lib/tsgen/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TSGenOptions = {
naming?: {
prefix: string;
};
systemFields?: boolean;
};

export type TSGenResult = {
Expand Down Expand Up @@ -54,6 +55,7 @@ const defaultOptions: TSGenOptions = {
naming: {
prefix: '',
},
systemFields: false
}

export default function (userOptions: TSGenOptions) {
Expand Down Expand Up @@ -115,9 +117,14 @@ export default function (userOptions: TSGenOptions) {
}

function define_interface(
contentType: ContentstackTypes.ContentType | ContentstackTypes.GlobalField
contentType: ContentstackTypes.ContentType | ContentstackTypes.GlobalField,
systemFields = false
) {
return ['export interface', name_type(contentType.data_type === 'global_field' ? (contentType.reference_to as string) : contentType.uid)].join(' ')
const interface_declaration = ['export interface', name_type(contentType.data_type === 'global_field' ? (contentType.reference_to as string) : contentType.uid)]
if (systemFields && contentType.schema_type !== "global_field") {
interface_declaration.push('extends', name_type('SystemFields'))
}
return interface_declaration.join(' ')
}

function op_array(type: string, field: ContentstackTypes.Field) {
Expand Down Expand Up @@ -226,7 +233,7 @@ export default function (userOptions: TSGenOptions) {
) {
return [
options.docgen.interface(contentType.description),
define_interface(contentType),
define_interface(contentType, options.systemFields),
'{',
['/**', "Version", '*/'].join(' '),
[`version: `,contentType._version,';'].join(' '),
Expand Down Expand Up @@ -302,7 +309,7 @@ export default function (userOptions: TSGenOptions) {
references.push(name_type(field.reference_to))
}

return ['(', references.join(' | '), ')', '[]'].join('')
return ['(', references.join(' | '), ')', field.field_metadata?.ref_multiple ? '[]' : ''].join('')
}

return function (contentType: ContentstackTypes.ContentType): TSGenResult|any {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/tsgen/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function createOutputPath(outputFile: string) {
return outputPath
}

export default async function tsgenRunner(outputFile: string, contentTypes: any[], prefix = '', includeDocumentation = true) {
export default async function tsgenRunner(outputFile: string, contentTypes: any[], prefix = '', includeDocumentation = true, systemFields = false) {
const docgen: DocumentationGenerator = includeDocumentation ? new JSDocumentationGenerator() : new NullDocumentationGenerator()

const outputPath = createOutputPath(outputFile)
Expand All @@ -38,6 +38,7 @@ export default async function tsgenRunner(outputFile: string, contentTypes: any[
naming: {
prefix,
},
systemFields
})

for (const contentType of contentTypes) {
Expand All @@ -56,7 +57,7 @@ export default async function tsgenRunner(outputFile: string, contentTypes: any[

const output = await format(
[
defaultInterfaces(prefix).join('\n\n'),
defaultInterfaces(prefix, systemFields).join('\n\n'),
[...globalFields].join('\n\n'),
definitions.join('\n\n'),
].join('\n\n')
Expand Down
34 changes: 17 additions & 17 deletions tests/tsgen/references.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const testData = require('./references.ct')
const testData = require("./references.ct");

import NullDocumentationGenerator from '../../src/lib/tsgen/docgen/nulldoc'
import tsgenFactory from '../../src/lib/tsgen/factory'
import NullDocumentationGenerator from "../../src/lib/tsgen/docgen/nulldoc";
import tsgenFactory from "../../src/lib/tsgen/factory";

const tsgen = tsgenFactory({
docgen: new NullDocumentationGenerator(),
naming: {
prefix: 'I',
prefix: "I",
},
})
});

describe('references', () => {
const result = tsgen(testData.references)
describe("references", () => {
const result = tsgen(testData.references);

test('metadata', () => {
const contentTypes = [...result.metadata.dependencies.contentTypes]
test("metadata", () => {
const contentTypes = [...result.metadata.dependencies.contentTypes];

expect(contentTypes).toEqual(
expect.arrayContaining(['IReferenceChild', 'IBoolean', 'IBuiltinExample'])
)
})
expect.arrayContaining(["IReferenceChild", "IBoolean", "IBuiltinExample"])
);
});

test('definition', () => {
test("definition", () => {
expect(result.definition).toMatchInlineSnapshot(`
"export interface IReferenceParent
{
/** Version */
version: 5 ;
title: string;
url: string;
single_reference: (IReferenceChild)[];
single_reference: (IReferenceChild);
multiple_reference?: (IReferenceChild | IBoolean | IBuiltinExample)[];
}"
`)
})
})
`);
});
});

0 comments on commit 6eef9ee

Please sign in to comment.