@jungvonmatt/contentful-typings 1.12.5 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.d.ts CHANGED
File without changes
package/dist/cli.js CHANGED
@@ -26,16 +26,18 @@ const program = new Command();
26
26
  program
27
27
  .command('generate')
28
28
  .description('Generate typescript definitions for contentful content types.')
29
- .option('-o, --output <filepath>', 'Specify output file', '@types/contentful.d.ts')
29
+ .option('-o, --output <filepath>', 'Specify output file', '@types/contentful.ts')
30
30
  .option('-l, --localized', 'Add localized types')
31
31
  .option('-d, --jsdoc', 'Add JSDoc comments')
32
- .option('-g, --typeguard ', 'Add type guards')
32
+ .option('-g, --typeguard', 'Add type guards')
33
+ .option('-l, --legacy', 'Use legacy types (before contentful@10)')
33
34
  .action(actionRunner(async (cmd) => {
34
35
  const output = cmd?.output ?? '';
35
36
  const typings = await generateTypings({
36
37
  typeguard: cmd.typeguard,
37
38
  jsdoc: cmd.jsdoc,
38
39
  localized: cmd.localized,
40
+ legacy: cmd.legacy,
39
41
  });
40
42
  await outputFile(output, typings);
41
43
  console.log(pico.green(` added: ${output}`));
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,IAAI,MAAM,YAAY,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,oCAAoC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,GAAG,GAAG,MAAM,CAAC,M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 @@
1
- declare type Options = {
1
+ type Options = {
2
2
  localized?: boolean;
3
3
  jsdoc?: boolean;
4
4
  typeguard?: boolean;
5
+ legacy?: boolean;
5
6
  };
6
7
  export declare const generateTypings: (options?: Options) => Promise<string>;
7
8
  export {};
package/dist/index.js CHANGED
@@ -1,49 +1,44 @@
1
+ import path from 'node:path';
1
2
  import { getConfig } from '@jungvonmatt/contentful-ssg/lib/config';
2
3
  import { getEnvironment, pagedGet } from '@jungvonmatt/contentful-ssg/lib/contentful';
3
4
  import { askMissing } from '@jungvonmatt/contentful-ssg/lib/ui';
4
- import camelcase from 'camelcase';
5
- import { CFDefinitionsBuilder, createDefaultContext, DefaultContentTypeRenderer, JsDocRenderer, LocalizedContentTypeRenderer, TypeGuardRenderer, } from 'cf-content-types-generator';
6
- const moduleName = (name) => `${camelcase(name, { pascalCase: true })}`;
7
- const moduleFieldsName = (name) => `${moduleName(name)}Fields`;
8
- const context = { ...createDefaultContext(), moduleName, moduleFieldsName };
9
- class CustomTypeGuardRenderer extends TypeGuardRenderer {
10
- createContext() {
11
- return context;
12
- }
13
- }
14
- class CustomJsDocRenderer extends JsDocRenderer {
15
- createContext() {
16
- return context;
17
- }
18
- }
19
- class CustomLocalizedContentTypeRenderer extends LocalizedContentTypeRenderer {
20
- createContext() {
21
- return context;
22
- }
23
- }
24
- class CustomContentTypeRenderer extends DefaultContentTypeRenderer {
25
- createContext() {
26
- return context;
27
- }
28
- }
5
+ import { CFDefinitionsBuilder } from 'cf-content-types-generator';
6
+ import { readPackageUp } from 'read-pkg-up';
7
+ import semiver from 'semiver';
8
+ import { DefaultContentTypeRenderer, V10ContentTypeRenderer, V10TypeGuardRenderer, JsDocRenderer, LocalizedContentTypeRenderer, TypeGuardRenderer, } from './renderer/index.js';
9
+ const isLegacyVersion = async (dir) => {
10
+ try {
11
+ const cwd = path.join(dir || process.cwd(), 'node_modules', 'contentful');
12
+ const { packageJson } = await readPackageUp({ cwd });
13
+ if (packageJson.name === 'contentful' &&
14
+ semiver(packageJson.version, '10.0.0-beta-v10.33') < 0) {
15
+ return true;
16
+ }
17
+ }
18
+ catch { }
19
+ return false;
20
+ };
29
21
  export const generateTypings = async (options = {}) => {
30
- const contentfulConfig = (await askMissing(await getConfig({
22
+ const contentfulConfig = await askMissing(await getConfig({
31
23
  previewAccessToken: '-',
32
24
  accessToken: '-',
33
- })));
25
+ }));
34
26
  const client = await getEnvironment(contentfulConfig);
35
27
  const { items: contentTypes } = await pagedGet(client, {
36
28
  method: 'getContentTypes',
37
29
  });
38
- const renderers = [new CustomContentTypeRenderer()];
30
+ const legacyVersion = typeof options.legacy === 'undefined' ? await isLegacyVersion() : options.legacy;
31
+ const renderers = [
32
+ legacyVersion ? new DefaultContentTypeRenderer() : new V10ContentTypeRenderer(),
33
+ ];
39
34
  if (options.localized) {
40
- renderers.push(new CustomLocalizedContentTypeRenderer());
35
+ renderers.push(new LocalizedContentTypeRenderer());
41
36
  }
42
37
  if (options.jsdoc) {
43
- renderers.push(new CustomJsDocRenderer());
38
+ renderers.push(new JsDocRenderer());
44
39
  }
45
40
  if (options.typeguard) {
46
- renderers.push(new CustomTypeGuardRenderer());
41
+ renderers.push(legacyVersion ? new TypeGuardRenderer() : new V10TypeGuardRenderer());
47
42
  }
48
43
  const builder = new CFDefinitionsBuilder(renderers);
49
44
  for (const model of contentTypes) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,4CAA4C,CAAC;AACtF,OAAO,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAC;AAChE,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,EACL,oBAAoB,EAEpB,oBAAoB,EACpB,0BAA0B,EAC1B,aAAa,EACb,4BAA4B,EAE5B,iBAAiB,GAClB,MA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CAAC,CAAC,CAAC,IAAI,oBAAoB,EAAE,CAAC,CAAC;KACtF;IAED,MAAM,OAAO,GAAG,IAAI,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAEpD,KAAK,MAAM,KAAK,IAAI,YAAY,EAAE;QAChC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;KAC3B;IAED,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC;AAC5B,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { DefaultContentTypeRenderer as DefaultContentTypeRendererOriginal, V10ContentTypeRenderer as V10ContentTypeRendererOriginal, RenderContext } from 'cf-content-types-generator';
2
+ export declare class DefaultContentTypeRenderer extends DefaultContentTypeRendererOriginal {
3
+ createContext(): RenderContext;
4
+ }
5
+ export declare class V10ContentTypeRenderer extends V10ContentTypeRendererOriginal {
6
+ createContext(): RenderContext;
7
+ }
@@ -0,0 +1,13 @@
1
+ import { DefaultContentTypeRenderer as DefaultContentTypeRendererOriginal, V10ContentTypeRenderer as V10ContentTypeRendererOriginal, } from 'cf-content-types-generator';
2
+ import { context, v10context } from './context.js';
3
+ export class DefaultContentTypeRenderer extends DefaultContentTypeRendererOriginal {
4
+ createContext() {
5
+ return context;
6
+ }
7
+ }
8
+ export class V10ContentTypeRenderer extends V10ContentTypeRendererOriginal {
9
+ createContext() {
10
+ return v10context;
11
+ }
12
+ }
13
+ //# sourceMappingURL=contentTypeRenderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contentTypeRenderer.js","sourceRoot":"","sources":["../../src/renderer/contentTypeRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0BAA0B,IAAI,kCAAkC,EAChE,sBAAsB,IAAI,8BAA8B,GAEzD,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEnD,MAAM,OAAO,0BAA2B,SAAQ,kCAAkC;IACzE,aAAa;QAClB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,8BAA8B;IACjE,aAAa;QAClB,OAAO,UAAU,CAAC;IACpB,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ import { RenderContext } from 'cf-content-types-generator';
2
+ export declare const moduleName: (name: string) => string;
3
+ export declare const moduleFieldsName: (name: string) => string;
4
+ export declare const moduleSkeletonName: (name: string) => string;
5
+ export declare const context: RenderContext;
6
+ export declare const v10context: RenderContext;
@@ -0,0 +1,18 @@
1
+ import camelcase from 'camelcase';
2
+ import { createDefaultContext, createV10Context } from 'cf-content-types-generator';
3
+ export const moduleName = (name) => `${camelcase(name, { pascalCase: true })}`;
4
+ export const moduleFieldsName = (name) => `${moduleName(name)}Fields`;
5
+ export const moduleSkeletonName = (name) => `${moduleName(name)}Skeleton`;
6
+ export const context = {
7
+ ...createDefaultContext(),
8
+ moduleName,
9
+ moduleFieldsName,
10
+ moduleSkeletonName,
11
+ };
12
+ export const v10context = {
13
+ ...createV10Context(),
14
+ moduleName,
15
+ moduleFieldsName,
16
+ moduleSkeletonName,
17
+ };
18
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/renderer/context.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAiB,MAAM,4BAA4B,CAAC;AAEnG,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC,IAAI,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACvF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC;AAC9E,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC;AAC1F,MAAM,CAAC,MAAM,OAAO,GAAkB;IACpC,GAAG,oBAAoB,EAAE;IACzB,UAAU;IACV,gBAAgB;IAChB,kBAAkB;CACnB,CAAC;AACF,MAAM,CAAC,MAAM,UAAU,GAAkB;IACvC,GAAG,gBAAgB,EAAE;IACrB,UAAU;IACV,gBAAgB;IAChB,kBAAkB;CACnB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { DefaultContentTypeRenderer, V10ContentTypeRenderer } from './contentTypeRenderer.js';
2
+ export { JsDocRenderer } from './jsDocRenderer.js';
3
+ export { TypeGuardRenderer, V10TypeGuardRenderer } from './typeGuardRenderer.js';
4
+ export { LocalizedContentTypeRenderer } from './localizedContentRenderer.js';
@@ -0,0 +1,5 @@
1
+ export { DefaultContentTypeRenderer, V10ContentTypeRenderer } from './contentTypeRenderer.js';
2
+ export { JsDocRenderer } from './jsDocRenderer.js';
3
+ export { TypeGuardRenderer, V10TypeGuardRenderer } from './typeGuardRenderer.js';
4
+ export { LocalizedContentTypeRenderer } from './localizedContentRenderer.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/renderer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAC9F,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACjF,OAAO,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { JsDocRenderer as JsDocRendererOriginal, RenderContext } from 'cf-content-types-generator';
2
+ export declare class JsDocRenderer extends JsDocRendererOriginal {
3
+ createContext(): RenderContext;
4
+ }
@@ -0,0 +1,8 @@
1
+ import { JsDocRenderer as JsDocRendererOriginal } from 'cf-content-types-generator';
2
+ import { context } from './context.js';
3
+ export class JsDocRenderer extends JsDocRendererOriginal {
4
+ createContext() {
5
+ return context;
6
+ }
7
+ }
8
+ //# sourceMappingURL=jsDocRenderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsDocRenderer.js","sourceRoot":"","sources":["../../src/renderer/jsDocRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,IAAI,qBAAqB,EAAiB,MAAM,4BAA4B,CAAC;AACnG,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,MAAM,OAAO,aAAc,SAAQ,qBAAqB;IAC/C,aAAa;QAClB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import { LocalizedContentTypeRenderer as LocalizedContentTypeRendererOriginal, RenderContext } from 'cf-content-types-generator';
2
+ export declare class LocalizedContentTypeRenderer extends LocalizedContentTypeRendererOriginal {
3
+ createContext(): RenderContext;
4
+ }
@@ -0,0 +1,8 @@
1
+ import { LocalizedContentTypeRenderer as LocalizedContentTypeRendererOriginal, } from 'cf-content-types-generator';
2
+ import { context } from './context.js';
3
+ export class LocalizedContentTypeRenderer extends LocalizedContentTypeRendererOriginal {
4
+ createContext() {
5
+ return context;
6
+ }
7
+ }
8
+ //# sourceMappingURL=localizedContentRenderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"localizedContentRenderer.js","sourceRoot":"","sources":["../../src/renderer/localizedContentRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,IAAI,oCAAoC,GAErE,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,MAAM,OAAO,4BAA6B,SAAQ,oCAAoC;IAC7E,aAAa;QAClB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ import { TypeGuardRenderer as TypeGuardRendererOriginal, V10TypeGuardRenderer as V10TypeGuardRendererOriginal, RenderContext, CFContentType } from 'cf-content-types-generator';
2
+ import { SourceFile } from 'ts-morph';
3
+ export declare class TypeGuardRenderer extends TypeGuardRendererOriginal {
4
+ createContext(): RenderContext;
5
+ render: (contentType: CFContentType, file: SourceFile) => void;
6
+ }
7
+ export declare class V10TypeGuardRenderer extends V10TypeGuardRendererOriginal {
8
+ createContext(): RenderContext;
9
+ render: (contentType: CFContentType, file: SourceFile) => void;
10
+ }
@@ -0,0 +1,61 @@
1
+ import { TypeGuardRenderer as TypeGuardRendererOriginal, V10TypeGuardRenderer as V10TypeGuardRendererOriginal, renderTypeGeneric, } from 'cf-content-types-generator';
2
+ import { context, v10context, moduleName } from './context.js';
3
+ export class TypeGuardRenderer extends TypeGuardRendererOriginal {
4
+ createContext() {
5
+ return context;
6
+ }
7
+ render = (contentType, file) => {
8
+ const entryInterfaceName = moduleName(contentType.sys.id);
9
+ file.addImportDeclaration({
10
+ moduleSpecifier: `./WithContentTypeLink`,
11
+ namedImports: ['WithContentTypeLink'],
12
+ isTypeOnly: true,
13
+ });
14
+ file.addFunction({
15
+ isExported: true,
16
+ name: `is${entryInterfaceName}`,
17
+ returnType: `entry is ${entryInterfaceName}`,
18
+ parameters: [
19
+ {
20
+ name: 'entry',
21
+ type: 'WithContentTypeLink',
22
+ },
23
+ ],
24
+ statements: `return entry.sys.contentType.sys.id === '${contentType.sys.id}'`,
25
+ });
26
+ file.organizeImports({
27
+ ensureNewLineAtEndOfFile: true,
28
+ });
29
+ file.formatText();
30
+ };
31
+ }
32
+ export class V10TypeGuardRenderer extends V10TypeGuardRendererOriginal {
33
+ createContext() {
34
+ return v10context;
35
+ }
36
+ render = (contentType, file) => {
37
+ const entryInterfaceName = moduleName(contentType.sys.id);
38
+ file.addImportDeclaration({
39
+ moduleSpecifier: `contentful`,
40
+ namedImports: ['ChainModifiers', 'Entry', 'LocaleCode'],
41
+ isTypeOnly: true,
42
+ });
43
+ file.addFunction({
44
+ isExported: true,
45
+ name: renderTypeGeneric(`is${entryInterfaceName}`, 'Modifiers extends ChainModifiers', 'Locales extends LocaleCode'),
46
+ returnType: `entry is ${renderTypeGeneric(entryInterfaceName, 'Modifiers', 'Locales')}`,
47
+ parameters: [
48
+ {
49
+ name: 'entry',
50
+ type: renderTypeGeneric('Entry', 'EntrySkeletonType', 'Modifiers', 'Locales'),
51
+ },
52
+ ],
53
+ statements: `return entry.sys.contentType.sys.id === '${contentType.sys.id}'`,
54
+ });
55
+ file.organizeImports({
56
+ ensureNewLineAtEndOfFile: true,
57
+ });
58
+ file.formatText();
59
+ };
60
+ }
61
+ //# sourceMappingURL=typeGuardRenderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeGuardRenderer.js","sourceRoot":"","sources":["../../src/renderer/typeGuardRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,IAAI,yBAAyB,EAC9C,oBAAoB,IAAI,4BAA4B,EAGpD,iBAAiB,GAClB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/D,MAAM,OAAO,iBAAkB,SAAQ,yBAAyB;IACvD,aAAa;QAClB,OAAO,OAAO,CAAC;IACjB,CAAC;IAEM,MAAM,GAAG,CAAC,WAA0B,EAAE,IAAgB,EAAQ,EAAE;QACrE,MAAM,kBAAkB,GAAG,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAE1D,IAAI,CAAC,oBAAoB,CAAC;YACxB,eAAe,EAAE,uBAAuB;YACxC,YAAY,EAAE,CAAC,qBAAqB,CAAC;YACrC,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC;QAEH,IAAI,CAAC,WAAW,CAAC;YACf,UAAU,EAAE,IAAI;YAChB,IAAI,EAAE,KAAK,kBAAkB,EAAE;YAC/B,UAAU,EAAE,YAAY,kBAAkB,EAAE;YAC5C,UAAU,EAAE;gBACV;oBACE,IAAI,EA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AC;CACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jungvonmatt/contentful-typings",
3
- "version": "1.12.5",
3
+ "version": "1.13.0",
4
4
  "main": "./dist/index.js",
5
5
  "type": "module",
6
6
  "exports": "./dist/index.js",
@@ -39,18 +39,20 @@
39
39
  "dependencies": {
40
40
  "@jungvonmatt/contentful-ssg": "^1.12.3",
41
41
  "camelcase": "^7.0.1",
42
- "cf-content-types-generator": "^2.9.1",
42
+ "cf-content-types-generator": "^2.12.2",
43
43
  "commander": "^10.0.0",
44
- "contentful": "^9.3.3",
44
+ "contentful": "^10.2.0",
45
45
  "contentful-management": "^10.30.0",
46
46
  "dotenv": "^16.0.3",
47
47
  "dotenv-expand": "^10.0.0",
48
48
  "fs-extra": "^11.1.0",
49
49
  "picocolors": "^1.0.0",
50
- "prettier": "^2.8.4"
50
+ "prettier": "^2.8.4",
51
+ "read-pkg-up": "^9.1.0",
52
+ "semiver": "^1.1.0"
51
53
  },
52
54
  "devDependencies": {
53
55
  "ts-morph": "^17.0.1"
54
56
  },
55
- "gitHead": "bf645c2d97d053eca730874e4931f86328903e1e"
57
+ "gitHead": "9d2a819dde3cbc7ae7bf57c5d852740519f50de1"
56
58
  }
package/src/cli.ts CHANGED
@@ -15,9 +15,10 @@ expand(env);
15
15
 
16
16
  type CommandArgs = {
17
17
  output: string;
18
- typeguard: boolean;
19
- jsdoc: boolean;
20
- localized: boolean;
18
+ typeguard?: boolean;
19
+ jsdoc?: boolean;
20
+ localized?: boolean;
21
+ legacy?: boolean;
21
22
  };
22
23
 
23
24
  type CommandError = Error & {
@@ -48,10 +49,11 @@ const program = new Command();
48
49
  program
49
50
  .command('generate')
50
51
  .description('Generate typescript definitions for contentful content types.')
51
- .option('-o, --output <filepath>', 'Specify output file', '@types/contentful.d.ts')
52
+ .option('-o, --output <filepath>', 'Specify output file', '@types/contentful.ts')
52
53
  .option('-l, --localized', 'Add localized types')
53
54
  .option('-d, --jsdoc', 'Add JSDoc comments')
54
- .option('-g, --typeguard ', 'Add type guards')
55
+ .option('-g, --typeguard', 'Add type guards')
56
+ .option('-l, --legacy', 'Use legacy types (before contentful@10)')
55
57
  .action(
56
58
  actionRunner(async (cmd: CommandArgs) => {
57
59
  const output: string = cmd?.output ?? '';
@@ -59,6 +61,7 @@ program
59
61
  typeguard: cmd.typeguard,
60
62
  jsdoc: cmd.jsdoc,
61
63
  localized: cmd.localized,
64
+ legacy: cmd.legacy,
62
65
  });
63
66
 
64
67
  await outputFile(output, typings);
package/src/index.ts CHANGED
@@ -1,78 +1,75 @@
1
1
  /* _eslint-disable @typescript-eslint/no-unsafe-call */
2
+ import path from 'node:path';
2
3
  import { ContentfulConfig, ContentType } from '@jungvonmatt/contentful-ssg';
3
4
  import { getConfig } from '@jungvonmatt/contentful-ssg/lib/config';
4
5
  import { getEnvironment, pagedGet } from '@jungvonmatt/contentful-ssg/lib/contentful';
5
6
  import { askMissing } from '@jungvonmatt/contentful-ssg/lib/ui';
6
- import camelcase from 'camelcase';
7
+ import { CFDefinitionsBuilder, ContentTypeRenderer } from 'cf-content-types-generator';
8
+ import { readPackageUp } from 'read-pkg-up';
9
+ import semiver from 'semiver';
10
+
7
11
  import {
8
- CFDefinitionsBuilder,
9
- ContentTypeRenderer,
10
- createDefaultContext,
11
12
  DefaultContentTypeRenderer,
13
+ V10ContentTypeRenderer,
14
+ V10TypeGuardRenderer,
12
15
  JsDocRenderer,
13
16
  LocalizedContentTypeRenderer,
14
- RenderContext,
15
17
  TypeGuardRenderer,
16
- } from 'cf-content-types-generator';
18
+ } from './renderer/index.js';
17
19
 
18
20
  type Options = {
19
21
  localized?: boolean;
20
22
  jsdoc?: boolean;
21
23
  typeguard?: boolean;
24
+ legacy?: boolean;
22
25
  };
23
26
 
24
- const moduleName = (name: string) => `${camelcase(name, { pascalCase: true })}`;
25
- const moduleFieldsName = (name: string) => `${moduleName(name)}Fields`;
26
- const context: RenderContext = { ...createDefaultContext(), moduleName, moduleFieldsName };
27
-
28
- class CustomTypeGuardRenderer extends TypeGuardRenderer {
29
- public createContext(): RenderContext {
30
- return context;
31
- }
32
- }
27
+ const isLegacyVersion = async (dir?: string) => {
28
+ try {
29
+ const cwd = path.join(dir || process.cwd(), 'node_modules', 'contentful');
30
+ const { packageJson } = await readPackageUp({ cwd });
31
+ // New skeleton types were released in contentful v10.0.0-beta-v10.33
32
+ if (
33
+ packageJson.name === 'contentful' &&
34
+ semiver(packageJson.version, '10.0.0-beta-v10.33') < 0
35
+ ) {
36
+ return true;
37
+ }
38
+ } catch {}
33
39
 
34
- class CustomJsDocRenderer extends JsDocRenderer {
35
- public createContext(): RenderContext {
36
- return context;
37
- }
38
- }
39
- class CustomLocalizedContentTypeRenderer extends LocalizedContentTypeRenderer {
40
- public createContext(): RenderContext {
41
- return context;
42
- }
43
- }
44
-
45
- class CustomContentTypeRenderer extends DefaultContentTypeRenderer {
46
- public createContext(): RenderContext {
47
- return context;
48
- }
49
- }
40
+ return false;
41
+ };
50
42
 
51
43
  export const generateTypings = async (options: Options = {}) => {
52
- const contentfulConfig: ContentfulConfig = (await askMissing(
44
+ const contentfulConfig = await askMissing(
53
45
  await getConfig({
54
46
  previewAccessToken: '-',
55
47
  accessToken: '-',
56
48
  })
57
- )) as ContentfulConfig;
49
+ );
58
50
 
59
- const client = await getEnvironment(contentfulConfig);
51
+ const client = await getEnvironment(contentfulConfig as ContentfulConfig);
60
52
 
61
53
  const { items: contentTypes } = await pagedGet<ContentType>(client, {
62
54
  method: 'getContentTypes',
63
55
  });
64
56
 
65
- const renderers: ContentTypeRenderer[] = [new CustomContentTypeRenderer()];
57
+ const legacyVersion =
58
+ typeof options.legacy === 'undefined' ? await isLegacyVersion() : options.legacy;
59
+
60
+ const renderers: ContentTypeRenderer[] = [
61
+ legacyVersion ? new DefaultContentTypeRenderer() : new V10ContentTypeRenderer(),
62
+ ];
66
63
  if (options.localized) {
67
- renderers.push(new CustomLocalizedContentTypeRenderer());
64
+ renderers.push(new LocalizedContentTypeRenderer());
68
65
  }
69
66
 
70
67
  if (options.jsdoc) {
71
- renderers.push(new CustomJsDocRenderer());
68
+ renderers.push(new JsDocRenderer());
72
69
  }
73
70
 
74
71
  if (options.typeguard) {
75
- renderers.push(new CustomTypeGuardRenderer());
72
+ renderers.push(legacyVersion ? new TypeGuardRenderer() : new V10TypeGuardRenderer());
76
73
  }
77
74
 
78
75
  const builder = new CFDefinitionsBuilder(renderers);
@@ -0,0 +1,18 @@
1
+ import {
2
+ DefaultContentTypeRenderer as DefaultContentTypeRendererOriginal,
3
+ V10ContentTypeRenderer as V10ContentTypeRendererOriginal,
4
+ RenderContext,
5
+ } from 'cf-content-types-generator';
6
+ import { context, v10context } from './context.js';
7
+
8
+ export class DefaultContentTypeRenderer extends DefaultContentTypeRendererOriginal {
9
+ public createContext(): RenderContext {
10
+ return context;
11
+ }
12
+ }
13
+
14
+ export class V10ContentTypeRenderer extends V10ContentTypeRendererOriginal {
15
+ public createContext(): RenderContext {
16
+ return v10context;
17
+ }
18
+ }
@@ -0,0 +1,18 @@
1
+ import camelcase from 'camelcase';
2
+ import { createDefaultContext, createV10Context, RenderContext } from 'cf-content-types-generator';
3
+
4
+ export const moduleName = (name: string) => `${camelcase(name, { pascalCase: true })}`;
5
+ export const moduleFieldsName = (name: string) => `${moduleName(name)}Fields`;
6
+ export const moduleSkeletonName = (name: string): string => `${moduleName(name)}Skeleton`;
7
+ export const context: RenderContext = {
8
+ ...createDefaultContext(),
9
+ moduleName,
10
+ moduleFieldsName,
11
+ moduleSkeletonName,
12
+ };
13
+ export const v10context: RenderContext = {
14
+ ...createV10Context(),
15
+ moduleName,
16
+ moduleFieldsName,
17
+ moduleSkeletonName,
18
+ };
@@ -0,0 +1,4 @@
1
+ export { DefaultContentTypeRenderer, V10ContentTypeRenderer } from './contentTypeRenderer.js';
2
+ export { JsDocRenderer } from './jsDocRenderer.js';
3
+ export { TypeGuardRenderer, V10TypeGuardRenderer } from './typeGuardRenderer.js';
4
+ export { LocalizedContentTypeRenderer } from './localizedContentRenderer.js';
@@ -0,0 +1,8 @@
1
+ import { JsDocRenderer as JsDocRendererOriginal, RenderContext } from 'cf-content-types-generator';
2
+ import { context } from './context.js';
3
+
4
+ export class JsDocRenderer extends JsDocRendererOriginal {
5
+ public createContext(): RenderContext {
6
+ return context;
7
+ }
8
+ }
@@ -0,0 +1,11 @@
1
+ import {
2
+ LocalizedContentTypeRenderer as LocalizedContentTypeRendererOriginal,
3
+ RenderContext,
4
+ } from 'cf-content-types-generator';
5
+ import { context } from './context.js';
6
+
7
+ export class LocalizedContentTypeRenderer extends LocalizedContentTypeRendererOriginal {
8
+ public createContext(): RenderContext {
9
+ return context;
10
+ }
11
+ }
@@ -0,0 +1,83 @@
1
+ import {
2
+ TypeGuardRenderer as TypeGuardRendererOriginal,
3
+ V10TypeGuardRenderer as V10TypeGuardRendererOriginal,
4
+ RenderContext,
5
+ CFContentType,
6
+ renderTypeGeneric,
7
+ } from 'cf-content-types-generator';
8
+ import { SourceFile } from 'ts-morph';
9
+ import { context, v10context, moduleName } from './context.js';
10
+
11
+ export class TypeGuardRenderer extends TypeGuardRendererOriginal {
12
+ public createContext(): RenderContext {
13
+ return context;
14
+ }
15
+
16
+ public render = (contentType: CFContentType, file: SourceFile): void => {
17
+ const entryInterfaceName = moduleName(contentType.sys.id);
18
+
19
+ file.addImportDeclaration({
20
+ moduleSpecifier: `./WithContentTypeLink`,
21
+ namedImports: ['WithContentTypeLink'],
22
+ isTypeOnly: true,
23
+ });
24
+
25
+ file.addFunction({
26
+ isExported: true,
27
+ name: `is${entryInterfaceName}`,
28
+ returnType: `entry is ${entryInterfaceName}`,
29
+ parameters: [
30
+ {
31
+ name: 'entry',
32
+ type: 'WithContentTypeLink',
33
+ },
34
+ ],
35
+ statements: `return entry.sys.contentType.sys.id === '${contentType.sys.id}'`,
36
+ });
37
+
38
+ file.organizeImports({
39
+ ensureNewLineAtEndOfFile: true,
40
+ });
41
+
42
+ file.formatText();
43
+ };
44
+ }
45
+
46
+ export class V10TypeGuardRenderer extends V10TypeGuardRendererOriginal {
47
+ public createContext(): RenderContext {
48
+ return v10context;
49
+ }
50
+
51
+ public render = (contentType: CFContentType, file: SourceFile): void => {
52
+ const entryInterfaceName = moduleName(contentType.sys.id);
53
+
54
+ file.addImportDeclaration({
55
+ moduleSpecifier: `contentful`,
56
+ namedImports: ['ChainModifiers', 'Entry', 'LocaleCode'],
57
+ isTypeOnly: true,
58
+ });
59
+
60
+ file.addFunction({
61
+ isExported: true,
62
+ name: renderTypeGeneric(
63
+ `is${entryInterfaceName}`,
64
+ 'Modifiers extends ChainModifiers',
65
+ 'Locales extends LocaleCode'
66
+ ),
67
+ returnType: `entry is ${renderTypeGeneric(entryInterfaceName, 'Modifiers', 'Locales')}`,
68
+ parameters: [
69
+ {
70
+ name: 'entry',
71
+ type: renderTypeGeneric('Entry', 'EntrySkeletonType', 'Modifiers', 'Locales'),
72
+ },
73
+ ],
74
+ statements: `return entry.sys.contentType.sys.id === '${contentType.sys.id}'`,
75
+ });
76
+
77
+ file.organizeImports({
78
+ ensureNewLineAtEndOfFile: true,
79
+ });
80
+
81
+ file.formatText();
82
+ };
83
+ }