UNPKG

@asyncapi/nodejs-ws-template

Version:

Node.js WebSockets template for AsyncAPI generator.

34 lines (25 loc) 746 B
const fs = require('fs'); const path = require('path'); const { Parser } = require('@asyncapi/parser'); const parser = new Parser(); let cached; module.exports.init = async () => { if (cached) return; let content; try { content = fs.readFileSync(path.resolve(__dirname, '../../asyncapi.yaml'), { encoding: 'utf8' }); } catch (e) { try { content = fs.readFileSync(path.resolve(__dirname, '../../asyncapi.json'), { encoding: 'utf8' }); } catch (err) { throw new Error('Coud not find asyncapi.yaml or asyncapi.json file in the root directory of the project.'); } } try { cached = await parser.parse(content); } catch (e) { throw e; } return cached; }; module.exports.get = () => cached;