From a31d964d19950a530905bf261c2a7b769ccf43f4 Mon Sep 17 00:00:00 2001 From: bedi gupta Date: Thu, 3 Oct 2024 17:19:35 +0530 Subject: [PATCH 01/14] refactor: improve generate function structure --- apps/generator/cli.js | 246 +++++++++++++++++++++++++++--------------- 1 file changed, 162 insertions(+), 84 deletions(-) diff --git a/apps/generator/cli.js b/apps/generator/cli.js index feb12446a..437dfb920 100755 --- a/apps/generator/cli.js +++ b/apps/generator/cli.js @@ -1,19 +1,19 @@ #!/usr/bin/env node -const path = require('path'); -const os = require('os'); -const program = require('commander'); -const xfs = require('fs.extra'); -const { DiagnosticSeverity } = require('@asyncapi/parser/cjs'); -const packageInfo = require('./package.json'); -const Generator = require('./lib/generator'); -const Watcher = require('./lib/watcher'); -const { isLocalTemplate, isFilePath } = require('./lib/utils'); - -const red = text => `\x1b[31m${text}\x1b[0m`; -const magenta = text => `\x1b[35m${text}\x1b[0m`; -const yellow = text => `\x1b[33m${text}\x1b[0m`; -const green = text => `\x1b[32m${text}\x1b[0m`; +const path = require("path"); +const os = require("os"); +const program = require("commander"); +const xfs = require("fs.extra"); +const { DiagnosticSeverity } = require("@asyncapi/parser/cjs"); +const packageInfo = require("./package.json"); +const Generator = require("./lib/generator"); +const Watcher = require("./lib/watcher"); +const { isLocalTemplate, isFilePath } = require("./lib/utils"); + +const red = (text) => `\x1b[31m${text}\x1b[0m`; +const magenta = (text) => `\x1b[35m${text}\x1b[0m`; +const yellow = (text) => `\x1b[33m${text}\x1b[0m`; +const green = (text) => `\x1b[32m${text}\x1b[0m`; let asyncapiDocPath; let template; @@ -22,20 +22,26 @@ const noOverwriteGlobs = []; const disabledHooks = {}; const mapBaseUrlToFolder = {}; -const parseOutput = dir => path.resolve(dir); +const parseOutput = (dir) => path.resolve(dir); -const paramParser = v => { - if (!v.includes('=')) throw new Error(`Invalid param ${v}. It must be in the format of --param name=value.`); +const paramParser = (v) => { + if (!v.includes("=")) + throw new Error( + `Invalid param ${v}. It must be in the format of --param name=value.` + ); const [paramName, paramValue] = v.split(/=(.+)/, 2); params[paramName] = paramValue; return v; }; -const noOverwriteParser = v => noOverwriteGlobs.push(v); +const noOverwriteParser = (v) => noOverwriteGlobs.push(v); -const disableHooksParser = v => { +const disableHooksParser = (v) => { const [hookType, hookNames] = v.split(/=/); - if (!hookType) throw new Error('Invalid --disable-hook flag. It must be in the format of: --disable-hook or --disable-hook =,,...'); + if (!hookType) + throw new Error( + "Invalid --disable-hook flag. It must be in the format of: --disable-hook or --disable-hook =,,..." + ); if (hookNames) { disabledHooks[hookType] = hookNames.split(/,/); } else { @@ -43,63 +49,101 @@ const disableHooksParser = v => { } }; -const mapBaseUrlParser = v => { +const mapBaseUrlParser = (v) => { // Example value for regular expression: https://schema.example.com/crm/:./test/docs/ // it splits on last occurrence of : into the groups all, url and folder const re = /(.*):(.*)/g; let mapping = []; - if ((mapping = re.exec(v))===null || mapping.length!==3) { - throw new Error('Invalid --map-base-url flag. A mapping : with delimiter : expected.'); + if ((mapping = re.exec(v)) === null || mapping.length !== 3) { + throw new Error( + "Invalid --map-base-url flag. A mapping : with delimiter : expected." + ); } // Folder is without trailing slash, so make sure that url has also no trailing slash: - mapBaseUrlToFolder.url = mapping[1].replace(/\/$/, ''); + mapBaseUrlToFolder.url = mapping[1].replace(/\/$/, ""); mapBaseUrlToFolder.folder = path.resolve(mapping[2]); const isURL = /^https?:/; if (!isURL.test(mapBaseUrlToFolder.url.toLowerCase())) { - throw new Error('Invalid --map-base-url flag. The mapping : requires a valid http/https url and valid folder with delimiter `:`.'); + throw new Error( + "Invalid --map-base-url flag. The mapping : requires a valid http/https url and valid folder with delimiter `:`." + ); } }; -const showError = err => { - console.error(red('Something went wrong:')); +const showError = (err) => { + console.error(red("Something went wrong:")); console.error(red(err.stack || err.message)); if (err.diagnostics) { - const errorDiagnostics = err.diagnostics.filter(diagnostic => diagnostic.severity === DiagnosticSeverity.Error); - console.error(red(`Errors:\n${JSON.stringify(errorDiagnostics, undefined, 2)}`)); + const errorDiagnostics = err.diagnostics.filter( + (diagnostic) => diagnostic.severity === DiagnosticSeverity.Error + ); + console.error( + red(`Errors:\n${JSON.stringify(errorDiagnostics, undefined, 2)}`) + ); } }; -const showErrorAndExit = err => { +const showErrorAndExit = (err) => { showError(err); process.exit(1); }; program .version(packageInfo.version) - .arguments('