Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Add progress bar and update version
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTsk committed Jan 12, 2019
1 parent 154659f commit 6c9a5d0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 86 deletions.
4 changes: 2 additions & 2 deletions cli/commands/openapi.generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down
66 changes: 33 additions & 33 deletions cli/generators/openapi.generator.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -157,29 +154,26 @@ function processMethod(controller: typeof IController, handler: string, methodAS
return descriptor;
}

function processController(controller: Constructor<IController>, classAST: ClassDeclaration): HandlerDescriptor[] {
const routePrefix = metadata(controller).getMetadata(CONTROLLER);
const routeMethods = reflectClassMethods(controller).filter(method => isRouteHandler(controller.prototype, method));
async function processController(controller: Constructor<IController>, classAST: ClassDeclaration): Promise<HandlerDescriptor[]> {
return new Promise<any>(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 = {
Expand All @@ -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);
Expand All @@ -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;
}
}

121 changes: 72 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down

0 comments on commit 6c9a5d0

Please sign in to comment.