-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcontentful-typescript-codegen.ts
105 lines (95 loc) · 3.09 KB
/
contentful-typescript-codegen.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import render from "./renderers/render"
import renderFieldsOnly from "./renderers/renderFieldsOnly"
import path from "path"
import { outputFileSync } from "fs-extra"
const meow = require("meow")
const cli = meow(
`
Usage
$ contentful-typescript-codegen --output <file> <options>
Options
--output, -o Where to write to
--poll, -p Continuously refresh types
--interval N, -i The interval in seconds at which to poll (defaults to 15)
--namespace N, -n Wrap types in namespace N (disabled by default)
--fields-only Output a tree that _only_ ensures fields are valid
and present, and does not provide types for Sys,
Assets, or Rich Text. This is useful for ensuring raw
Contentful responses will be compatible with your code.
--localization -l Output fields with localized values
--environment-file -e Specify a path to Contentful environment file (defaults to ./getContentfulEnvironment.js)
Examples
$ contentful-typescript-codegen -o src/@types/generated/contentful.d.ts
`,
{
flags: {
output: {
type: "string",
alias: "o",
required: true,
},
fieldsOnly: {
type: "boolean",
required: false,
},
poll: {
type: "boolean",
alias: "p",
required: false,
},
interval: {
type: "string",
alias: "i",
required: false,
},
namespace: {
type: "string",
alias: "n",
required: false,
},
localization: {
type: "boolean",
alias: "l",
required: false,
},
environmentFile: {
type: "string",
alias: "e",
required: false,
},
},
},
)
async function runCodegen(outputFile: string) {
const getEnvironmentPathDefault = path.resolve(process.cwd(), "./getContentfulEnvironment.js")
const getEnvironmentPath = cli.flags.environmentFile
? path.resolve(cli.flags.environmentFile)
: getEnvironmentPathDefault
const getEnvironment = require(getEnvironmentPath)
const environment = await getEnvironment()
const contentTypes = await environment.getContentTypes({ limit: 1000 })
const locales = await environment.getLocales()
const outputPath = path.resolve(process.cwd(), outputFile)
let output
if (cli.flags.fieldsOnly) {
output = await renderFieldsOnly(contentTypes.items, { namespace: cli.flags.namespace })
} else {
output = await render(contentTypes.items, locales.items, {
localization: cli.flags.localization,
namespace: cli.flags.namespace,
})
}
outputFileSync(outputPath, output)
}
runCodegen(cli.flags.output).catch(error => {
console.error(error)
process.exit(1)
})
if (cli.flags.poll) {
const intervalInSeconds = parseInt(cli.flags.interval, 10)
if (!isNaN(intervalInSeconds) && intervalInSeconds > 0) {
setInterval(() => runCodegen(cli.flags.output), intervalInSeconds * 1000)
} else {
throw new Error(`Expected a positive numeric interval, but got ${cli.flags.interval}`)
}
}