Skip to content

Commit

Permalink
Merge pull request #525 from contember/fix/cli-print
Browse files Browse the repository at this point in the history
cli: fix print schema
  • Loading branch information
matej21 committed Mar 20, 2024
2 parents fbebab7 + a7f8a43 commit f6019f1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/cli/src/commands/project/ProjectPrintSchemaCommand.ts
@@ -1,15 +1,14 @@
import { Command, CommandConfiguration, Input } from '@contember/cli-common'
import { Command, CommandConfiguration, Input, Workspace } from '@contember/cli-common'
import { validateSchemaAndPrintErrors } from '../../utils/schema'
import { Workspace, validateProjectName } from '@contember/cli-common'
import {
Authorizator,
EntityRulesResolver,
GraphQlSchemaBuilderFactory,
IntrospectionSchemaDefinitionFactory,
IntrospectionSchemaFactory,
PermissionFactory,
Authorizator,
} from '@contember/engine-content-api'
import { DocumentNode, GraphQLSchema, printSchema } from 'graphql'
import { printSchema } from 'graphql'
import { mergeSchemas } from '@graphql-tools/schema'
import { loadSchema } from '../../utils/project/loadSchema'
import { normalizeSchema } from '@contember/schema-utils'
Expand Down Expand Up @@ -70,13 +69,20 @@ export class ProjectPrintSchemaCommand extends Command<Args, Options> {
new EntityRulesResolver(schemaNormalized.validation, schemaNormalized.model),
authorizator,
)

const printByLine = (out: string) => {
out.split('\n').forEach(line => {
process.stdout.write(line + '\n')
})
}
if (format === 'schema') {
if (inputRoles) {
throw `--roles option is not supported for "schema" format`
}
console.log(JSON.stringify(schemaNormalized, null, '\t'))
const jsonString = JSON.stringify(schemaNormalized, null, '\t')
printByLine(jsonString)
} else if (format === 'introspection') {
console.log(JSON.stringify(introspection.create(), null, '\t'))
printByLine(JSON.stringify(introspection.create(), null, '\t'))
} else if (format === 'graphql') {
const contentSchema = schemaBuilderFactory.create(schemaNormalized.model, authorizator).build()
const introspectionSchemaFactory = new IntrospectionSchemaDefinitionFactory(introspection)
Expand All @@ -86,7 +92,7 @@ export class ProjectPrintSchemaCommand extends Command<Args, Options> {
})
gqlSchema.description = '' // this enforces schema definition print
const printedSchema = printSchema(gqlSchema).replace('""""""\n', '') // remove empty comment
console.log(printedSchema)
printByLine(printedSchema)
} else {
throw `Unknown format ${format}`
}
Expand Down

0 comments on commit f6019f1

Please sign in to comment.