Skip to content

Commit

Permalink
feat(tags): clients generated to folder services when tag option used
Browse files Browse the repository at this point in the history
BREAKING CHANGE: models import path had changed as services now share the models folder

Signed-off-by: Vojtech Mašek <[email protected]>
  • Loading branch information
vmasek committed Aug 24, 2018
1 parent 5276d65 commit e7d3a08
Show file tree
Hide file tree
Showing 201 changed files with 11,311 additions and 9,265 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"gen-custom": "rimraf ./tests/custom/api && ts-node --files ./src/main.ts -s ./tests/custom/swagger.yaml -o ./tests/custom/api",
"gen-esquare": "rimraf ./tests/esquare/api && ts-node --files ./src/main.ts -s ./tests/esquare/swagger.yaml -o ./tests/esquare/api",
"gen-gcloud-firestore": "rimraf ./tests/gcloud-firestore/api && ts-node --files ./src/main.ts -s ./tests/gcloud-firestore/swagger.yaml -o ./tests/gcloud-firestore/api",
"gen-github": "rimraf ./tests/github/api && ts-node --files ./src/main.ts -s ./tests/github/swagger.yaml -o ./tests/github/api",
"gen-github-tags": "rimraf ./tests/github/api-tags && ts-node --files ./src/main.ts -s ./tests/github/swagger.yaml -o ./tests/github/api-tags -t emojis",
"gen-github": "rimraf ./tests/github/api && ts-node --files ./src/main.ts -s ./tests/github/swagger.yaml -o ./tests/github/api -t all",
"gen-filtered-api": "rimraf ./tests/filtered-api/api && ts-node --files ./src/main.ts -s ./tests/filtered-api/swagger.json -o ./tests/filtered-api/api -t DummySelector,Project,NonExistingTag -m",
"gen-with-all-tags": "rimraf ./tests/with-all-tags/api && ts-node --files ./src/main.ts -s ./tests/with-all-tags/swagger.json -o ./tests/with-all-tags/api -t all",
"release": "npm run tests && npm run build && standard-version --tag-prefix=''",
Expand Down
2 changes: 1 addition & 1 deletion src/diff-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Diff compare', () => {
});

it('should check with [ GitHub ] reference', async () => {
const reference = new TestReference('github');
const reference = new TestReference('github', 'yaml', false, 'all');
expect(await compareWithReference(reference)).toBeNull();
});

Expand Down
72 changes: 42 additions & 30 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, mkdir, readFile, writeFile } from 'fs';
import { createWriteStream, exists, existsSync, mkdir, readFile, writeFile, WriteStream } from 'fs';
import { ensureDir } from 'fs-extra';
import * as Mustache from 'mustache';
import { dirname, join } from 'path';
Expand Down Expand Up @@ -34,11 +34,28 @@ export async function generateAPIClient(options: GenOptions): Promise<string[]>
const swaggerDef = await swaggerFile(swaggerFilePath);
const allTags = getAllSwaggerTags(swaggerDef);
const specifiedTags = options.splitPathTags || [];
const usedTags: (string | undefined)[] = specifiedTags.length === 0 ?
[undefined] :
specifiedTags[0] === ALL_TAGS_OPTION ?
allTags :
specifiedTags;
const usedTags: (string | undefined)[] = specifiedTags.length === 0
? [undefined]
: specifiedTags[0] === ALL_TAGS_OPTION
? allTags
: specifiedTags;

const modelsOutputDir = join(options.outputPath, MODEL_DIR_NAME);

const modelTemplate = (await promisify(readFile)(`${__dirname}/../templates/ngx-model.mustache`)).toString();
const modelExportTemplate = (await promisify(readFile)(`${__dirname}/../templates/ngx-models-export.mustache`)).toString();

if (!(await promisify(exists)(options.outputPath))) {
await promisify(mkdir)(options.outputPath);
}

if (!(await promisify(exists)(modelsOutputDir))) {
await promisify(mkdir)(modelsOutputDir);
}

await promisify(writeFile)(`${options.outputPath}/models/index.ts`, '/* tslint:disable */\n\n', 'utf-8');
const modelsIndexFileStream = createWriteStream(`${options.outputPath}/models/index.ts`, {flags: 'a'});


