-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathrenderFieldsOnly.ts
27 lines (19 loc) · 978 Bytes
/
renderFieldsOnly.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ContentType } from "contentful"
import { format, resolveConfig } from "prettier"
import renderContentType from "./contentful-fields-only/renderContentType"
import renderNamespace from "./contentful/renderNamespace"
export interface Options {
namespace?: string
prefix?: string
suffix?: string
}
export default async function renderFieldsOnly(contentTypes: ContentType[], options: Options) {
const sortedContentTypes = contentTypes.sort((a, b) => a.sys.id.localeCompare(b.sys.id))
const typingsSource = renderAllContentTypes(sortedContentTypes, options)
const source = [renderNamespace(typingsSource, options.namespace)].join("\n\n")
const prettierConfig = await resolveConfig(process.cwd())
return format(source, { ...prettierConfig, parser: "typescript" })
}
function renderAllContentTypes(contentTypes: ContentType[], options: Options): string {
return contentTypes.map(contentType => renderContentType(contentType, options)).join("\n\n")
}