Updated automatically the main typings file with the generated t… (#919)

* Updated automatically the main typings file with the generated types

* Removed useless log
This commit is contained in:
Tomas Della Vedova
2019-07-25 10:57:47 +02:00
committed by delvedor
parent f74123fb53
commit 29ad0735c2
4 changed files with 18 additions and 10 deletions

View File

@ -20,7 +20,7 @@
'use strict'
const { join } = require('path')
const { readdirSync, writeFileSync } = require('fs')
const { readdirSync, readFileSync, writeFileSync } = require('fs')
const minimist = require('minimist')
const semver = require('semver')
const ora = require('ora')
@ -47,7 +47,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')
const typeDefFile = join(__dirname, '..', 'index.d.ts')
const docOutputFile = join(__dirname, '..', 'docs', 'reference.asciidoc')
const requestParamsOutputFile = join(packageFolder, 'requestParams.d.ts')
const allSpec = []
@ -79,9 +79,14 @@ function start (opts) {
factory,
{ encoding: 'utf8' }
)
const oldTypeDefString = readFileSync(typeDefFile, 'utf8')
const start = oldTypeDefString.indexOf('/* GENERATED */')
const end = oldTypeDefString.indexOf('/* /GENERATED */')
const newTypeDefString = oldTypeDefString.slice(0, start + 15) + '\n' + types + '\n ' + oldTypeDefString.slice(end)
writeFileSync(
typesOutputFile,
types,
typeDefFile,
newTypeDefString,
{ encoding: 'utf8' }
)
@ -97,7 +102,6 @@ function start (opts) {
)
log.succeed('Done!')
console.log('Remember to copy the generated types into the index.d.ts file')
})
})

View File

@ -88,7 +88,13 @@ function genFactory (folder) {
// serialize the type object
const typesStr = Object.keys(types)
.map(key => `${key}: ${JSON.stringify(types[key], null, 2)}`)
.map(key => {
const line = ` ${key}: ${JSON.stringify(types[key], null, 4)}`
if (line.slice(-1) === '}') {
return line.slice(0, -1) + ' }'
}
return line
})
.join('\n')
// remove useless quotes and commas
.replace(/"/g, '')