-
Notifications
You must be signed in to change notification settings - Fork 17
feat: adding a simple CLI #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kokovtsev
wants to merge
6
commits into
master
Choose a base branch
from
feature/cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0306375
feat: adding a simple CLI
kokovtsev 9863318
chore: fixing the build, removing mentions of travis
kokovtsev 4911d6c
chore: publishing scripts
kokovtsev f67dd52
Merge remote-tracking branch 'origin/master' into feature/cli
kokovtsev c906084
feat: CLI updates
kokovtsev ce80bcb
chore: dedupe dependencies
kokovtsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,5 @@ | |
/tslint.json | ||
/.eslintignore | ||
/.eslintrc.json | ||
/.travis.yml | ||
/tsconfig.build.json | ||
/tsconfig.test.json |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#!/usr/bin/env node | ||
const yargs = require('yargs'); // eslint-disable-line | ||
const { hideBin } = require('yargs/helpers'); // eslint-disable-line | ||
import { array, either, taskEither } from 'fp-ts'; | ||
import { serialize as serializeOpenAPI3 } from '../language/typescript/3.0'; | ||
import { generate, Language } from '../index'; | ||
import { Decoder } from 'io-ts'; | ||
import { Reader } from 'fp-ts/lib/Reader'; | ||
import { ResolveRefContext } from '../utils/ref'; | ||
import { TaskEither } from 'fp-ts/lib/TaskEither'; | ||
import * as $RefParser from 'json-schema-ref-parser'; | ||
import * as path from 'path'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { OpenapiObject, OpenapiObjectCodec } from '../schema/3.0/openapi-object'; | ||
import { SwaggerObject } from '../schema/2.0/swagger-object'; | ||
import { serialize as serializeSwagger } from '../language/typescript/2.0'; | ||
import { AsyncAPIObject, AsyncAPIObjectCodec } from '../schema/asyncapi-2.0.0/asyncapi-object'; | ||
import { serialize as serializeAsyncApi } from '../language/typescript/asyncapi-2.0.0'; | ||
|
||
const options = yargs(hideBin(process.argv)) | ||
.usage('swagger-codegen-ts -o <output_path> [flags] <path/to/spec.yaml>...') | ||
.option('baseDir', { | ||
alias: 'b', | ||
describe: 'base directory for <specs> paths', | ||
defaultDescription: 'current working dir', | ||
}) | ||
.option('outDir', { | ||
alias: 'o', | ||
describe: 'path to the output files, relative to the current working dir' | ||
}) | ||
.positional('', { | ||
array: true, | ||
type: 'string', | ||
describe: 'list of OpenAPI or YAML specifications, paths relative to the <baseDir>', | ||
}) | ||
.demandCommand(1, 'Please provide one or more spec files.') | ||
.argv; | ||
|
||
const cwd = process.cwd(); | ||
const specsDir = path.resolve(cwd, options.baseDir || '.'); | ||
const outDir = path.resolve(cwd, options.outDir || '.'); | ||
|
||
const tasks = pipe( | ||
andr3medeiros marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options._, | ||
array.map((spec: string) => path.resolve(specsDir, spec)), | ||
array.map(spec => | ||
pipe( | ||
detectCodec(spec), | ||
taskEither.chain(codec => | ||
generate({ | ||
...(codec as any), | ||
cwd: path.dirname(spec), | ||
spec: path.basename(spec), | ||
out: outDir, | ||
}), | ||
), | ||
), | ||
), | ||
array.array.sequence(taskEither.taskEither), | ||
); | ||
|
||
tasks().then( | ||
either.fold( | ||
error => { | ||
console.error(error); | ||
process.exit(1); | ||
}, | ||
() => { | ||
console.log('Generated successfully'); | ||
}, | ||
), | ||
); | ||
|
||
export interface DetectedCodec<A> { | ||
readonly decoder: Decoder<unknown, A>; | ||
readonly language: Reader<ResolveRefContext, Language<A>>; | ||
} | ||
|
||
const supportedCodecs = [ | ||
{ | ||
decoder: OpenapiObjectCodec, | ||
language: serializeOpenAPI3, | ||
}, | ||
{ | ||
decoder: SwaggerObject, | ||
language: serializeSwagger, | ||
}, | ||
{ | ||
decoder: AsyncAPIObjectCodec, | ||
language: serializeAsyncApi, | ||
}, | ||
]; | ||
|
||
function detectCodec( | ||
spec: string, | ||
): TaskEither<Error, DetectedCodec<OpenapiObject> | DetectedCodec<SwaggerObject> | DetectedCodec<AsyncAPIObject>> { | ||
return pipe( | ||
taskEither.tryCatch( | ||
() => | ||
$RefParser.resolve(spec, { | ||
resolve: { | ||
external: false, | ||
http: false, | ||
}, | ||
}), | ||
either.toError, | ||
), | ||
taskEither.map(refs => Object.entries(refs.values())), | ||
taskEither.filterOrElse( | ||
i => i.length === 1, | ||
() => new Error('Could not detect the schema type for ' + spec), | ||
), | ||
taskEither.chain(([[, schema]]) => | ||
pipe( | ||
supportedCodecs, | ||
array.findFirst(c => either.isRight<unknown, unknown>(c.decoder.decode(schema))), | ||
taskEither.fromOption(() => new Error('Could not detect the schema type for ' + spec)), | ||
), | ||
), | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.