Skip to content

Commit

Permalink
feat: add multiple file support
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
scink authored Dec 13, 2018
1 parent acd1c2a commit ca7c164
Show file tree
Hide file tree
Showing 10 changed files with 789 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"tabWidth": 4,
"trailingComma": "all",
"useTabs": true
}
}
27 changes: 16 additions & 11 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@
"@devexperts/utils": "^0.12.6",
"@types/del": "^3.0.1",
"@types/fs-extra": "^5.0.4",
"@types/js-yaml": "^3.11.2",
"@types/prettier": "^1.13.2",
"del": "^3.0.0",
"fp-ts": "^1.10.0",
"fs-extra": "^7.0.0",
"io-ts": "^1.2.1",
"io-ts-types": "^0.4.0",
"prettier": "^1.13.7",
"js-yaml": "^3.12.0",
"prettier": "^1.15.3",
"typescript": "^3.1.3"
},
"devDependencies": {
"@devexperts/lint": "^0.19.1",
"@devexperts/remote-data-ts": "^0.3.1",
"@devexperts/rx-utils": "^0.19.0",
"@types/del": "^3.0.1",
"@types/node": "^10.5.3",
"conventional-changelog-cli": "^2.0.5",
"nodemon": "^1.18.3",
Expand Down
8 changes: 7 additions & 1 deletion src/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { TFileReader } from './index';
import * as yaml from 'js-yaml';

export type TJSON = {
[key: string]: unknown;
};
export type TFileReader = (buffer: Buffer) => TJSON;

export const fromJSON: TFileReader = buffer => JSON.parse(buffer.toString());
export const fromYaml: TFileReader = buffer => yaml.safeLoad(buffer.toString());
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ import { Right } from 'fp-ts/lib/Either';
import { ValidationError } from 'io-ts';
import { serialize } from './language/typescript';
import * as del from 'del';
import { TFileReader } from './fileReader';

const log = console.log.bind(console, '[SWAGGER-CODEGEN-TS]:');

type TJSON = {
[key: string]: unknown;
};

export type TFileReader = (buffer: Buffer) => TJSON;

export type TGenerateOptions = {
/**
* Paths to spec files
Expand Down
16 changes: 13 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
import * as path from 'path';
import { generate } from '../src';
import { serialize } from '../src/language/typescript';
import { fromJSON } from '../src/fileReader';
import { fromJSON, fromYaml } from '../src/fileReader';

const self = path.resolve(__dirname);

generate({
pathsToSpec: [path.resolve(self, './specs/swagger.json'), path.resolve(self, './specs/common.json')],
out: path.resolve(self, './out'),
pathsToSpec: [path.resolve(self, './specs/json/swagger.json'), path.resolve(self, './specs/json/common.json')],
out: path.resolve(self, './out/json'),
serialize,
fileReader: fromJSON,
}).catch(error => {
console.error(error);
process.exit(1);
});

generate({
pathsToSpec: [path.resolve(self, './specs/yaml/swagger.yml'), path.resolve(self, './specs/yaml/common.yml')],
out: path.resolve(self, './out/yaml'),
serialize,
fileReader: fromYaml,
}).catch(error => {
console.error(error);
process.exit(1);
});
File renamed without changes.
File renamed without changes.
127 changes: 127 additions & 0 deletions test/specs/yaml/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
swagger: '2.0'
info:
version: 1.0.0
title: Swagger Petstore
paths: []

definitions:
Order:
type: object
properties:
id:
type: integer
format: int64
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
xml:
name: Order
User:
type: object
properties:
id:
type: integer
format: int64
username:
type: string
firstName:
type: string
lastName:
type: string
email:
type: string
password:
type: string
phone:
type: string
userStatus:
type: integer
format: int32
description: User Status
xml:
name: User
Category:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Category
Tag:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Tag
ApiResponse:
type: object
properties:
code:
type: integer
format: int32
type:
type: string
message:
type: string
Pet:
type: object
required:
- name
- photoUrls
properties:
id:
type: integer
format: int64
category:
"$ref": "#/definitions/Category"
name:
type: string
example: doggie
photoUrls:
type: array
xml:
name: photoUrl
wrapped: true
items:
type: string
tags:
type: array
xml:
name: tag
wrapped: true
items:
"$ref": "#/definitions/Tag"
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: Pet
EmptyObject:
type: object
Loading

0 comments on commit ca7c164

Please sign in to comment.