return flattenAll(
usedTags.map(async tag => {
Expand All @@ -49,20 +66,20 @@ export async function generateAPIClient(options: GenOptions): Promise<string[]>
return [];
}

const subFolder = usedTags.length > 1 ? dashCase(tag) : '';
const outputPath = join(options.outputPath, subFolder);
const subFolder = usedTags && usedTags[0] ? `services/${dashCase(tag)}` : '';
const clientOutputPath = join(options.outputPath, subFolder);

if (!existsSync(outputPath)) {
await ensureDir(outputPath);
if (!existsSync(clientOutputPath)) {
await ensureDir(clientOutputPath);
}

return flattenAll([
generateClient(mustacheData, outputPath),
generateClientInterface(mustacheData, outputPath),
generateModels(mustacheData, outputPath),
...!options.skipModuleExport ?
[generateModuleExportIndex(mustacheData, outputPath)] :
[],
generateClient(mustacheData, clientOutputPath),
generateClientInterface(mustacheData, clientOutputPath),
generateModels(mustacheData, modelsOutputDir, modelsIndexFileStream, modelTemplate, modelExportTemplate),
...!options.skipModuleExport
? [generateModuleExportIndex(mustacheData, clientOutputPath)]
: [],
]);
})
);
Expand All @@ -87,23 +104,18 @@ async function generateClientInterface(viewContext: MustacheData, outputPath: st
return [outfile];
}

