Skip to content

Commit

Permalink
Code-Style and Chore, BREAKING: Remove support for Node 7
Browse files Browse the repository at this point in the history
- apply updated Standard Code Style
- Add thought and istanbul to dev-dependencies
- Add coverage script to package.json
- Run cli-tool after running tests (just to be sure)
- Add esnext.asynciterator to tsconfig-libs, which is needed for current graphql version
- Tests with Node 7 are removed from the TravisCI-build (should
  still work, but are not tested anymore). Node 8 has been added instead
  • Loading branch information
nknapp committed Aug 16, 2017
1 parent 7f686a9 commit fe35e2a
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 83 deletions.
17 changes: 5 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ language: "node_js"
node_js:
- "4"
- "6"
- "7"
before_script:
- "8"
script:
- npm install istanbul remap-istanbul
- istanbul cover --print none --report none ./node_modules/.bin/_mocha
# Test that running the executable itself does not create an error
- node bin/graphql-typewriter.js
- npm install
- npm run coverage
- npm run lint
after_script:
# This will fail in node v4, but it does not matter, because it is in
# after_script
- remap-istanbul -i ./coverage/coverage.json -o ./coverage/coverage-remapped.json
- istanbul report --include ./coverage/coverage-remapped.json text lcovonly
- # Send to coveralls
- npm install coveralls
- cat ./coverage/lcov.info | coveralls
- cat ./coverage/lcov.info | coveralls
6 changes: 3 additions & 3 deletions bin/graphql-typewriter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node
#!/usr/bin/env node

import program = require('commander')
import {runCli} from '../src/runCli'
import { runCli } from '../src/runCli'

