@contember/client-content-generator 2.2.0-alpha.2 → 2.2.0-alpha.21
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/development/generate.cjs +2 -54
- package/dist/development/generate.cjs.map +1 -1
- package/dist/development/generate.js +2 -37
- package/dist/development/generate.js.map +1 -1
- package/dist/development/src/ContemberClientGenerator.cjs +4 -5
- package/dist/development/src/ContemberClientGenerator.cjs.map +1 -1
- package/dist/development/src/ContemberClientGenerator.js +4 -5
- package/dist/development/src/ContemberClientGenerator.js.map +1 -1
- package/dist/development/src/EntityTypeSchemaGenerator.cjs +40 -16
- package/dist/development/src/EntityTypeSchemaGenerator.cjs.map +1 -1
- package/dist/development/src/EntityTypeSchemaGenerator.js +41 -17
- package/dist/development/src/EntityTypeSchemaGenerator.js.map +1 -1
- package/dist/development/src/GenerateContentClient.cjs +72 -0
- package/dist/development/src/GenerateContentClient.cjs.map +1 -0
- package/dist/development/src/GenerateContentClient.js +55 -0
- package/dist/development/src/GenerateContentClient.js.map +1 -0
- package/dist/development/src/NameSchemaGenerator.cjs +12 -2
- package/dist/development/src/NameSchemaGenerator.cjs.map +1 -1
- package/dist/development/src/NameSchemaGenerator.js +12 -2
- package/dist/development/src/NameSchemaGenerator.js.map +1 -1
- package/dist/development/src/utils.cjs +21 -0
- package/dist/development/src/utils.cjs.map +1 -1
- package/dist/development/src/utils.js +22 -1
- package/dist/development/src/utils.js.map +1 -1
- package/dist/production/generate.cjs +2 -54
- package/dist/production/generate.cjs.map +1 -1
- package/dist/production/generate.js +2 -37
- package/dist/production/generate.js.map +1 -1
- package/dist/production/src/ContemberClientGenerator.cjs +4 -5
- package/dist/production/src/ContemberClientGenerator.cjs.map +1 -1
- package/dist/production/src/ContemberClientGenerator.js +4 -5
- package/dist/production/src/ContemberClientGenerator.js.map +1 -1
- package/dist/production/src/EntityTypeSchemaGenerator.cjs +40 -16
- package/dist/production/src/EntityTypeSchemaGenerator.cjs.map +1 -1
- package/dist/production/src/EntityTypeSchemaGenerator.js +41 -17
- package/dist/production/src/EntityTypeSchemaGenerator.js.map +1 -1
- package/dist/production/src/GenerateContentClient.cjs +72 -0
- package/dist/production/src/GenerateContentClient.cjs.map +1 -0
- package/dist/production/src/GenerateContentClient.js +55 -0
- package/dist/production/src/GenerateContentClient.js.map +1 -0
- package/dist/production/src/NameSchemaGenerator.cjs +12 -2
- package/dist/production/src/NameSchemaGenerator.cjs.map +1 -1
- package/dist/production/src/NameSchemaGenerator.js +12 -2
- package/dist/production/src/NameSchemaGenerator.js.map +1 -1
- package/dist/production/src/utils.cjs +21 -0
- package/dist/production/src/utils.cjs.map +1 -1
- package/dist/production/src/utils.js +22 -1
- package/dist/production/src/utils.js.map +1 -1
- package/dist/types/ContemberClientGenerator.d.ts +2 -1
- package/dist/types/ContemberClientGenerator.d.ts.map +1 -1
- package/dist/types/EntityTypeSchemaGenerator.d.ts +2 -1
- package/dist/types/EntityTypeSchemaGenerator.d.ts.map +1 -1
- package/dist/types/EnumTypeSchemaGenerator.d.ts.map +1 -1
- package/dist/types/GenerateContentClient.d.ts +14 -0
- package/dist/types/GenerateContentClient.d.ts.map +1 -0
- package/dist/types/NameSchemaGenerator.d.ts +2 -1
- package/dist/types/NameSchemaGenerator.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils.d.ts +13 -0
- package/dist/types/utils.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/ContemberClientGenerator.ts +5 -5
- package/src/EntityTypeSchemaGenerator.ts +43 -16
- package/src/GenerateContentClient.ts +72 -0
- package/src/NameSchemaGenerator.ts +12 -2
- package/src/generate.ts +2 -41
- package/src/index.ts +1 -0
- package/src/tsconfig.json +1 -0
- package/src/utils.ts +29 -0
- package/tests/__snapshots__/generateEnittyTypes.test.ts.snap +559 -1
- package/tests/generateEnittyTypes.test.ts +40 -0
- package/tests/schemas.ts +93 -20
- package/tests/typecheckGeneratedClient.test.ts +53 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises'
|
|
2
|
+
import { join, resolve } from 'node:path'
|
|
3
|
+
import { Command, CommandConfiguration, Input } from '@contember/cli-common'
|
|
4
|
+
import { ContemberClientGenerator } from './ContemberClientGenerator.js'
|
|
5
|
+
|
|
6
|
+
type Args = {
|
|
7
|
+
['schema-path']?: string
|
|
8
|
+
['out-dir']?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type Options = {
|
|
12
|
+
['exclude-deprecated']?: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const usageInfo = `
|
|
16
|
+
Usage:
|
|
17
|
+
|
|
18
|
+
From file:
|
|
19
|
+
yarn contember-client-generator <schema.json> <out-dir> [--exclude-deprecated]
|
|
20
|
+
|
|
21
|
+
From stdin:
|
|
22
|
+
yarn run --silent contember project:print-schema --format=schema | yarn contember-client-generator - <out-dir> [--exclude-deprecated]
|
|
23
|
+
`
|
|
24
|
+
|
|
25
|
+
export class GenerateContentClient extends Command<Args, Options> {
|
|
26
|
+
protected configure(configuration: CommandConfiguration<Args, Options>) {
|
|
27
|
+
configuration.description('Generate content client.')
|
|
28
|
+
|
|
29
|
+
configuration.argument('schema-path').optional().description('Path to the json schema file. Tip: Use "-" to read from stdin.')
|
|
30
|
+
configuration.argument('out-dir').optional().description('Output directory for generated files.')
|
|
31
|
+
|
|
32
|
+
configuration.option('exclude-deprecated').valueNone().description('Leave out deprecated entities and fields instead of marking them @deprecated')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected async execute(input: Input<Args, Options>) {
|
|
36
|
+
const schemaPath = input.getArgument('schema-path')
|
|
37
|
+
const outDir = input.getArgument('out-dir')
|
|
38
|
+
const excludeDeprecated = input.getOption('exclude-deprecated')
|
|
39
|
+
|
|
40
|
+
if (!schemaPath || !outDir) {
|
|
41
|
+
// eslint-disable-next-line no-console
|
|
42
|
+
console.info(usageInfo)
|
|
43
|
+
throw new Error('Missing required arguments <schema.json> and <out-dir>')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const sourceData = await (async () => {
|
|
47
|
+
if (schemaPath === '-') {
|
|
48
|
+
if (process.stdin.isTTY) {
|
|
49
|
+
throw new Error('Cannot read from stdin in TTY mode')
|
|
50
|
+
}
|
|
51
|
+
const buffer = []
|
|
52
|
+
for await (const chunk of process.stdin) {
|
|
53
|
+
buffer.push(chunk)
|
|
54
|
+
}
|
|
55
|
+
return Buffer.concat(buffer).toString('utf8')
|
|
56
|
+
}
|
|
57
|
+
return await fs.readFile(resolve(process.cwd(), schemaPath), 'utf8')
|
|
58
|
+
})()
|
|
59
|
+
|
|
60
|
+
const source = JSON.parse(sourceData)
|
|
61
|
+
|
|
62
|
+
const dir = resolve(process.cwd(), outDir)
|
|
63
|
+
const generator = new ContemberClientGenerator()
|
|
64
|
+
const result = generator.generate(source.model, { excludeDeprecated })
|
|
65
|
+
|
|
66
|
+
await fs.mkdir(dir, { recursive: true })
|
|
67
|
+
|
|
68
|
+
for (const [name, content] of Object.entries(result)) {
|
|
69
|
+
await fs.writeFile(join(dir, name), content, 'utf8')
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
import { SchemaEntityNames, SchemaNames } from '@contember/client-content'
|
|
2
2
|
import { Model } from '@contember/schema'
|
|
3
3
|
import { acceptEveryFieldVisitor } from '@contember/schema-utils'
|
|
4
|
+
import { GenerateOptions, isColumnExcluded, isEntityExcluded, isRelationExcluded } from './utils.js'
|
|
4
5
|
|
|
5
6
|
export class NameSchemaGenerator {
|
|
6
|
-
generate(model: Model.Schema): SchemaNames {
|
|
7
|
+
generate(model: Model.Schema, options?: GenerateOptions): SchemaNames {
|
|
7
8
|
return {
|
|
8
9
|
entities: Object.fromEntries(
|
|
9
|
-
Object.values(model.entities).map(entity => {
|
|
10
|
+
Object.values(model.entities).filter(entity => !isEntityExcluded(entity, options)).map(entity => {
|
|
10
11
|
const fields: Record<string, SchemaEntityNames<any>['fields'][string]> = {}
|
|
11
12
|
const scalars: string[] = []
|
|
12
13
|
|
|
13
14
|
acceptEveryFieldVisitor(model, entity, {
|
|
14
15
|
visitHasOne: ctx => {
|
|
16
|
+
if (isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
15
19
|
fields[ctx.relation.name] = {
|
|
16
20
|
type: 'one',
|
|
17
21
|
entity: ctx.targetEntity.name,
|
|
18
22
|
}
|
|
19
23
|
},
|
|
20
24
|
visitHasMany: ctx => {
|
|
25
|
+
if (isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {
|
|
26
|
+
return
|
|
27
|
+
}
|
|
21
28
|
fields[ctx.relation.name] = {
|
|
22
29
|
type: 'many',
|
|
23
30
|
entity: ctx.targetEntity.name,
|
|
24
31
|
}
|
|
25
32
|
},
|
|
26
33
|
visitColumn: ctx => {
|
|
34
|
+
if (isColumnExcluded(ctx.column, options)) {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
27
37
|
scalars.push(ctx.column.name)
|
|
28
38
|
fields[ctx.column.name] = {
|
|
29
39
|
type: 'column',
|
package/src/generate.ts
CHANGED
|
@@ -1,46 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import { join, resolve } from 'node:path'
|
|
4
|
-
import { ContemberClientGenerator } from './ContemberClientGenerator.js'
|
|
2
|
+
import { GenerateContentClient } from './GenerateContentClient.js'
|
|
5
3
|
;(async () => {
|
|
6
|
-
|
|
7
|
-
const outDir = process.argv[3]
|
|
8
|
-
|
|
9
|
-
if (!schemaPath || !outDir) {
|
|
10
|
-
console.error(`Usage:
|
|
11
|
-
|
|
12
|
-
From file:
|
|
13
|
-
yarn contember-client-generator <schema.json> <out-dir>
|
|
14
|
-
|
|
15
|
-
From stdin:
|
|
16
|
-
yarn run --silent contember project:print-schema --format=schema | yarn contember-client-generator - <out-dir>
|
|
17
|
-
`)
|
|
18
|
-
process.exit(1)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const sourceData = await (async () => {
|
|
22
|
-
if (schemaPath === '-') {
|
|
23
|
-
if (process.stdin.isTTY) {
|
|
24
|
-
throw new Error('Cannot read from stdin in TTY mode')
|
|
25
|
-
}
|
|
26
|
-
const buffer = []
|
|
27
|
-
for await (const chunk of process.stdin) {
|
|
28
|
-
buffer.push(chunk)
|
|
29
|
-
}
|
|
30
|
-
return Buffer.concat(buffer).toString('utf8')
|
|
31
|
-
}
|
|
32
|
-
return await fs.readFile(resolve(process.cwd(), process.argv[2]), 'utf8')
|
|
33
|
-
})()
|
|
34
|
-
|
|
35
|
-
const source = JSON.parse(sourceData)
|
|
36
|
-
|
|
37
|
-
const dir = resolve(process.cwd(), process.argv[3])
|
|
38
|
-
const generator = new ContemberClientGenerator()
|
|
39
|
-
const result = generator.generate(source.model)
|
|
40
|
-
await fs.mkdir(dir, { recursive: true })
|
|
41
|
-
for (const [name, content] of Object.entries(result)) {
|
|
42
|
-
await fs.writeFile(join(dir, name), content, 'utf8')
|
|
43
|
-
}
|
|
4
|
+
await new GenerateContentClient().run(process.argv.slice(2))
|
|
44
5
|
})().catch(e => {
|
|
45
6
|
console.error(e)
|
|
46
7
|
process.exit(1)
|
package/src/index.ts
CHANGED
package/src/tsconfig.json
CHANGED
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
import { Model } from '@contember/schema'
|
|
2
|
+
|
|
1
3
|
const upperCaseFirstLetter = (str: string): string => str.charAt(0).toUpperCase() + str.slice(1)
|
|
2
4
|
|
|
3
5
|
export const getEnumTypeName = (enumName: string): string => upperCaseFirstLetter(enumName)
|
|
6
|
+
|
|
7
|
+
export type GenerateOptions = {
|
|
8
|
+
/** Leave deprecated entities and fields out of the generated client. By default they are kept and marked `@deprecated`. */
|
|
9
|
+
excludeDeprecated?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const isEntityExcluded = (entity: Model.Entity, options?: GenerateOptions): boolean =>
|
|
13
|
+
!!options?.excludeDeprecated && entity.deprecationReason !== undefined
|
|
14
|
+
|
|
15
|
+
export const isColumnExcluded = (column: Model.AnyColumn, options?: GenerateOptions): boolean =>
|
|
16
|
+
!!options?.excludeDeprecated && column.deprecationReason !== undefined
|
|
17
|
+
|
|
18
|
+
export const isRelationExcluded = (relation: Model.AnyRelation, targetEntity: Model.Entity, options?: GenerateOptions): boolean =>
|
|
19
|
+
!!options?.excludeDeprecated && (relation.deprecationReason !== undefined || targetEntity.deprecationReason !== undefined)
|
|
20
|
+
|
|
21
|
+
/** A JSDoc block with the description and the deprecation reason, or an empty string when there is neither. */
|
|
22
|
+
export const formatDocComment = (indent: string, annotated: { description?: string; deprecationReason?: string }): string => {
|
|
23
|
+
const lines = [
|
|
24
|
+
...(annotated.description !== undefined ? annotated.description.split('\n') : []),
|
|
25
|
+
...(annotated.deprecationReason !== undefined ? [`@deprecated ${annotated.deprecationReason}`.trimEnd()] : []),
|
|
26
|
+
]
|
|
27
|
+
if (lines.length === 0) {
|
|
28
|
+
return ''
|
|
29
|
+
}
|
|
30
|
+
const escaped = lines.map(line => line.replaceAll('*/', '*\\/'))
|
|
31
|
+
return `${indent}/**\n${escaped.map(line => `${indent} * ${line}`.trimEnd()).join('\n')}\n${indent} */\n`
|
|
32
|
+
}
|