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

Commit

Permalink
Add more ui
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTsk committed Jan 12, 2019
1 parent b2ec8c7 commit 154659f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
16 changes: 8 additions & 8 deletions cli/commands/openapi.generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ export default function (program: CommanderStatic) {
.option('-s, --sources <path>', 'set sources path', )
.option('-e, --entry <path>', 'set entry file path')
.option('-o, --output <path>', 'set output path ')
.option('-l, --links [urls...]', 'set server links', (val, memo) => [...memo, val], [])
.option('-l, --link [urls]', 'set server links', (val, memo) => [...memo, val], [])
.option('-t, --title <title>', 'set app titile', 'Docs')
.description('Generate API docs')
.action(({ raw, sources, entry, output, title, links }) => {
console.log(links);
if(!sources)
.action(options => {

if(!options.sources)
return console.log('Sources must be specified');

action({ sources, output, entry: entry || join(sources, './index.ts'), title, links });
action(options);
});
}

function action({ sources, entry, output, links, title }: any) {
const doc = generateOpenAPI(process.cwd(), sources, entry);
function action({ sources, entry, output, link, title }: any) {
const doc = generateOpenAPI(process.cwd(), sources, (entry || join(sources, './index.ts')));

doc.info.title = title;
doc.servers = links.map((link: string) => ({ url: link }));
doc.servers = link.map((link: string) => ({ url: link }));

return writeFileSync(resolve(process.cwd(), output || `./swagger-${doc.info.version}.json`), JSON.stringify(doc, null, 4));
}
24 changes: 21 additions & 3 deletions cli/generators/openapi.generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import "reflect-metadata";
import * as ora from 'ora';

//@ts-ignore
import * as spinner from 'cli-spinners';

import { OpenAPIV3 } from 'openapi-types';

Expand All @@ -12,9 +16,9 @@ import { getFunctionArgs } from "../../src/utils/reflection/function.reflection"
import { concatinateBase } from "../../src/utils/url.utils";
import { remapPath, injectTsFiles } from '../utils';
import { Constructor } from '../types';
import { resolve, relative } from 'path';
import { resolve } from 'path';
import { getProgram, extractReturnType } from '../ast/parser';
import { ClassDeclaration, MethodDeclaration, JSDoc } from 'ts-simple-ast';
import { ClassDeclaration, MethodDeclaration, JSDoc, ts } from 'ts-simple-ast';
import chalk from "chalk";

type HandlerDescriptor = OpenAPIV3.OperationObject & { path: string, method: string };
Expand Down Expand Up @@ -168,7 +172,14 @@ function processController(controller: Constructor<IController>, classAST: Class
}

export function generateOpenAPI(base: string, sources: string, rootFile: string, ) {
//Wrap with loader
const jsLoader = ora({ text: 'Improting dependencies', spinner: spinner['dots'] }).start();

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 @@ -180,6 +191,9 @@ export function generateOpenAPI(base: string, sources: string, rootFile: string,
paths: {}
};

//Wrap with loader
const tsLoader = ora({ text: 'Processing controllers', spinner: spinner['dots'] }).start();

for (const { classType, jsPath, tsPath } of controllers) {
const file = getProgram().addExistingSourceFile(tsPath);
const classAST = file.getClass(classType.name);
Expand All @@ -198,8 +212,12 @@ export function generateOpenAPI(base: string, sources: string, rootFile: string,
};
});

console.log(chalk`{green ✔} {gray ${'['}}${classType.name}{gray ] successfully parsed}`);
tsLoader.text = `${classType} succesfully processed`;
}

//Finish
tsLoader.succeed('Controllers successfully processed');

console.log(chalk`{green Done !}`);
return document;
}
49 changes: 48 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"fastify-cookie": "^2.1.5",
"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 154659f

Please sign in to comment.