Skip to content

Split markdown files by "kind" #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const resolveFrom = require('resolve-from')
const { loadSchemaJSON, schemaToJSON } = require('./loadSchemaJSON')
const renderSchema = require('./renderSchema')
const updateSchema = require('./updateSchema')
const updateSchemaByKind = require('./updateSchemaByKind')
const diffSchema = require('./diffSchema')

function safeExit(code) {
Expand Down Expand Up @@ -39,6 +40,14 @@ function printHelp(console) {
create (if the file does not exist)
--require <module> If importing the schema from a module, require the specified
module first (useful for e.g. babel-register)
--by-kind <boolean> Split markdown by kind (default: false)
- Query
- Mutation
- Objects
- Inputs
- Enums
- Scalar
- Interfaces
--version Print version and exit
`)
}
Expand All @@ -47,7 +56,10 @@ function run(
argv = process.argv.slice(2),
{ console = global.console, exit = true } = {}
) {
const args = parseArgs(argv)
const args = parseArgs(argv, {
boolean: 'by-kind',
default: { 'by-kind': false }
})

if (args.help) {
printHelp(console)
Expand Down Expand Up @@ -97,6 +109,29 @@ function run(
safeExit(1)
}
})
} else if (args['by-kind']) {
;[
'Query.md',
'Mutation.md',
'Objects.md',
'InputObjects.md',
'Enums.md',
'Scalars.md',
'Interfaces.md'
].forEach(item => {
updateSchemaByKind(item, schema, options)
.then(() => {
if (exit) {
safeExit(0)
}
})
.catch(err => {
console.error(err)
if (exit) {
safeExit(1)
}
})
})
} else {
renderSchema(schema, options)
if (exit) {
Expand Down
232 changes: 158 additions & 74 deletions src/renderSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ function sortBy(arr, property) {
}

function renderType(type, options) {
const appendObjectUrl = options.appendObjectUrl || ''

if (type.kind === 'NON_NULL') {
return renderType(type.ofType, options) + '!'
}
if (type.kind === 'LIST') {
return `[${renderType(type.ofType, options)}]`
}
const url = options.getTypeURL(type)
const url = options.getTypeURL(type, appendObjectUrl)
return url ? `<a href="${url}">${type.name}</a>` : type.name
}

Expand All @@ -27,9 +29,12 @@ function renderObject(type, options) {
const headingLevel = options.headingLevel || 1
const getTypeURL = options.getTypeURL
const isInputObject = type.kind === 'INPUT_OBJECT'
const hasPrefix = options.hasPrefix || false

if (!skipTitle) {
printer(`\n${'#'.repeat(headingLevel + 2)} ${type.name}\n`)
printer(
`\n${'#'.repeat(headingLevel + (hasPrefix ? 1 : 2))} ${type.name}\n`
)
}
if (type.description) {
printer(`${type.description}\n`)
Expand Down Expand Up @@ -57,7 +62,12 @@ function renderObject(type, options) {
field.isDeprecated ? ' ⚠️' : ''
}</td>`
)
printer(`<td valign="top">${renderType(field.type, { getTypeURL })}</td>`)
printer(
`<td valign="top">${renderType(field.type, {
getTypeURL,
appendObjectUrl: hasPrefix
})}</td>`
)
if (field.description || field.isDeprecated) {
printer('<td>')
if (field.description) {
Expand All @@ -80,7 +90,12 @@ function renderObject(type, options) {
field.args.forEach((arg, i) => {
printer('<tr>')
printer(`<td colspan="2" align="right" valign="top">${arg.name}</td>`)
printer(`<td valign="top">${renderType(arg.type, { getTypeURL })}</td>`)
printer(
`<td valign="top">${renderType(arg.type, {
getTypeURL,
appendObjectUrl: hasPrefix
})}</td>`
)
if (arg.description) {
printer('<td>')
printer(`\n${arg.description}\n`)
Expand All @@ -105,6 +120,7 @@ function renderSchema(schema, options) {
const printer = options.printer || console.log
const headingLevel = options.headingLevel || 1
const unknownTypeURL = options.unknownTypeURL
const itemType = options.itemType || false

if (schema.__schema) {
schema = schema.__schema
Expand All @@ -114,8 +130,18 @@ function renderSchema(schema, options) {
const typeMap = schema.types.reduce((typeMap, type) => {
return Object.assign(typeMap, { [type.name]: type })
}, {})
const getTypeURL = type => {
const url = `#${type.name.toLowerCase()}`
const getTypeURL = (type, hasPrefix = false) => {
const prefix = hasPrefix
? type.kind
.split('_')
.map(i => {
return i.charAt(0).toUpperCase() + i.slice(1).toLowerCase()
})
.join('') + 's.html'
: ''

const url = `${prefix}#${type.name.toLowerCase()}`

if (typeMap[type.name]) {
return url
} else if (typeof unknownTypeURL === 'function') {
Expand Down Expand Up @@ -145,95 +171,134 @@ function renderSchema(schema, options) {
sortBy(scalars, 'name')
sortBy(interfaces, 'name')

if (!skipTitle) {
printer(`${'#'.repeat(headingLevel)} ${title}\n`)
}
if (!itemType) {
if (!skipTitle) {
printer(`${'#'.repeat(headingLevel)} ${title}\n`)
}

if (prologue) {
printer(`${prologue}\n`)
}
if (prologue) {
printer(`${prologue}\n`)
}

printer('<details>')
printer(' <summary><strong>Table of Contents</strong></summary>\n')
if (query) {
printer(' * [Query](#query)')
}
if (mutation) {
printer(' * [Mutation](#mutation)')
}
if (objects.length) {
printer(' * [Objects](#objects)')
objects.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (inputs.length) {
printer(' * [Inputs](#inputs)')
inputs.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (enums.length) {
printer(' * [Enums](#enums)')
enums.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (scalars.length) {
printer(' * [Scalars](#scalars)')
scalars.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (interfaces.length) {
printer(' * [Interfaces](#interfaces)')
interfaces.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
printer('<details>')
printer(' <summary><strong>Table of Contents</strong></summary>\n')
if (query) {
printer(' * [Query](#query)')
}
if (mutation) {
printer(' * [Mutation](#mutation)')
}
if (objects.length) {
printer(' * [Objects](#objects)')
objects.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (inputs.length) {
printer(' * [Inputs](#inputs)')
inputs.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (enums.length) {
printer(' * [Enums](#enums)')
enums.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (scalars.length) {
printer(' * [Scalars](#scalars)')
scalars.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
if (interfaces.length) {
printer(' * [Interfaces](#interfaces)')
interfaces.forEach(type => {
printer(` * [${type.name}](#${type.name.toLowerCase()})`)
})
}
printer('\n</details>')
}
printer('\n</details>')

if (query) {
if ((query && !itemType) || (itemType === 'Query' && query)) {
printer(
`\n${'#'.repeat(headingLevel + 1)} Query${
query.name === 'Query' ? '' : ' (' + query.name + ')'
}`
)
renderObject(query, { skipTitle: true, headingLevel, printer, getTypeURL })
renderObject(query, {
skipTitle: true,
headingLevel,
printer,
getTypeURL,
hasPrefix: !!itemType
})
}

if (mutation) {
printer(
`\n${'#'.repeat(headingLevel + 1)} Mutation${
mutation.name === 'Mutation' ? '' : ' (' + mutation.name + ')'
}`
)
if ((mutation && !itemType) || (itemType === 'Mutation' && mutation)) {
if (!itemType) {
printer(
`\n${'#'.repeat(headingLevel + 1)} Mutation${
mutation.name === 'Mutation' ? '' : ' (' + mutation.name + ')'
}`
)
}

renderObject(mutation, {
skipTitle: true,
headingLevel,
printer,
getTypeURL
getTypeURL,
hasPrefix: !!itemType
})
}

if (objects.length) {
printer(`\n${'#'.repeat(headingLevel + 1)} Objects`)
if (
(objects.length && !itemType) ||
(itemType === 'Objects' && objects.length)
) {
if (!itemType) {
printer(`\n${'#'.repeat(headingLevel + 1)} Objects`)
}

objects.forEach(type =>
renderObject(type, { headingLevel, printer, getTypeURL })
renderObject(type, {
headingLevel,
printer,
getTypeURL,
hasPrefix: !!itemType
})
)
}

if (inputs.length) {
printer(`\n${'#'.repeat(headingLevel + 1)} Inputs`)
if (
(inputs.length && !itemType) ||
(itemType === 'InputObjects' && inputs.length)
) {
if (!itemType) {
printer(`\n${'#'.repeat(headingLevel + 1)} Inputs`)
}

inputs.forEach(type =>
renderObject(type, { headingLevel, printer, getTypeURL })
renderObject(type, {
headingLevel,
printer,
getTypeURL,
hasPrefix: !!itemType
})
)
}

if (enums.length) {
printer(`\n${'#'.repeat(headingLevel + 1)} Enums`)
if ((enums.length && !itemType) || (itemType === 'Enums' && enums.length)) {
if (!itemType) {
printer(`\n${'#'.repeat(headingLevel + 1)} Enums`)
}

enums.forEach(type => {
printer(`\n${'#'.repeat(headingLevel + 2)} ${type.name}\n`)
printer(
`\n${'#'.repeat(headingLevel + (!itemType ? 2 : 1))} ${type.name}\n`
)
if (type.description) {
printer(`${type.description}\n`)
}
Expand Down Expand Up @@ -274,20 +339,39 @@ function renderSchema(schema, options) {
})
}

if (scalars.length) {
printer(`\n${'#'.repeat(headingLevel + 1)} Scalars\n`)
if (
(scalars.length && !itemType) ||
(itemType === 'Scalars' && scalars.length)
) {
if (!itemType) {
printer(`\n${'#'.repeat(headingLevel + 1)} Scalars\n`)
}

scalars.forEach(type => {
printer(`${'#'.repeat(headingLevel + 2)} ${type.name}\n`)
printer(
`${'#'.repeat(headingLevel + (!itemType ? 2 : 1))} ${type.name}\n`
)
if (type.description) {
printer(`${type.description}\n`)
}
})
}

if (interfaces.length) {
printer(`\n${'#'.repeat(headingLevel + 1)} Interfaces\n`)
if (
(interfaces.length && !itemType) ||
(itemType === 'Interfaces' && interfaces.length)
) {
if (!itemType) {
printer(`\n${'#'.repeat(headingLevel + 1)} Interfaces\n`)
}

interfaces.forEach(type =>
renderObject(type, { headingLevel, printer, getTypeURL })
renderObject(type, {
headingLevel,
printer,
getTypeURL,
hasPrefix: !!itemType
})
)
}

Expand Down
Loading