Skip to content

Instantly share code, notes, and snippets.

@tscpp
Last active November 9, 2020 11:23
Show Gist options
  • Save tscpp/c029f76df7adc035743ff0f79d8cc2d1 to your computer and use it in GitHub Desktop.
Save tscpp/c029f76df7adc035743ff0f79d8cc2d1 to your computer and use it in GitHub Desktop.
import NSwag from 'nswagjs'
import * as path from 'path'
import * as protoc_swagger_gen from '@accility/protoc-swagger-plugin'
import * as protoc_tools from '@accility/protoc-tools'
import * as protoc_apis from 'google-proto-files'
import * as fs from 'fs'
const PATH2GENERATED = path.join(__dirname, 'generated')
const PATH2PROTO = path.join(__dirname, 'protos')
const PATH2NSWAG = path.join(__dirname, 'node_modules/nswag')
const PATH2CONFIG = path.join(__dirname, 'nswag.json')
const nswag = new NSwag(PATH2NSWAG, NSwag.getCoreVersion() ?? 'Win x86');
(async () => {
const promises: Promise<unknown>[] = []
if (!fs.existsSync(PATH2GENERATED))
fs.mkdirSync(PATH2GENERATED)
const names: string[] = []
for (const file of fs.readdirSync(PATH2PROTO)) {
names.push(file.replace(/\.[^.]+?$/, ''))
await protoc_tools.protoc({
includeDirs: [
path.resolve(protoc_apis.getProtoPath(), '..').replace(/\\/g, '/'),
PATH2PROTO.replace(/\\/g, '/')
],
files: [path.join(PATH2PROTO, file).replace(/\\/g, '/')],
outDir: PATH2GENERATED,
outOptions: [
protoc_swagger_gen.createSwaggerOptions({ outOptions: 'logtostderr=true' }),
protoc_tools.generators.js()
]
})
}
for (const name of names) {
const input = path.join(PATH2GENERATED, name + '.swagger.json')
const output = path.join(PATH2GENERATED, name + '.ts')
let _promise: Promise<unknown> = nswag.run(PATH2CONFIG, {
input: { url: input },
outputs: {
openApiToTypeScriptClient: output
}
}).then(data => {
let match: RegExpExecArray
while ((match = /(.+?):[0-9]+?:[0-9]+? (.+)/g.exec(data) as RegExpExecArray) != null)
console.log(match)
_promise = Promise.all([_promise, nswag.openapi2tsclient(input, output)])
})
promises.push(_promise)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment