Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #15 from StephenRadachy/master
Browse files Browse the repository at this point in the history
add snake->camel case + type prefixing
  • Loading branch information
StephenRadachy authored Dec 18, 2017
2 parents 2b64a77 + bb8a74d commit c244502
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Copyright (c) 2017 Target Brands, Inc."
],
"name": "graphql-liftoff",
"version": "1.0.0",
"version": "1.0.2",
"description": "generates GraphQL schema language from API specifications and more",
"engineStrict": true,
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async function exec(parser: string, mArgs: string[], stdin: boolean): Promise<an
}

function usage(): void {
console.log(`usage: ${(pkg as any).name} [--help|-h] <parser> [--key=value|--key|-k] <filename|url|none-for-stdin>
console.log(`usage: ${(pkg as any).name} [--help|-h] <parser> [--key=value|-k=value|--key|-k] <filename|url|none-for-stdin>
description:
\t${(pkg as any).name} ${(pkg as any).description}
Expand Down
27 changes: 24 additions & 3 deletions src/parsers/swagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import * as yml from 'js-yaml'
import * as converter from 'oas-raml-converter'
import { parserUsage } from '../../utils'
import { addPrefix, getOptionValue, isOptionSet, parserUsage, toCamel } from '../../utils'

let isYAML: boolean = false
let isPrefix: boolean = false
let isCamelCase: boolean = false

let prefix: string = ''
export async function parse(content: string, options: any): Promise<AST> {
let isYAML = false
if (options.hasOwnProperty('y') || options.hasOwnProperty('yaml')) { isYAML = true }
if (isOptionSet(options, 'c', 'camel-case')) { isCamelCase = true }
if (isOptionSet(options, 'p', 'prefix-type-name')) {
prefix = getOptionValue(options, 'p', 'prefix-type-name')
isPrefix = true
}
if (isOptionSet(options, 'y', 'yaml')) { isYAML = true }
const transformer = new converter.Converter(converter.Formats.OAS20, converter.Formats.RAML)
if (!isYAML) {
content = yml.safeDump(JSON.parse(content))
Expand All @@ -16,6 +25,16 @@ export async function parse(content: string, options: any): Promise<AST> {

export function usage(parser: string): void {
const args = [
{
short: 'c',
long: 'camel-case',
description: 'convert all keys from snake_case to camelCase'
},
{
short: 'p',
long: 'prefix-type-name',
description: '[--prefix-type-name|-prefix]="prefix" prefix type names'
},
{
short: 'y',
long: 'yaml',
Expand All @@ -27,6 +46,8 @@ export function usage(parser: string): void {

function parseDefinitions(definitions: any[], ast: AST = {} as AST): AST {
definitions.map((d: any) => parseDefinition(d, ast))
if (isCamelCase) { ast = toCamel(ast) }
if (isPrefix) { ast = addPrefix(ast, prefix) }
return ast
}

Expand Down
47 changes: 46 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,53 @@ export function parserUsage(parser: string, args: Arg[]) {
description: 'Show this help documentation'
} as Arg)
const sorted = args.sort((a: Arg, b: Arg) => a.long.localeCompare(b.long))
console.log(`usage: ${(pkg as any).name} ${parser} [--key=value|--key|-k] <filename|url|none-for-stdin>\n\noptions:`)
console.log(`usage: ${(pkg as any).name} ${parser} [--key=value|-k=value|--key|-k] <filename|url|none-for-stdin>\n\noptions:`)
sorted.map(a => {
console.log(`\t--${a.long},-${a.short}\t${a.description}`)
})
}

function convertSnakeToCamel(s: string): string {
return s.replace(/(\_\w)/g, m => m[1].toUpperCase())
}

export function toCamel(ast: AST): AST {
return Object
.keys(ast)
.reduce((acc: AST, cur: string, idx: number) => {
acc[cur] = {
name: convertSnakeToCamel(ast[cur].name),
description: ast[cur].description,
fields: ast[cur].fields.map(f => {
f.name = convertSnakeToCamel(f.name)
return f
})
} as GraphQLType
return acc
}, {} as AST)
}

export function addPrefix(ast: AST, prefix: String): AST {
return Object
.keys(ast)
.reduce((acc: AST, cur: string) => {
acc[cur] = {
name: prefix + ast[cur].name,
description: ast[cur].description,
fields: ast[cur].fields
} as GraphQLType
return acc
}, {} as AST)
}

export function isOptionSet(options: any, short: string, long: string): boolean {
return options.hasOwnProperty(short) || options.hasOwnProperty(long)
}

export function getOptionValue(options: any, short: string, long: string): any {
if (options.hasOwnProperty(short)) {
return options[short]
} else if (options.hasOwnProperty(long)) {
return options[long]
}
}
106 changes: 103 additions & 3 deletions test/unit/utils/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getContent, parserUsage } from '../../../src/utils/index.ts'
import { addPrefix, getContent, getOptionValue, isOptionSet, parserUsage, toCamel } from '../../../src/utils/index.ts'
import packageJSON from '../../../package.json'

describe('getContent', () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('parser usage', () => {
// given
const parser = 'example'
const args = []
const expected = `usage: graphql-liftoff example [--key=value|--key|-k] <filename|url|none-for-stdin>
const expected = `usage: graphql-liftoff example [--key=value|-k=value|--key|-k] <filename|url|none-for-stdin>
options:
\t--help,-h Show this help documentation
Expand All @@ -93,7 +93,7 @@ options:
description: 'test description'
}
]
const expected = `usage: graphql-liftoff example [--key=value|--key|-k] <filename|url|none-for-stdin>
const expected = `usage: graphql-liftoff example [--key=value|-k=value|--key|-k] <filename|url|none-for-stdin>
options:
\t--help,-h Show this help documentation
Expand All @@ -106,3 +106,103 @@ options:
expect(consoleOutput).toEqual(expected)
})
})

describe('toCamel', () => {
it('should convert snake to camel case', () => {
// given
const input = {
test: {
name: 'snake_case',
fields: [
{
name: 'field_snake_case'
}
]
}
}

// when
const result = toCamel(input)

// then
expect(result).toEqual({ test: { name: 'snakeCase', fields: [ { name: 'fieldSnakeCase'}]}})
})
})

describe('addPrefix', () => {
it('should add a prefix', () => {
// given
const input = {
test: {
name: 'snake_case',
fields: [
{
name: 'field_snake_case'
}
]
}
}

// when
const result = addPrefix(input, 'TEST_')

// then
expect(result).toEqual({ test: { name: 'TEST_snake_case', fields: [ { name: 'field_snake_case'}]}})
})
})

describe('isOptionSet', () => {
it('should find the short option value', () => {
// given
const input = {
v: 'asdf'
}

// when
const result = isOptionSet(input, 'v', 'verylongargname')

// then
expect(result).toEqual(true)
})

it('should find the long option value', () => {
// given
const input = {
verylongargname: 'asdf'
}

// when
const result = isOptionSet(input, 'v', 'verylongargname')

// then
expect(result).toEqual(true)
})
})

describe('getOptionValue', () => {
it('should get the short option value', () => {
// given
const input = {
v: 'asdf'
}

// when
const result = getOptionValue(input, 'v', 'verylongargname')

// then
expect(result).toEqual('asdf')
})

it('should get the long option value', () => {
// given
const input = {
verylongargname: 'asdf'
}

// when
const result = getOptionValue(input, 'v', 'verylongargname')

// then
expect(result).toEqual('asdf')
})
})

0 comments on commit c244502

Please sign in to comment.