function stringArray(arg: any, message: string): string[] {
function stringArray (arg: any, message: string): string[] {
if (!arg) {
throw new Error(`${message}: Value not set`)
}
Expand Down
12 changes: 6 additions & 6 deletions examples/example-usage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {graphql, buildSchema} from 'graphql'
import {schema} from './graphql/schema/example.graphqls'
import { graphql, buildSchema } from 'graphql'
import { schema } from './graphql/schema/example.graphqls'
import * as fs from 'fs'

type Context = {
Expand All @@ -8,7 +8,7 @@ type Context = {

// Implement the generated interface
class Root implements schema.Query<Context> {
person(args: {name: string}) {
person (args: {name: string}) {
return new Person(args.name, 1981)
}
}
Expand All @@ -17,16 +17,16 @@ class Person implements schema.Person<Context> {
name: string
yearOfBirth: number

constructor(name: string, yearOfBirth: number) {
constructor (name: string, yearOfBirth: number) {
this.name = name
this.yearOfBirth = yearOfBirth
}

age(_, context: Context) {
age (_, context: Context) {
return context.year - this.yearOfBirth
}

async friends(): Promise<Person[]> {
async friends (): Promise<Person[]> {
return Promise.resolve([
new Person(this.name + "'s first friend", this.yearOfBirth - 1),
new Person(this.name + "'s second friend", this.yearOfBirth - 2)
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
"license": "MIT",
"scripts": {
"compile": "tsc --noUnusedLocals",
"tslint": "tslint -e '**/*.d.ts' -e 'node_modules/**' '**/*.ts'",
"test": "mocha && npm run tslint",
"lint": "tslint -e '**/*.d.ts' -e 'node_modules/**' '**/*.ts'",
"pretest": "npm run compile",
"test": "mocha && npm run lint && node ./bin/graphql-typewriter.js",
"precoverage": "npm run compile",
"coverage": "istanbul cover --print none --report none ./node_modules/.bin/_mocha && remap-istanbul -i ./coverage/coverage.json -o ./coverage/coverage-remapped.json && istanbul report --include ./coverage/coverage-remapped.json",
"postcoverage": "istanbul check-coverage coverage/coverage-remapped.json --statements 100",
"thought": "thought run -a",
"prethoughtcheck": "thought --version || npm -g install thought",
"thoughtcheck": "thought check-engines",
"version": "thoughtful changelog -o -a && npm run thought",
"preversion": "npm run thoughtcheck",
"prepublish": "npm run compile && npm run test"
},
"dependencies": {
Expand All @@ -55,7 +56,10 @@
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.19",
"chai": "^4.1.1",
"istanbul": "^0.4.5",
"mocha": "^3.5.0",
"remap-istanbul": "^0.9.5",
"thought": "^1.5.1",
"thoughtful-release": "^0.3.1",
"ts-node": "^3.3.0",
"tslint": "^5.5.0",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Released under the MIT license.
*/

import {graphql, introspectionQuery, buildSchema} from 'graphql'
import {Renderer} from './render'
import { graphql, introspectionQuery, buildSchema } from 'graphql'
import { Renderer } from './render'

/**
* The converter class
Expand All @@ -17,7 +17,7 @@ export class Converter {
* @param graphqls the source code of the graphQL schema
* @return a Promise for the TypeScript source code.
*/
public async convert(graphqls: string): Promise<string> {
public async convert (graphqls: string): Promise<string> {
const schema: any = buildSchema(graphqls)
const renderer = new Renderer({})
const introSpection: any = await graphql(schema, introspectionQuery, {})
Expand Down
60 changes: 30 additions & 30 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Root, TypeDef, Field, Argument, EnumValue, InputField} from './model'
import {source, OMIT_NEXT_NEWLINE} from './renderTag'
import { Root, TypeDef, Field, Argument, EnumValue, InputField } from './model'
import { source, OMIT_NEXT_NEWLINE } from './renderTag'

export interface Options {
tslint?: Object
Expand All @@ -16,7 +16,7 @@ export class Renderer {
'__Directive', '__DirectiveLocation'
])

constructor(options: Options) {
constructor (options: Options) {
this.options = options
}

Expand All @@ -25,7 +25,7 @@ export class Renderer {
* @param root
* @returns {string}
*/
render(root: Root): string {
render (root: Root): string {
const namespace = source`
export namespace schema {
export type GraphqlField<Args, Result, Ctx> = Result | Promise<Result> |
Expand All @@ -48,7 +48,7 @@ ${namespace.replace(/^\s+$/mg, '')}`
* @param types
* @returns
*/
renderTypes(types: TypeDef[]) {
renderTypes (types: TypeDef[]) {
return types
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'OBJECT')
Expand All @@ -61,7 +61,7 @@ ${namespace.replace(/^\s+$/mg, '')}`
* @param type
* @returns
*/
renderTypeDef(type: TypeDef, all: TypeDef[]): string {
renderTypeDef (type: TypeDef, all: TypeDef[]): string {
return source`
${this.renderComment(type.description)}
export interface ${type.name}<Ctx> ${this.renderExtends(type)}{
Expand All @@ -76,7 +76,7 @@ ${type.fields.map((field) => this.renderMemberWithComment(field)).join('\n')}
* @param forType
* @param all
*/
renderTypename(forType: string, all: TypeDef[]): string {
renderTypename (forType: string, all: TypeDef[]): string {
const usedBy = all
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'UNION' || type.kind === 'INTERFACE')
Expand All @@ -92,7 +92,7 @@ ${type.fields.map((field) => this.renderMemberWithComment(field)).join('\n')}
* @param type
* @returns
*/
renderExtends(type: TypeDef): string {
renderExtends (type: TypeDef): string {
if (type.interfaces && type.interfaces.length > 0) {
const interfaces = type.interfaces.map((it) => `${it.name}<Ctx>`).join(', ')
return `extends ${interfaces} `
Expand All @@ -106,7 +106,7 @@ ${type.fields.map((field) => this.renderMemberWithComment(field)).join('\n')}
* @param field
* @returns
*/
renderMemberWithComment(field: Field): string {
renderMemberWithComment (field: Field): string {
return source`
${this.renderComment(field.description)}
${this.renderMember(field)}
Expand All @@ -118,7 +118,7 @@ ${this.renderMember(field)}
* @param field
* @returns {string}
*/
renderMember(field: Field) {
renderMember (field: Field) {
const optional = field.type.kind !== 'NON_NULL'
const type = this.renderType(field.type, false)
const resultType = optional ? `${type} | undefined` : type
Expand All @@ -131,11 +131,11 @@ ${this.renderMember(field)}
* Render a single return type (or field type)
* This function creates the base type that is then used as generic to a promise
*/
renderType(type, optional: boolean) {
function maybeOptional(arg) {
renderType (type, optional: boolean) {
function maybeOptional (arg) {
return optional ? `(${arg} | undefined)` : arg
}
function generic(arg) {
function generic (arg) {
return `${arg}<Ctx>`
}

Expand All @@ -159,7 +159,7 @@ ${this.renderMember(field)}
/**
* Render a description as doc-comment
*/
renderComment(description: string): string | typeof OMIT_NEXT_NEWLINE {
renderComment (description: string): string | typeof OMIT_NEXT_NEWLINE {
if (!description) {
// Parsed by the `source` tag-function to remove the next newline
return OMIT_NEXT_NEWLINE
Expand All @@ -170,7 +170,7 @@ ${this.renderMember(field)}
/**
* Render the arguments of a function
*/
renderArgumentType(args: Argument[]) {
renderArgumentType (args: Argument[]) {
const base = args.map((arg) => {
return `${arg.name}: ${this.renderType(arg.type, false)}`
}).join(', ')
Expand All @@ -182,7 +182,7 @@ ${this.renderMember(field)}
* @param types
* @returns
*/
renderEnums(types: TypeDef[]) {
renderEnums (types: TypeDef[]) {
return types
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'ENUM')
Expand All @@ -195,7 +195,7 @@ ${this.renderMember(field)}
* @param type
* @returns
*/
renderEnum(type: TypeDef): string {
renderEnum (type: TypeDef): string {
return source`
${this.renderComment(type.description)}
export type ${type.name} = ${type.enumValues.map((value) => `'${value.name}'`).join(' | ')}
Expand All @@ -211,7 +211,7 @@ export const ${type.name}: {
/**
* Renders a type definition for an enum value.
*/
renderEnumValueType(value: EnumValue): string {
renderEnumValueType (value: EnumValue): string {
return source`
${value.name}: '${value.name}',
`
Expand All @@ -220,7 +220,7 @@ ${value.name}: '${value.name}',
/**
* Renders a the definition of an enum value.
*/
renderEnumValue(value: EnumValue): string {
renderEnumValue (value: EnumValue): string {
return source`
${this.renderComment(value.description)}
${value.name}: '${value.name}',
Expand All @@ -232,7 +232,7 @@ ${value.name}: '${value.name}',
* @param types
* @returns
*/
renderUnions(types: TypeDef[]) {
renderUnions (types: TypeDef[]) {
return types
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'UNION')
Expand All @@ -245,7 +245,7 @@ ${value.name}: '${value.name}',
* @param type
* @returns
*/
renderUnion(type: TypeDef): string {
renderUnion (type: TypeDef): string {
// Scalars cannot be used in unions, so we're safe here
const unionValues = type.possibleTypes.map(type => `${type.name}<Ctx>`).join(' | ')
return source`
Expand All @@ -260,7 +260,7 @@ export type ${type.name}<Ctx> = ${unionValues}
* @param types
* @returns
*/
renderInterfaces(types: TypeDef[]) {
renderInterfaces (types: TypeDef[]) {
return types
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'INTERFACE')
Expand All @@ -273,7 +273,7 @@ export type ${type.name}<Ctx> = ${unionValues}
* @param type
* @returns
*/
renderInterface(type: TypeDef): string {
renderInterface (type: TypeDef): string {
return source`
${this.renderComment(type.description)}
export interface ${type.name}<Ctx> {
Expand All @@ -287,7 +287,7 @@ export interface ${type.name}<Ctx> {
* @param types
* @returns
*/
renderInputObjects(types: TypeDef[]) {
renderInputObjects (types: TypeDef[]) {
return types
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'INPUT_OBJECT')
Expand All @@ -300,7 +300,7 @@ export interface ${type.name}<Ctx> {
* @param type
* @returns
*/
renderInputObject(type: TypeDef): string {
renderInputObject (type: TypeDef): string {
return source`
${this.renderComment(type.description)}
export interface ${type.name} {
Expand All @@ -314,7 +314,7 @@ export interface ${type.name} {
* @param field
* @returns
*/
renderInputMemberWithComment(field: InputField): string {
renderInputMemberWithComment (field: InputField): string {
return source`
${this.renderComment(field.description)}
${this.renderInputMember(field)}
Expand All @@ -326,7 +326,7 @@ ${this.renderInputMember(field)}
* @param field
* @returns {string}
*/
renderInputMember(field: InputField) {
renderInputMember (field: InputField) {
const type = this.renderType(field.type, false)
// Render property as field, with the option of being of a function-type () => ReturnValue
const optional = field.type.kind !== 'NON_NULL'
Expand All @@ -339,7 +339,7 @@ ${this.renderInputMember(field)}
* @param types
* @return string
*/
renderDefaultResolvers(types: TypeDef[]): string {
renderDefaultResolvers (types: TypeDef[]): string {
const resolvers = types
.filter((type) => !this.introspectionTypes[type.name])
.filter((type) => type.kind === 'UNION' || type.kind === 'INTERFACE')
Expand All @@ -357,7 +357,7 @@ ${resolvers}
* @param type
* @return string
*/
renderResolver(type: TypeDef): string {
renderResolver (type: TypeDef): string {
return source`
${type.name}: {
__resolveType(obj) {
Expand All @@ -380,7 +380,7 @@ const scalars = {
* @param array
* @returns {{}}
*/
function setOf(array: string[]): {[key: string]: boolean} {
function setOf (array: string[]): {[key: string]: boolean} {
return array.reduce(
(set, current): {[key: string]: boolean} => {
set[current] = true
Expand Down
2 changes: 1 addition & 1 deletion src/renderTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const OMIT_NEXT_NEWLINE = {
* @param a5 string substitution
* @param a6 string substitution
*/
export function source(array, a1, a2?, a3?, a4?, a5?, a6? /* dynamic args */) {
export function source (array, a1, a2?, a3?, a4?, a5?, a6? /* dynamic args */) {
const args = Array.prototype.slice.call(arguments, 1)
let result = array[0]
for (let i = 0; i < args.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/runCli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import glob = require('glob')
import {Converter} from './index'
import { Converter } from './index'
// Import via ES 5, node-style 'require', because there are no typings for this file (yet)
const mfs = require('m-io/fs')

Expand All @@ -11,7 +11,7 @@ export interface CliArgs {
dontSaveSameFile: boolean
}

export async function runCli(cliArgs: CliArgs): Promise<any> {
export async function runCli (cliArgs: CliArgs): Promise<any> {

// Remove default value from 'exclude', if explicit values have been provided
if (cliArgs.exclude.length > 1) {
Expand Down

0 comments on commit fe35e2a

Please sign in to comment.