From 8ca4194d57158b4f9d2390afc84023e68ed12855 Mon Sep 17 00:00:00 2001 From: Flavian DESVERNE Date: Fri, 15 Mar 2019 11:31:30 +0100 Subject: [PATCH] Support express middleware when ejecting --- examples/minimal/tsconfig.json | 2 +- packages/yoga/src/cli/commands/build/index.ts | 4 +- .../yoga/src/cli/commands/build/renderers.ts | 69 +++++++++---------- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/examples/minimal/tsconfig.json b/examples/minimal/tsconfig.json index 76ba725..15412a8 100644 --- a/examples/minimal/tsconfig.json +++ b/examples/minimal/tsconfig.json @@ -3,7 +3,7 @@ "module": "commonjs", "moduleResolution": "node", "target": "es5", - "rootDir": "./src", + "rootDir": ".", "outDir": "dist", "sourceMap": true, "lib": ["es2015", "esnext.asynciterable", "es2017", "dom"], diff --git a/packages/yoga/src/cli/commands/build/index.ts b/packages/yoga/src/cli/commands/build/index.ts index 5dcfffd..002aa21 100644 --- a/packages/yoga/src/cli/commands/build/index.ts +++ b/packages/yoga/src/cli/commands/build/index.ts @@ -55,7 +55,7 @@ function useEntryPoint( const indexFile = renderIndexFile(ejectFilePath) const indexFilePath = path.join(path.dirname(ejectFilePath), 'index.ts') - outputFile(indexFile, indexFilePath, config.options, info) + outputFile(indexFilePath, indexFile, config.options, info) } export function writeEjectFiles( @@ -123,8 +123,8 @@ function transpileModule( } function outputFile( - fileContent: string, filePath: string, + fileContent: string, compilerOptions: ts.CompilerOptions, info: ConfigWithInfo, ) { diff --git a/packages/yoga/src/cli/commands/build/renderers.ts b/packages/yoga/src/cli/commands/build/renderers.ts index 27fdc0f..7755f69 100644 --- a/packages/yoga/src/cli/commands/build/renderers.ts +++ b/packages/yoga/src/cli/commands/build/renderers.ts @@ -1,8 +1,8 @@ -import { getRelativePath } from '.' +import { EOL } from 'os' import * as path from 'path' -import { ConfigWithInfo } from '../../../types' +import { getRelativePath } from '.' import { findFileByExtension } from '../../../helpers' -import { EOL } from 'os' +import { ConfigWithInfo } from '../../../types' export function renderIndexFile(ejectFilePath: string) { return ` @@ -27,26 +27,14 @@ export function renderPrismaEjectFile(filePath: string, info: ConfigWithInfo) { return ` import * as path from 'path' import { ApolloServer, makePrismaSchema, express, yogaEject } from 'yoga' - import datamodelInfo from '${getRelativePath( - fileDir, - info.datamodelInfoDir!, - )}' - import { prisma } from '${getRelativePath(fileDir, info.prismaClientDir!)}' - ${ - info.yogaConfig.contextPath - ? `import context from '${getRelativePath( - fileDir, - info.yogaConfig.contextPath, - )}'` - : '' - } - import * as types from '${getRelativePath( - fileDir, - info.yogaConfig.resolversPath, - )}' + ${renderImportIf('* as types', fileDir, info.yogaConfig.resolversPath)} + ${renderImportIf('context', fileDir, info.yogaConfig.contextPath)} + ${renderImportIf('expressMiddleware', fileDir, info.yogaConfig.expressPath)} + ${renderImportIf('datamodelInfo', fileDir, info.datamodelInfoDir)} + ${renderImportIf('{ prisma }', fileDir, info.prismaClientDir)} export default yogaEject({ - server() { + async server() { const schema = makePrismaSchema({ types, prisma: { @@ -106,18 +94,19 @@ export function renderPrismaEjectFile(filePath: string, info: ConfigWithInfo) { }) const app = express() + ${info.yogaConfig.expressPath ? 'await expressMiddleware(app)' : ''} apolloServer.applyMiddleware({ app, path: '/' }) return app }, - startServer(app) { + async startServer(app) { return app.listen({ port: 4000 }, () => { console.log( \`🚀 Server ready at http://localhost:4000/\`, ) }) }, - stopServer(http) { + async stopServer(http) { http.close() } }) @@ -130,21 +119,12 @@ export function renderSimpleIndexFile(filePath: string, info: ConfigWithInfo) { return `\ import * as path from 'path' import { ApolloServer, makeSchema, express, yogaEject } from 'yoga' -${ - info.yogaConfig.contextPath - ? `import context from '${getRelativePath( - fileDir, - info.yogaConfig.contextPath, - )}'` - : '' -} -import * as types from '${getRelativePath( - fileDir, - info.yogaConfig.resolversPath, - )}' +${renderImportIf('* as types', fileDir, info.yogaConfig.resolversPath)} +${renderImportIf('context', fileDir, info.yogaConfig.contextPath)} +${renderImportIf('expressMiddleware', fileDir, info.yogaConfig.expressPath)} export default yogaEject({ - server() { + async server() { const schema = makeSchema({ types, outputs: { @@ -183,18 +163,19 @@ export default yogaEject({ }) const app = express() + ${info.yogaConfig.expressPath ? 'await expressMiddleware(app)' : ''} apolloServer.applyMiddleware({ app, path: '/' }) return app }, - startServer(app) { + async startServer(app) { return app.listen({ port: 4000 }, () => { console.log( \`🚀 Server ready at http://localhost:4000/\`, ) }) }, - stopServer(http) { + async stopServer(http) { http.close() } }) @@ -228,3 +209,15 @@ ${resolversFile .join(EOL)} ` } + +export function renderImportIf( + importName: string, + sourceDir: string, + targetPath: string | undefined, +) { + if (!targetPath) { + return '' + } + + return `import ${importName} from '${getRelativePath(sourceDir, targetPath)}'` +}