Skip to content

Commit

Permalink
refactor: no fatal when building tsc program (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Kuhrt committed Jun 9, 2020
1 parent 0a07b7b commit 15ab57a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/lib/add-to-context-extractor/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { codeBlock } from 'common-tags'
import { Either, isLeft, right } from 'fp-ts/lib/Either'
import * as fs from 'fs-jetpack'
import { hardWriteFile } from '../fs'
import * as Layout from '../layout'
Expand All @@ -20,16 +21,18 @@ export const NEXUS_DEFAULT_RUNTIME_CONTEXT_TYPEGEN_PATH = fs.path(
*/
export async function generateContextExtractionArtifacts(
layout: Layout.Layout
): Promise<ExtractedContextTypes> {
): Promise<Either<Error, ExtractedContextTypes>> {
log.trace('starting context type extraction')
const program = createTSProgram(layout, { withCache: true })
const errProgram = createTSProgram(layout, { withCache: true })
if (isLeft(errProgram)) return errProgram
const program = errProgram.right
const contextTypes = extractContextTypes(program.getProgram())

await writeContextTypeGenFile(contextTypes)

log.trace('finished context type extraction', { contextTypes })

return contextTypes
return right(contextTypes)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/lib/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rightOrThrow } from '@nexus/logger/dist/utils'
import { stripIndent } from 'common-tags'
import * as FS from 'fs-jetpack'
import * as Path from 'path'
Expand Down Expand Up @@ -85,7 +86,7 @@ export async function buildNexusApp(settings: BuildSettings) {

log.info('building typescript program')

const tsBuilder = createTSProgram(layout, { withCache: true })
const tsBuilder = rightOrThrow(createTSProgram(layout, { withCache: true }))

log.info('compiling a production build')

Expand Down
8 changes: 7 additions & 1 deletion src/lib/reflection/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rightOrThrow } from '@nexus/logger/dist/utils'
import * as NexusSchema from '@nexus/schema'
import * as Schema from '../../runtime/schema'
import { generateContextExtractionArtifacts } from '../add-to-context-extractor'
Expand Down Expand Up @@ -30,5 +31,10 @@ export async function writeArtifacts(params: TypegenParams) {
// Generate the context typegen file
const contextExtractorTypegenPromise = generateContextExtractionArtifacts(params.layout)

await Promise.all([nexusSchemaTypegenPromise, contextExtractorTypegenPromise])
const [_, contextExtractorTypegen] = await Promise.all([
nexusSchemaTypegenPromise,
contextExtractorTypegenPromise,
])

rightOrThrow(contextExtractorTypegen)
}
15 changes: 6 additions & 9 deletions src/lib/tsc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Either, left, right } from 'fp-ts/lib/Either'
import * as fs from 'fs-jetpack'
import { addHook } from 'pirates'
import * as ts from 'typescript'
Expand All @@ -16,7 +17,7 @@ interface ProgramOptions {
export function createTSProgram(
layout: Layout,
options?: ProgramOptions
): ts.EmitAndSemanticDiagnosticsBuilderProgram {
): Either<Error, ts.EmitAndSemanticDiagnosticsBuilderProgram> {
// Incremental option cannot be set when `noEmit: true`
const compilerCacheOptions =
options?.withCache && !layout.tsConfig.content.options.noEmit
Expand Down Expand Up @@ -47,17 +48,13 @@ export function createTSProgram(
(error) => error.code === SOURCE_ROOT_MUST_CONTAIN_ALL_SOURCE_FILES_ERROR_CODE
)
if (maybeSourceRootMustContainAllSourceFilesError) {
log.fatal(
const message =
'Your app is invalid\n\n' +
ts.formatDiagnosticsWithColorAndContext(
[maybeSourceRootMustContainAllSourceFilesError],
diagnosticHost
)
)
process.exit(1)
ts.formatDiagnosticsWithColorAndContext([maybeSourceRootMustContainAllSourceFilesError], diagnosticHost)
return left(Error(message))
}

return builder
return right(builder)
}

export function deleteTSIncrementalFile(layout: Layout) {
Expand Down

0 comments on commit 15ab57a

Please sign in to comment.