async function generateModels(viewContext: MustacheData, outputPath: string): Promise<string[]> {
const outputDir = join(outputPath, MODEL_DIR_NAME);
const outIndexFile = join(outputDir, '/index.ts');

const modelTemplate = (await promisify(readFile)(`${__dirname}/../templates/ngx-model.mustache`)).toString();
const modelExportTemplate = (await promisify(readFile)(`${__dirname}/../templates/ngx-models-export.mustache`)).toString();

if (!existsSync(outputDir)) {
await promisify(mkdir)(outputDir);
}

// generate model export index here
await promisify(writeFile)(outIndexFile, Mustache.render(modelExportTemplate, viewContext), 'utf-8');
async function generateModels(
viewContext: MustacheData,
outputDir: string,
exportIndexStream: WriteStream,
modelTemplate: string,
modelExportTemplate: string,
): Promise<string[]> {
// write the model exports to export index stream
exportIndexStream.write(Mustache.render(modelExportTemplate, viewContext));

// generate API models
return Promise.all([
outIndexFile,
...viewContext.definitions.map(async (definition) => {
const result = Mustache.render(modelTemplate, definition);
const outfile = join(outputDir, `${fileName(definition.name, definition.isEnum ? 'enum' : 'model')}.ts`);
Expand Down
8 changes: 5 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function createMustacheViewModel(swagger: Swagger, swaggerTag?: string):
return {
isSecure: !!swagger.securityDefinitions,
swagger: swagger,
swaggerTag,
domain: determineDomain(swagger),
methods: methods,
definitions: parseDefinitions(swagger.definitions, swagger.parameters, swaggerTag ? methods : undefined),
Expand Down Expand Up @@ -276,15 +277,16 @@ function defineInterface(schema: Schema, definitionKey: string): Definition {
}

function determineResponseType(responses: { [responseName: string]: Response }): ResponseType {
const okResponse = responses['200'];
const okResponse = responses['200'] || responses['201'];

if (okResponse == null) { // TODO: check non-200 response codes
logWarn('200 response not specified; `any` will be used');
logWarn('200 or 201 response not specified; `any` will be used');
return {name: 'any', type: 'any'};
}

const {schema} = okResponse;
if (schema == null) {
logWarn('200 response schema not specified; `any` will be used');
logWarn('200 or 201 response schema not specified; `any` will be used');
return {name: 'any', type: 'any'};
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Definition {
export interface MustacheData {
readonly isSecure: boolean;
readonly swagger: Swagger;
readonly swaggerTag?: string;
readonly domain: string;
readonly methods: Method[];
readonly definitions: Definition[];
Expand Down
2 changes: 1 addition & 1 deletion templates/ngx-model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
{{#imports}}
{{.}},
{{/imports}}
} from './..';
} from '.';
{{/imports.length}}

{{#description}}
Expand Down
2 changes: 0 additions & 2 deletions templates/ngx-models-export.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* tslint:disable */

{{#definitions}}
export { {{name}} } from './{{#renderFileName}}{{name}}{{/renderFileName}}';
{{/definitions}}
4 changes: 0 additions & 4 deletions templates/ngx-module-export.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { {{&serviceName}}, USE_DOMAIN, USE_HTTP_OPTIONS } from './{{&serviceFileName}}';

{{#definitions.length}}
export * from './models';
{{/definitions.length}}

export { {{&serviceName}} } from './{{&serviceFileName}}';
export { {{&interfaceName}} } from './{{&interfaceFileName}}';

Expand Down
2 changes: 1 addition & 1 deletion templates/ngx-service-interface.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Observable } from 'rxjs';
import { HttpOptions } from './';
import * as models from './models';
import * as models from '.{{#swaggerTag}}./..{{/swaggerTag}}/models';

export interface {{&interfaceName}} {
Expand Down
2 changes: 1 addition & 1 deletion templates/ngx-service.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Observable, throwError } from 'rxjs';
import { DefaultHttpOptions, HttpOptions, {{&interfaceName}} } from './';

{{#definitions.length}}
import * as models from './models';
import * as models from '.{{#swaggerTag}}./..{{/swaggerTag}}/models';
{{/definitions.length}}

export const USE_DOMAIN = new InjectionToken<string>('{{&serviceName}}_USE_DOMAIN');
Expand Down
2 changes: 0 additions & 2 deletions tests/custom/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { APIClient, USE_DOMAIN, USE_HTTP_OPTIONS } from './api-client.service';

export * from './models';

export { APIClient } from './api-client.service';
export { APIClientInterface } from './api-client.interface';

Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/cat.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Mouse,
Pet,
} from './..';
} from '.';

export interface Cat extends Pet {
age: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/customer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Model,
Right,
} from './..';
} from '.';

export interface Customer extends Model {
address: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/dog.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Pet,
} from './..';
} from '.';

export interface Dog extends Pet {
bark: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/item-list.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Data,
} from './..';
} from '.';

/**
* List of items,
Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/item-model-list.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
DataModel,
} from './..';
} from '.';

/**
* List of Item models
Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/mouse.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Pet,
} from './..';
} from '.';

export interface Mouse extends Pet {
color: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/custom/api/models/my-interface.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Data,
} from './..';
} from '.';

export interface MyInterface {
myId: string;
Expand Down
2 changes: 0 additions & 2 deletions tests/esquare/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { APIClient, USE_DOMAIN, USE_HTTP_OPTIONS } from './api-client.service';

export * from './models';

export { APIClient } from './api-client.service';
export { APIClientInterface } from './api-client.interface';

Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/import-history-item.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
ImportType,
Status,
} from './..';
} from '.';

export interface ImportHistoryItem {
datetime: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/import-stats-group.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
ImportStats,
} from './..';
} from '.';

export interface ImportStatsGroup {
imported: ImportStats[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Status,
} from './..';
} from '.';

export interface ImportStatusDetailsItem {
id: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/import-status-item.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Criticality,
ImportStatusDetailsItem,
} from './..';
} from '.';

export interface ImportStatusItem {
criticality: Criticality;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/issue.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
IssueAlertType,
} from './..';
} from '.';

export interface Issue {
alert: IssueAlertType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
NotificationEditable,
} from './..';
} from '.';

export interface NotificationEditableListItem extends NotificationEditable {
enabled: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/notification-editable.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Criticality,
Frequency,
} from './..';
} from '.';

export interface NotificationEditable {
description: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/notification-list-item.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Criticality,
} from './..';
} from '.';

export interface NotificationListItem {
date: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/orderable-table.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Order,
Table,
} from './..';
} from '.';

export interface OrderableTable extends Table {
order: Order;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/paged-table.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable */
import {
Table,
} from './..';
} from '.';

export interface PagedTable extends Table {
totalRows: number;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/report-item.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
ReportListItem,
ReportTemplateGroup,
} from './..';
} from '.';

export interface ReportItem extends ReportListItem {
description: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/esquare/api/models/report-list-item.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
Criticality,
Status,
} from './..';
} from '.';

export interface ReportListItem {
criticality: Criticality;
Expand Down
Loading

0 comments on commit e7d3a08

Please sign in to comment.