forked from octokit/octokit.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-types.js
167 lines (135 loc) · 4.82 KB
/
generate-types.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module.exports = generateTypes
const { readFileSync, writeFileSync } = require('fs')
const { join: pathJoin } = require('path')
const Mustache = require('mustache')
const upperFirst = require('lodash.upperfirst')
const camelCase = require('lodash.camelcase')
const set = require('lodash.set')
const TypeWriter = require('@gimenete/type-writer')
const prettier = require('prettier')
const ROUTES = require('./lib/get-routes')()
const typeMap = {
integer: 'number',
'integer[]': 'number[]'
}
function parameterize (definition) {
if (definition === null) {
return {}
}
const key = definition.name
const type = typeMap[definition.type] || definition.type
const enums = definition.enum
? definition.enum.map(JSON.stringify).join('|')
: null
const deprecated = definition.deprecated ? `\n@deprecated "${key}" has been renamed to "${definition.alias}"` : ''
return {
name: pascalcase(key),
key: key,
required: !definition.deprecated && definition.required,
type: enums || type,
alias: definition.alias,
deprecated: definition.deprecated,
allowNull: definition.allowNull,
jsdoc: jsdoc(definition.description + deprecated)
}
}
function pascalcase (string) {
return upperFirst(camelCase(string))
}
function toCombineParams (params, param) {
return params
.concat(parameterize(param))
}
function toParamAlias (param, i, params) {
if (!param.alias) {
return param
}
const actualParam = params.find(({ key }) => key === param.alias)
param.required = !param.deprecated && actualParam.required
param.type = actualParam.type
return param
}
function jsdoc (description) {
return description && '/**\n' + description.split('\n').map(str => '* ' + str) + '\n*/'
}
function normalize (methodName) {
return camelCase(methodName.replace(/^edit/, 'update'))
}
generateTypes(
'TypeScript',
'index.d.ts.tpl',
'index.d.ts'
)
function generateTypes (languageName, templateFile, outputFile) {
const templatePath = pathJoin(__dirname, 'templates', templateFile)
const template = readFileSync(templatePath, 'utf8')
const typeWriter = new TypeWriter()
console.log(`Generating ${languageName} types...`)
const childParams = {}
const namespaces = Object.keys(ROUTES)
.reduce((namespaces, namespace) => {
const methods = ROUTES[namespace].reduce((methods, entry) => {
const methodName = normalize(entry.idName)
const namespacedParamsName = pascalcase(`${namespace}-${methodName}Params`)
const params = entry.params
.reduce(toCombineParams, [])
.map(toParamAlias)
// handle "object" & "object[]" types
.map(param => {
if (param.type === 'object' || param.type === 'object[]') {
const childParamsName = pascalcase(`${namespacedParamsName}.${param.key}`)
param.type = param.type.replace('object', childParamsName)
if (!childParams[childParamsName]) {
childParams[childParamsName] = {}
}
}
if (!/\./.test(param.key)) {
return param
}
const childKey = param.key.split('.').pop()
const parentKey = param.key.replace(/\.[^.]+$/, '')
param.key = childKey
const childParamsName = pascalcase(`${namespacedParamsName}.${parentKey}`)
set(childParams, `${childParamsName}.${childKey}`, param)
})
.filter(Boolean)
const hasParams = params.length > 0
let paramTypeName = hasParams
? namespacedParamsName
: pascalcase('EmptyParams')
let responseType = 'Octokit.AnyResponse'
if (entry.responses) {
const typeName = 'Octokit.' + typeWriter.add(entry.responses.map(response => response.body || {}), {
rootTypeName: pascalcase(`${namespace}-${entry.idName}Response`)
})
responseType = 'Octokit.Response<' + typeName + '>'
}
return methods.concat({
method: methodName,
paramTypeName,
ownParams: params.length > 0 && { params },
exclude: !hasParams,
responseType,
jsdoc: jsdoc(entry.description)
})
}, [])
return namespaces.concat({
namespace: camelCase(namespace),
methods
})
}, [])
const body = Mustache.render(template, {
responseTypes: typeWriter.generate('typescript'),
namespaces,
childParams: Object.keys(childParams).map(key => {
return {
paramTypeName: key,
params: Object.values(childParams[key])
}
})
})
const source = prettier.format(body, { parser: languageName.toLowerCase() })
const definitionFilePath = pathJoin(__dirname, '..', outputFile)
writeFileSync(definitionFilePath, source, 'utf8')
console.log(`${languageName} declarations written to ${definitionFilePath}`)
}