Skip to content

Commit

Permalink
fix(*): Dep Update and Pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
William Luke committed Aug 20, 2019
1 parent ace9a47 commit 9d1583f
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 72 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
"pluralize": "^7.0.0",
"prettier": "^1.17.0",
"pretty-error": "2.2.0-rc.1",
"prisma-datamodel": "^1.31.1",
"prisma-generate-schema": "^1.31.1",
"prisma-datamodel": "^1.34.6",
"prisma-generate-schema": "^1.34.6",
"prisma-json-schema": "^0.1.3",
"request": "^2.88.0",
"tar": "^4.4.8",
"ts-node": "^8.1.0",
"typescript": "^3.4.4",
"typescript": "3.4.5",
"yargs": "^13.2.2"
},
"devDependencies": {
Expand Down
18 changes: 8 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import { importFile } from './helpers';
import { Config, ConfigWithInfo, InputConfig } from './types';
import { DEFAULT_META_SCHEMA_DIR, normalizeConfig } from './yogaDefaults';
import * as dotenv from 'dotenv'
import * as fs from 'fs'
import * as path from 'path'
import * as ts from 'typescript'
import { importFile } from './helpers'
import { Config, ConfigWithInfo, InputConfig } from './types'
import { DEFAULT_META_SCHEMA_DIR, normalizeConfig } from './yogaDefaults'

/**
* Find a `prisma.yml` file if it exists
Expand Down Expand Up @@ -121,9 +121,7 @@ export function importYogaConfig(
const yogaConfigPath = findYogaConfigFile()
const defaultProjectDir = findConfigFile('package.json', { required: true })
const projectDir = path.dirname(
yogaConfigPath
? yogaConfigPath
: defaultProjectDir,
yogaConfigPath ? yogaConfigPath : defaultProjectDir,
)
const inputConfig = yogaConfigPath
? importFile<InputConfig>(yogaConfigPath, 'default', opts.invalidate)
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ export function prettify(
return code
}
}
export function prettyPath(filePath: string): string{
return(path.relative(process.cwd(), filePath))
export function prettyPath(filePath: string): string {
return path.relative(process.cwd(), filePath)
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { InputConfig as YogaConfig, MaybePromise, Yoga } from './types'
import { injectCustomEnvironmentVariables } from './config'
export * from 'nexus'
export * from 'nexus-prisma'
import gqlMiddleware from './gqlMiddleware';
import gqlMiddleware from './gqlMiddleware'
export { ApolloServer, express, logger, gqlMiddleware }


injectCustomEnvironmentVariables()

export function yogaConfig(opts: YogaConfig) {
Expand Down
14 changes: 7 additions & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import chalk from 'chalk'
import readline from 'readline'
let logLevel = 0

export function setLogLevel(level: number){
logLevel=level
export function setLogLevel(level: number) {
logLevel = level
}

function format(label: string, msg: string) {
Expand All @@ -23,26 +23,26 @@ function chalkTag(msg: string) {
}

export function log(msg: string = '', tag?: string, level: number = 3) {
if(level > logLevel) return
if (level > logLevel) return
tag ? console.log(format(chalkTag(tag), msg)) : console.log(msg)
}

export function info(msg: string, tag?: string, level: number = 2) {
if(level > logLevel) return
if (level > logLevel) return
console.log(
format(chalk.bgBlue.black(' INFO ') + (tag ? chalkTag(tag) : ''), msg),
)
}

export function done(msg: string, tag?: string, level: number = 2) {
if(level > logLevel) return
if (level > logLevel) return
console.log(
format(chalk.bgGreen.black(' DONE ') + (tag ? chalkTag(tag) : ''), msg),
)
}

export function warn(msg: string, tag?: string, level: number = 1) {
if(level > logLevel) return
if (level > logLevel) return
console.warn(
format(
chalk.bgYellow.black(' WARN ') + (tag ? chalkTag(tag) : ''),
Expand All @@ -52,7 +52,7 @@ export function warn(msg: string, tag?: string, level: number = 1) {
}

export function error(msg: string, tag?: string, level: number = 0) {
if(level > logLevel) return
if (level > logLevel) return
console.error(
format(chalk.bgRed(' ERROR ') + (tag ? chalkTag(tag) : ''), msg),
)
Expand Down
62 changes: 34 additions & 28 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import { existsSync } from 'fs';
import { GraphQLSchema } from 'graphql';
import { applyMiddleware } from 'graphql-middleware';
import { Server } from 'http';
import { makeSchema } from 'nexus';
import { makePrismaSchema } from 'nexus-prisma';
import nodeWatch from 'node-watch';
import * as path from 'path';
import PrettyError from 'pretty-error';
import { register } from 'ts-node';
import { importYogaConfig } from './config';
import { findFileByExtension, importFile } from './helpers';
import * as logger from './logger';
import { makeSchemaDefaults } from './nexusDefaults';
import { ConfigWithInfo, GraphqlMiddleware, Yoga } from './types';
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { existsSync } from 'fs'
import { GraphQLSchema } from 'graphql'
import { applyMiddleware } from 'graphql-middleware'
import { Server } from 'http'
import { makeSchema } from 'nexus'
import { makePrismaSchema } from 'nexus-prisma'
import nodeWatch from 'node-watch'
import * as path from 'path'
import PrettyError from 'pretty-error'
import { register } from 'ts-node'
import { importYogaConfig } from './config'
import { findFileByExtension, importFile } from './helpers'
import * as logger from './logger'
import { makeSchemaDefaults } from './nexusDefaults'
import { ConfigWithInfo, GraphqlMiddleware, Yoga } from './types'

const pe = new PrettyError().appendStyle({
'pretty-error': {
Expand All @@ -38,7 +38,7 @@ export async function watch(env?: string): Promise<void> {
logger.info('Starting development server...')
let info = importYogaConfig({ env })

let filesToWatch = [path.join(info.projectDir, 'src', '**','*.ts')]
let filesToWatch = [path.join(info.projectDir, 'src', '**', '*.ts')]
logger.info(`Watching ${JSON.stringify(filesToWatch)}`)

let oldServer: any | undefined = await start(info, true)
Expand Down Expand Up @@ -188,10 +188,12 @@ function importArtifacts(
graphqlMiddleware = importFile(graphqlMiddlewarePath, 'default')

if (!graphqlMiddleware) {
throw new Error(`Unable to import graphqlMiddleware from ${graphqlMiddlewarePath}`)
throw new Error(
`Unable to import graphqlMiddleware from ${graphqlMiddlewarePath}`,
)
}
}

return {
context,
expressMiddleware: express,
Expand All @@ -211,31 +213,35 @@ function getYogaServer(info: ConfigWithInfo): Yoga {
return {
async server() {
const app = express()
const { types, context, expressMiddleware, graphqlMiddleware } = importArtifacts(
const {
types,
context,
expressMiddleware,
graphqlMiddleware,
} = importArtifacts(
config.resolversPath,
config.contextPath,
config.expressPath,
config.graphqlMiddlewarePath
config.graphqlMiddlewarePath,
)
const makeSchemaOptions = makeSchemaDefaults(
config as any,
types,
info.prismaClientDir,
)
let schema: GraphQLSchema;
if(config.prisma){
logger.info("Using MakePrismaSchema")
let schema: GraphQLSchema
if (config.prisma) {
logger.info('Using MakePrismaSchema')
schema = makePrismaSchema({
...makeSchemaOptions,
prisma: config.prisma,
})
logger.done("Prisma Schema Generated")
logger.done('Prisma Schema Generated')
} else {
schema = makeSchema(makeSchemaOptions)
}


if(graphqlMiddleware){
if (graphqlMiddleware) {
schema = applyMiddleware(schema, ...graphqlMiddleware)
}
const server = new ApolloServer({
Expand Down
16 changes: 11 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { PrismaClientInput, PrismaSchemaConfig } from 'nexus-prisma/dist/types'
import Express from 'express'
import { Server as HttpServer } from 'http'
import { IMiddlewareWithOptions, IMiddlewareResolver } from 'graphql-middleware/dist/types';
import { IMiddlewareTypeMap, IMiddlewareGenerator } from 'graphql-middleware';
import {
IMiddlewareWithOptions,
IMiddlewareResolver,
} from 'graphql-middleware/dist/types'
import { IMiddlewareTypeMap, IMiddlewareGenerator } from 'graphql-middleware'

export type MaybePromise<T> = Promise<T> | T

Expand Down Expand Up @@ -68,7 +71,7 @@ export type InputConfig = {
* @default ./src/express.ts
*/
expressPath?: string
/**
/**
* Path to the `graphqlMiddleware.ts` file.
* This file returns graphql middleware to inject into the schema .
* **If provided, path has to exist**
Expand All @@ -89,8 +92,11 @@ type RequiredProperty<T extends keyof InputConfig> = Exclude<
InputConfig[T],
undefined
>
export type GraphqlMiddleware =
(IMiddlewareGenerator<any, any, any> | IMiddlewareWithOptions<any, any, any> | IMiddlewareResolver<any, any, any> | IMiddlewareTypeMap<any, any, any>)[];
export type GraphqlMiddleware = (
| IMiddlewareGenerator<any, any, any>
| IMiddlewareWithOptions<any, any, any>
| IMiddlewareResolver<any, any, any>
| IMiddlewareTypeMap<any, any, any>)[]

export type Config = {
resolversPath: RequiredProperty<'resolversPath'>
Expand Down
52 changes: 37 additions & 15 deletions src/yogaDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import chalk from 'chalk';
import { existsSync } from 'fs';
import { PrismaClientInput } from 'nexus-prisma/dist/types';
import { join, relative } from 'path';
import { findPrismaConfigFile } from './config';
import { importFile } from './helpers';
import * as logger from './logger';
import { Config, DatamodelInfo, DefaultConfig, InputConfig, InputOutputFilesConfig, InputPrismaConfig } from './types';
import chalk from 'chalk'
import { existsSync } from 'fs'
import { PrismaClientInput } from 'nexus-prisma/dist/types'
import { join, relative } from 'path'
import { findPrismaConfigFile } from './config'
import { importFile } from './helpers'
import * as logger from './logger'
import {
Config,
DatamodelInfo,
DefaultConfig,
InputConfig,
InputOutputFilesConfig,
InputPrismaConfig,
} from './types'

export const DEFAULTS: DefaultConfig = {
contextPath: './src/context.ts',
Expand Down Expand Up @@ -37,7 +44,7 @@ export const DEFAULTS: DefaultConfig = {
},
},
expressPath: './src/express.ts',
graphqlMiddlewarePath: './src/graphqlMiddleware.ts'
graphqlMiddlewarePath: './src/graphqlMiddleware.ts',
}

export const DEFAULT_META_SCHEMA_DIR = './src/generated/nexus-prisma/'
Expand All @@ -56,7 +63,10 @@ export function normalizeConfig(
typesPath: typesPath(projectDir, config.typesPath),
ejectedFilePath: ejectFilePath(projectDir, config.ejectedFilePath),
expressPath: expressPath(projectDir, config.expressPath),
graphqlMiddlewarePath: graphqlMiddlewarePath(projectDir, config.graphqlMiddlewarePath),
graphqlMiddlewarePath: graphqlMiddlewarePath(
projectDir,
config.graphqlMiddlewarePath,
),
output: output(projectDir, config.output),
prisma: prisma(projectDir, config.prisma),
}
Expand Down Expand Up @@ -178,9 +188,17 @@ function graphqlMiddlewarePath(
projectDir: string,
input: string | undefined,
): string | undefined {
const path = inputOrDefaultPath(projectDir, input, DEFAULTS.graphqlMiddlewarePath!)
const path = inputOrDefaultPath(
projectDir,
input,
DEFAULTS.graphqlMiddlewarePath!,
)

return optional(path, input, buildError(projectDir, path, 'graphqlMiddleware'))
return optional(
path,
input,
buildError(projectDir, path, 'graphqlMiddleware'),
)
}
function output(
projectDir: string,
Expand Down Expand Up @@ -216,8 +234,12 @@ export function client(
datamodelInfo: DatamodelInfo,
): PrismaClientInput {
if (input === undefined) {
logger.warn("No Prisma Client Input found trying to resolve path")
const defaultPath = join(projectDir, datamodelInfo.clientPath.replace(/\\/g, "/"), 'index.ts')
logger.warn('No Prisma Client Input found trying to resolve path')
const defaultPath = join(
projectDir,
datamodelInfo.clientPath.replace(/\\/g, '/'),
'index.ts',
)
logger.info(`Looking for Prisma Client in ${defaultPath}`)
const clientPath = requiredPath(
defaultPath,
Expand Down Expand Up @@ -281,7 +303,7 @@ function prisma(
if (input === undefined && hasPrisma) {
input = {}
}
logger.info("Prisma Found in Project")
logger.info('Prisma Found in Project')
const importedDatamodelInfo = datamodelInfo(
projectDir,
input!.datamodelInfoPath,
Expand Down

0 comments on commit 9d1583f

Please sign in to comment.