Skip to content

Commit 40899ea

Browse files
committed
Code cleaning; Fix index generation
1 parent 2f7cf17 commit 40899ea

File tree

14 files changed

+261
-6336
lines changed

14 files changed

+261
-6336
lines changed

.circleci/config.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/index.ts

100644100755
File mode changed.

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ export async function generate({
3939
}: GenerationOptions): Promise<void> {
4040
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
4141
const openApiVersion = getOpenApiVersion(openApi);
42+
const defined_export_options = fixOptions(export_options);
4243
const templates = registerHandlebarTemplates({
4344
httpClient,
4445
useUnionTypes,
4546
useOptions,
47+
exportOptions: defined_export_options,
4648
});
4749

48-
const defined_export_options = fixOptions(export_options);
49-
5050
const clientFinal = getClient(openApiVersion, openApi);
51+
5152
if (write) {
5253
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, defined_export_options, request);
5354
}

src/templates/backendIndex.hbs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{>header}}
2+
3+
{{#each imports}}
4+
{{import_printer this}}
5+
{{/each}}
6+
7+
import { App, Host } from "havas-express";
8+
9+
@Host({
10+
host: 'localhost',
11+
port_number: 3000,
12+
auto_start: false,
13+
})
14+
class MainApp extends App {
15+
16+
}
17+
18+
19+
function startApp () {
20+
const app = new MainApp();
21+
22+
{{#each controller_names}}
23+
const {{variableName this}} = new {{this}}();
24+
{{/each}}
25+
26+
app
27+
{{#each controller_names}}
28+
.append({{variableName this}})
29+
{{/each}};
30+
31+
}

src/utils/ImportBuilder.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
export class ImportBuilder {
3+
4+
/** Will be problematic later on! string-> string[] required; + adding default export */
5+
protected imports: {
6+
name: string,
7+
alias?: string
8+
file_path: string,
9+
}[] = [];
10+
11+
protected included_names = new Map();
12+
13+
static create (): ImportBuilder {
14+
return new ImportBuilder();
15+
}
16+
17+
public get_imports (): ImportBuilder['imports'] {
18+
return this.imports;
19+
}
20+
21+
/**
22+
* Returns the imported name
23+
* @param name
24+
* @param file_path
25+
* @param suffix
26+
*/
27+
add_import (name: string, file_path: string, suffix: string): string {
28+
const base: ImportBuilder['imports'][0] = {
29+
name,
30+
file_path,
31+
};
32+
33+
if (!this.included_names.has(name)) {
34+
this.included_names.set(base.name, undefined);
35+
} else if (this.included_names.has(name) && !this.included_names.has(name+suffix)) {
36+
base.alias = name+suffix;
37+
this.included_names.set(base.alias, undefined);
38+
} else {
39+
// console.log(name+suffix, this);
40+
throw new Error('Unable to create alias ( Already used )');
41+
}
42+
43+
44+
this.imports.push(base);
45+
return base.alias ?? base.name;
46+
}
47+
}

src/utils/registerHandlebarTemplates.ts

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import templateExportModel from '../templates/exportModel.hbs';
1111
import templateExportSchema from '../templates/exportSchema.hbs';
1212
import templateExportService from '../templates/exportService.hbs';
1313
import templateIndex from '../templates/index.hbs';
14+
import backendIndex from '../templates/backendIndex.hbs';
1415

15-
import controllerImports from '../templates/controller_partials/imports.hbs';
16+
// import controllerImports from '../templates/controller_partials/imports.hbs';
1617
import controller_method from '../templates/controller_partials/method.hbs';
1718
import controller_method_comment from '../templates/controller_partials/method_comment.hbs';
1819
import { registerHandlebarHelpers } from './registerHandlebarHelpers';
1920

20-
import { Operation } from "../client/interfaces/Operation";
2121
import { register_partials, register_generic_functions, register_fetch_files, register_xmr_files, register_node_client } from "./template_register_methods";
22+
import { registerCaseGeneration, registerMyHelpers } from "./template_register_methods/registerMyHelpers";
23+
import { ExportOptions } from "../types/default";
2224

2325
export interface Templates {
2426
index: Handlebars.TemplateDelegate;
@@ -42,14 +44,15 @@ export interface Templates {
4244
* Read all the Handlebar templates that we need and return on wrapper object
4345
* so we can easily access the templates in out generator / write functions.
4446
*/
45-
export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOptions: boolean; useUnionTypes: boolean }): Templates {
47+
export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOptions: boolean; useUnionTypes: boolean, exportOptions: ExportOptions }): Templates {
4648
registerHandlebarHelpers(root);
4749
registerMyHelpers();
50+
registerCaseGeneration();
4851

4952
// Main templates (entry points for the files we write to disk)
5053
const templates: Templates = {
5154
index: Handlebars.template(templateIndex),
52-
backend_index: Handlebars.template(templateIndex),
55+
backend_index: Handlebars.template(backendIndex),
5356
exports: {
5457
model: Handlebars.template(templateExportModel),
5558
schema: Handlebars.template(templateExportSchema),
@@ -78,66 +81,6 @@ export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOp
7881
return templates;
7982
}
8083

81-
function registerMyHelpers() {
82-
Handlebars.registerHelper('MethodInput', function (obj: Operation['parameters'][0]): string {
83-
let decorator_name = '';
84-
let property = '';
85-
86-
switch (obj.in) {
87-
case 'path':
88-
decorator_name = 'PathVariable';
89-
property = obj.prop;
90-
break;
91-
92-
93-
case 'query':
94-
decorator_name = 'Query';
95-
property = obj.name;
96-
break;
97-
98-
case 'header':
99-
decorator_name = 'Header';
100-
break;
101-
102-
case 'formData':
103-
case 'body':
104-
decorator_name = 'Body';
105-
property = obj.prop;
106-
break;
107-
108-
case "cookie":
109-
decorator_name = 'Body';
110-
property = obj.prop;
111-
break;
112-
113-
default:
114-
console.log('Whatta', obj.in);
115-
}
116-
117-
return `@${decorator_name}('${property}') ${obj.name}: ${obj.type}`;
118-
});
119-
Handlebars.registerHelper('Capital', function (x: string, fn) {
120-
const head = x.charAt(0).toUpperCase();
121-
const tail = x.slice(1).toLowerCase();
122-
123-
return head + tail;
124-
});
125-
126-
Handlebars.registerHelper('Lower', function (c: any, x: any) {
127-
return x.toLowerCase();
128-
});
129-
130-
Handlebars.registerHelper('Upper', function (c: any, x: any) {
131-
return x.toUpperCase();
132-
});
133-
134-
}
135-
136-
function registerCaseGeneration () {
137-
const case_types = ['snake_case', 'kabob-case', 'PascalCase', 'camelCase', 'UPPER_CAMEL'];
138-
const main_types = ['Class', 'Method', 'MethodVariable', 'Variable', 'Enums', 'EnumVariable']
139-
}
140-
14184
function register_controller_partials () {
14285
// Handlebars.registerPartial('controller_imports', Handlebars.template(controllerImports));
14386
Handlebars.registerPartial('controller_method', Handlebars.template(controller_method));
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import Handlebars from "handlebars/runtime";
2+
import { Operation } from "../../client/interfaces/Operation";
3+
import { ImportBuilder } from "../ImportBuilder";
4+
5+
export function registerMyHelpers() {
6+
Handlebars.registerHelper('MethodInput', function (obj: Operation['parameters'][0]): string {
7+
let decorator_name = '';
8+
let property = '';
9+
10+
switch (obj.in) {
11+
case 'path':
12+
decorator_name = 'PathVariable';
13+
property = obj.prop;
14+
break;
15+
16+
case 'query':
17+
decorator_name = 'Query';
18+
property = obj.name;
19+
break;
20+
21+
case 'header':
22+
decorator_name = 'Header';
23+
break;
24+
25+
case 'formData':
26+
case 'body':
27+
decorator_name = 'Body';
28+
property = obj.prop;
29+
break;
30+
31+
case "cookie":
32+
decorator_name = 'Body';
33+
property = obj.prop;
34+
break;
35+
36+
default:
37+
console.log('Whatta', obj.in);
38+
}
39+
40+
return `@${decorator_name}('${property}') ${obj.name}: ${obj.type}`;
41+
});
42+
Handlebars.registerHelper('Capital', function (x: string, fn) {
43+
const head = x.charAt(0).toUpperCase();
44+
const tail = x.slice(1).toLowerCase();
45+
46+
return head + tail;
47+
});
48+
49+
Handlebars.registerHelper('Lower', function (c: any, x: any) {
50+
return x.toLowerCase();
51+
});
52+
53+
Handlebars.registerHelper('Upper', function (c: any, x: any) {
54+
return x.toUpperCase();
55+
});
56+
57+
Handlebars.registerHelper('import_printer', function (object: ImportBuilder['imports'][0]) {
58+
// console.log('X', object);
59+
60+
return `import { ${object.name} ${object.alias ? `as ${object.alias}` : '' } } from '${object.file_path}';`;
61+
});
62+
63+
}
64+
65+
66+
67+
enum Typings {
68+
snake_case,
69+
camelCase,
70+
PascalCase,
71+
UPPER_CAMEL,
72+
Some_Weird,
73+
cabob,
74+
}
75+
76+
export function registerCaseGeneration () {
77+
const case_types = ['snake_case', 'kabob-case', 'PascalCase', 'camelCase', 'UPPER_CAMEL'];
78+
const main_types = ['Class', 'Method', 'MethodVariable', 'Variable', 'Enums', 'EnumVariable'];
79+
80+
const classTyping = Typings.PascalCase;
81+
const active_values = {
82+
variable: Typings.camelCase,
83+
className: classTyping,
84+
fileNames: classTyping, // Make live easier if same as className
85+
}
86+
87+
Handlebars.registerHelper('variableName', (variable: string) => {
88+
return formatNameDyn(variable, active_values.variable);
89+
});
90+
91+
Handlebars.registerHelper('className', (variable: string) => {
92+
return variable;
93+
});
94+
}
95+
96+
function formatNameDyn (string_like: string, typing: Typings) {
97+
let formatted: string = '';
98+
99+
switch (typing) {
100+
case Typings.snake_case:
101+
case Typings.camelCase:
102+
default:
103+
formatted = `__${string_like}`;
104+
}
105+
106+
return formatted;
107+
}

src/utils/writeBackendControllers.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Controller } from "../client/interfaces/Client";
2+
import { Templates } from "./registerHandlebarTemplates";
3+
import { HttpClient } from "../HttpClient";
4+
import { resolve } from "path";
5+
import { writeFile } from "./fileSystem";
6+
import { format } from "./format";
7+
import { VERSION_TEMPLATE_STRING } from "./writeClientServices";
8+
9+
export async function writeBackendControllers (controllers: Controller[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise<void> {
10+
11+
if (templates.exports.controllers === undefined) {
12+
throw new Error('Controller template is not defined!');
13+
}
14+
15+
for (const controller of controllers) {
16+
const file = resolve(outputPath, `${controller.name}.ts`);
17+
const useVersion = controller.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));
18+
19+
const templateResult = templates.exports.controllers({
20+
...controller,
21+
httpClient,
22+
useUnionTypes,
23+
useVersion,
24+
useOptions,
25+
});
26+
27+
await writeFile(file, format(templateResult));
28+
}
29+
}

0 commit comments

Comments
 (0)