Typings support (#737)

* Updated scripts

* Updated .gitignore

* Removed symbols

* Fixed typo

* Added typings

* Updated test

* Added typings test
This commit is contained in:
Tomas Della Vedova
2018-11-30 17:01:55 +01:00
committed by GitHub
parent db73802af9
commit ab1d7ba992
16 changed files with 869 additions and 45 deletions

View File

@ -22,6 +22,7 @@ function start (opts) {
const packageFolder = join(__dirname, '..', 'api')
const apiOutputFolder = join(packageFolder, 'api')
const mainOutputFile = join(packageFolder, 'index.js')
const typesOutputFile = join(packageFolder, 'generated.d.ts')
log.text = 'Cleaning API folder...'
rimraf.sync(join(apiOutputFolder, '*.js'))
@ -35,9 +36,15 @@ function start (opts) {
readdirSync(apiFolder).forEach(generateApiFile(apiFolder, log))
readdirSync(xPackFolder).forEach(generateApiFile(xPackFolder, log))
const { fn: factory, types } = genFactory(apiOutputFolder)
writeFileSync(
mainOutputFile,
genFactory(apiOutputFolder),
factory,
{ encoding: 'utf8' }
)
writeFileSync(
typesOutputFile,
types,
{ encoding: 'utf8' }
)
lintFiles(log)
@ -69,6 +76,7 @@ function start (opts) {
return log.fail(err.message)
}
log.succeed('Done!')
console.log('Remember to copy the generated types into the index.d.ts file')
})
}
}

View File

@ -7,6 +7,28 @@ const deepmerge = require('deepmerge')
function genFactory (folder) {
// get all the API files
const apiFiles = readdirSync(folder)
const types = apiFiles
.map(file => {
return file
.slice(0, -3) // remove `.js` extension
.split('.')
.reverse()
.reduce((acc, val) => {
const obj = {
[val]: acc === null
? 'apiMethod'
: acc
}
if (isSnakeCased(val)) {
obj[camelify(val)] = acc === null
? 'apiMethod'
: acc
}
return obj
}, null)
})
.reduce((acc, val) => deepmerge(acc, val), {})
const apis = apiFiles
.map(file => {
// const name = format(file.slice(0, -3))
@ -38,6 +60,14 @@ function genFactory (folder) {
// remove useless quotes
.replace(/"/g, '')
// serialize the type object
const typesStr = Object.keys(types)
.map(key => `${key}: ${JSON.stringify(types[key], null, 2)}`)
.join('\n')
// remove useless quotes and commas
.replace(/"/g, '')
.replace(/,/g, '')
const fn = dedent`
'use strict'
@ -75,7 +105,7 @@ function genFactory (folder) {
`
// new line at the end of file
return fn + '\n'
return { fn: fn + '\n', types: typesStr }
}
// from snake_case to camelCase