From 6c9a5d06bfd80c7f304b8cc8881059d52c698ae5 Mon Sep 17 00:00:00 2001 From: DanTsk Date: Sat, 12 Jan 2019 22:43:48 +0200 Subject: [PATCH] Add progress bar and update version --- cli/commands/openapi.generate.ts | 4 +- cli/generators/openapi.generator.ts | 66 +++++++-------- package-lock.json | 121 +++++++++++++++++----------- package.json | 4 +- 4 files changed, 109 insertions(+), 86 deletions(-) diff --git a/cli/commands/openapi.generate.ts b/cli/commands/openapi.generate.ts index 031d4a58..a28d4635 100644 --- a/cli/commands/openapi.generate.ts +++ b/cli/commands/openapi.generate.ts @@ -22,8 +22,8 @@ export default function (program: CommanderStatic) { }); } -function action({ sources, entry, output, link, title }: any) { - const doc = generateOpenAPI(process.cwd(), sources, (entry || join(sources, './index.ts'))); +async function action({ sources, entry, output, link, title }: any) { + const doc = await generateOpenAPI(process.cwd(), sources, (entry || join(sources, './index.ts'))); doc.info.title = title; doc.servers = link.map((link: string) => ({ url: link })); diff --git a/cli/generators/openapi.generator.ts b/cli/generators/openapi.generator.ts index f17364b6..92f5f46f 100644 --- a/cli/generators/openapi.generator.ts +++ b/cli/generators/openapi.generator.ts @@ -1,8 +1,5 @@ import "reflect-metadata"; -import * as ora from 'ora'; - -//@ts-ignore -import * as spinner from 'cli-spinners'; +const Gauge = require("gauge"); import { OpenAPIV3 } from 'openapi-types'; @@ -157,29 +154,26 @@ function processMethod(controller: typeof IController, handler: string, methodAS return descriptor; } -function processController(controller: Constructor, classAST: ClassDeclaration): HandlerDescriptor[] { - const routePrefix = metadata(controller).getMetadata(CONTROLLER); - const routeMethods = reflectClassMethods(controller).filter(method => isRouteHandler(controller.prototype, method)); +async function processController(controller: Constructor, classAST: ClassDeclaration): Promise { + return new Promise(res => setTimeout(async () => { + const routePrefix = metadata(controller).getMetadata(CONTROLLER); + const routeMethods = reflectClassMethods(controller).filter(method => isRouteHandler(controller.prototype, method)); - return routeMethods.map(method => { - const methodAST = classAST.getMethodOrThrow(method); + const result = routeMethods.map(method => { + const methodAST = classAST.getMethodOrThrow(method); - const descriptor = processMethod(controller, method, methodAST); - descriptor.path = concatinateBase(routePrefix.path, descriptor.path); + const descriptor = processMethod(controller, method, methodAST); + descriptor.path = concatinateBase(routePrefix.path, descriptor.path); - return descriptor; - }); -} + return descriptor; + }); -export function generateOpenAPI(base: string, sources: string, rootFile: string, ) { - //Wrap with loader - const jsLoader = ora({ text: 'Improting dependencies', spinner: spinner['dots'] }).start(); + res(result); + }, 200)); +} +export async function generateOpenAPI(base: string, sources: string, rootFile: string, ) { const controllers = readControllers(base, [resolve(base, sources), `!${resolve(base, rootFile)}`]); - - jsLoader.succeed('All dependencies improted'); - //Finish - const { version } = require(resolve(process.cwd(), './package.json')); const document: OpenAPIV3.Document = { @@ -191,17 +185,26 @@ export function generateOpenAPI(base: string, sources: string, rootFile: string, paths: {} }; - //Wrap with loader - const tsLoader = ora({ text: 'Processing controllers', spinner: spinner['dots'] }).start(); + + let gt = new Gauge(process.stdout, { + updateInterval: 50, + theme: 'colorASCII' + }); + + let progress = 0; + gt.show('Start processing...', progress); + + for (const [i, { classType, jsPath, tsPath }] of controllers.entries()) { - for (const { classType, jsPath, tsPath } of controllers) { const file = getProgram().addExistingSourceFile(tsPath); const classAST = file.getClass(classType.name); + gt.show(chalk.yellow('Processing: ') + classType.name, (100 / controllers.length * (i + 1))/ 100); + if (!classAST) throw new Error(`Can't find class - ${classType.name} in ts file - ${tsPath}`); - const handlers = processController(classType, classAST); + const handlers = await processController(classType, classAST); handlers.forEach(({ path, method, ...descriptor }) => { const route = remapPath(path); @@ -211,13 +214,10 @@ export function generateOpenAPI(base: string, sources: string, rootFile: string, [method]: descriptor }; }); + } + gt.hide(); - tsLoader.text = `${classType} succesfully processed`; - } - - //Finish - tsLoader.succeed('Controllers successfully processed'); - - console.log(chalk`{green Done !}`); + console.log(chalk`{green ✔ Done!}`); return document; -} \ No newline at end of file +} + diff --git a/package-lock.json b/package-lock.json index 95513be9..c439ec9b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -601,6 +601,11 @@ "integrity": "sha1-mL9lmTJ+zqGZMJhm6BQDaP0uZGo=", "optional": true }, + "aproba": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/aproba/-/aproba-1.2.0.tgz", + "integrity": "sha512-Y9J6ZjXtoYh8RnXVCMOU/ttDmk1aBjunq9vO0ta5x85WDQiQfUF9sIPBITdbiiIVcBo03Hi3jMxigBtsddlXRw==" + }, "argparse": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", @@ -1089,11 +1094,6 @@ } } }, - "cli-spinners": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-1.3.1.tgz", - "integrity": "sha512-1QL4544moEsDVH9T/l6Cemov/37iv1RtoKf7NJ04A60+4MREXNfx/QvavbH6QoGdsD4N4Mwy49cmaINR/o2mdg==" - }, "cli-table3": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/cli-table3/-/cli-table3-0.5.1.tgz", @@ -1120,11 +1120,6 @@ "wrap-ansi": "^2.0.0" } }, - "clone": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", - "integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4=" - }, "cls-hooked": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/cls-hooked/-/cls-hooked-4.2.2.tgz", @@ -1207,6 +1202,11 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, + "console-control-strings": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", + "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=" + }, "cookie": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", @@ -1289,14 +1289,6 @@ "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-3.0.0.tgz", "integrity": "sha512-a8z8bkgHsAML+uHLqmMS83HHlpy3PvZOOuiTQqaa3wu8ZVg3h0hqHk6aCsGdOnZV2XMM/FRimNGjUh0KCcmHBw==" }, - "defaults": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.3.tgz", - "integrity": "sha1-xlYFHpgX2f8I7YgUd/P+QBnz730=", - "requires": { - "clone": "^1.0.2" - } - }, "define-property": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/define-property/-/define-property-2.0.2.tgz", @@ -1843,6 +1835,54 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "gauge": { + "version": "2.7.4", + "resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz", + "integrity": "sha1-LANAXHU4w51+s3sxcCLjJfsBi/c=", + "requires": { + "aproba": "^1.0.3", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.0", + "object-assign": "^4.1.0", + "signal-exit": "^3.0.0", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wide-align": "^1.1.0" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "^2.0.0" + } + } + } + }, "get-caller-file": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", @@ -2001,6 +2041,11 @@ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, + "has-unicode": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", + "integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=" + }, "has-value": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/has-value/-/has-value-1.0.0.tgz", @@ -2581,14 +2626,6 @@ "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=", "optional": true }, - "log-symbols": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.2.0.tgz", - "integrity": "sha512-VeIAFslyIerEJLXHziedo2basKbMKtTw3vfn5IzG0XTjhAVEJyNHnL2p7vc+wBDSdQuUpNw3M2u6xb9QsAY5Eg==", - "requires": { - "chalk": "^2.0.1" - } - }, "loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", @@ -4068,8 +4105,7 @@ "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", - "dev": true + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" }, "object-component": { "version": "0.0.3", @@ -4181,19 +4217,6 @@ "wordwrap": "~1.0.0" } }, - "ora": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/ora/-/ora-3.0.0.tgz", - "integrity": "sha512-LBS97LFe2RV6GJmXBi6OKcETKyklHNMV0xw7BtsVn2MlsgsydyZetSCbCANr+PFLmDyv4KV88nn0eCKza665Mg==", - "requires": { - "chalk": "^2.3.1", - "cli-cursor": "^2.1.0", - "cli-spinners": "^1.1.0", - "log-symbols": "^2.2.0", - "strip-ansi": "^4.0.0", - "wcwidth": "^1.0.1" - } - }, "os-locale": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-2.1.0.tgz", @@ -5561,14 +5584,6 @@ "integrity": "sha1-9GV3F2JLDkuIrzb5jYlYmlu+5Wk=", "dev": true }, - "wcwidth": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", - "integrity": "sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=", - "requires": { - "defaults": "^1.0.3" - } - }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", @@ -5582,6 +5597,14 @@ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=" }, + "wide-align": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", + "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", + "requires": { + "string-width": "^1.0.2 || 2" + } + }, "wordwrap": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", diff --git a/package.json b/package.json index bda3e284..87298935 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "odi", - "version": "0.2.26", + "version": "0.3.0", "description": "🌪🌌 Opinionated, Declarative, Idiomatic framework for building scalable, supportable, enterprise-grade applications.", "main": "lib/src/index.js", "homepage": "https://github.com/Odi-ts/Odi#readme", @@ -66,9 +66,9 @@ "esprima": "^4.0.1", "fastify": "^1.13.3", "fastify-cookie": "^2.1.5", + "gauge": "^2.7.4", "globby": "^8.0.2", "inquirer": "^6.2.1", - "ora": "^3.0.0", "reflect-metadata": "^0.1.12", "ts-simple-ast": "^20.0.0" },