fix: avoid syncronous errors in compile method

This commit is contained in:
calebboyd
2019-01-23 20:21:34 -06:00
parent 12c77a34fd
commit 1a75bdb058
8 changed files with 80 additions and 31 deletions
+10 -3
View File
@@ -35,6 +35,13 @@ export interface NexeFile {
export { NexeOptions }
export class NexeError extends Error {
constructor(m: string) {
super(m)
Object.setPrototypeOf(this, NexeError.prototype)
}
}
export class NexeCompiler {
/**
* Epoch of when compilation started
@@ -178,7 +185,7 @@ export class NexeCompiler {
assertBuild() {
if (!this.options.build) {
throw new Error('This feature is only available with `--build`')
throw new NexeError('This feature is only available with `--build`')
}
}
@@ -211,7 +218,7 @@ export class NexeCompiler {
}
if (code != 0) {
const error = `${command} ${args.join(' ')} exited with code: ${code}`
reject(new Error(error))
reject(new NexeError(error))
}
resolve()
})
@@ -255,7 +262,7 @@ export class NexeCompiler {
const asset = githubRelease.assets.find(x => x.name === assetName)
if (!asset) {
throw new Error(`${assetName} not available, create it using the --build flag`)
throw new NexeError(`${assetName} not available, create it using the --build flag`)
}
const filename = this.getNodeExecutableLocation(target)
+16 -19
View File
@@ -1,6 +1,6 @@
import { EOL } from 'os'
import { compose } from 'app-builder'
import { NexeCompiler } from './compiler'
import { NexeCompiler, NexeError } from './compiler'
import { normalizeOptions, NexeOptions, NexePatch } from './options'
import resource from './steps/resource'
import clean from './steps/clean'
@@ -15,31 +15,28 @@ async function compile(
compilerOptions?: Partial<NexeOptions>,
callback?: (err: Error | null) => void
) {
const options = normalizeOptions(compilerOptions)
const compiler = new NexeCompiler(options)
let error = null,
options: NexeOptions | null = null,
compiler: NexeCompiler | null = null
const nexe = compose(
clean,
resource,
cli,
bundle,
shim,
options.build ? [download, artifacts, ...patches, ...(options.patches as NexePatch[])] : [],
options.plugins as NexePatch[]
)
let error = null
try {
await nexe(compiler)
options = normalizeOptions(compilerOptions)
compiler = new NexeCompiler(options)
await compose(
clean,
resource,
cli,
bundle,
shim,
options.build ? [download, artifacts, ...patches, ...(options.patches as NexePatch[])] : [],
options.plugins as NexePatch[]
)(compiler)
} catch (e) {
error = e
}
if (error) {
compiler.quit(error)
if (compiler.options.loglevel !== 'silent' && error) {
process.stderr.write(EOL + error.message + EOL)
}
compiler && compiler.quit(error)
if (callback) return callback(error)
return Promise.reject(error)
}
+2 -2
View File
@@ -1,5 +1,5 @@
import * as parseArgv from 'minimist'
import { NexeCompiler } from './compiler'
import { NexeCompiler, NexeError } from './compiler'
import { isWindows, STDIN_FLAG } from './util'
import { basename, extname, join, isAbsolute, resolve } from 'path'
import { getTarget, NexeTarget } from './target'
@@ -222,7 +222,7 @@ export function resolveEntry(
result = resolveFileNameSync(cwd, '.')
}
if (!result.absPath) {
throw new Error(`Entry file "${input}" not found!`)
throw new NexeError(`Entry file "${input || ''}" not found!`)
}
return result.absPath
}
+2 -2
View File
@@ -1,4 +1,4 @@
import { NexeCompiler } from '../compiler'
import { NexeCompiler, NexeError } from '../compiler'
import { resolve, relative } from 'path'
import { each } from '@calebboyd/semaphore'
import resolveFiles from 'resolve-dependencies'
@@ -48,7 +48,7 @@ export default async function bundle(compiler: NexeCompiler, next: any) {
if (
warnings.filter(x => x.startsWith('Error parsing file') && !x.includes('node_modules')).length
) {
throw new Error('Parsing Error:\n' + warnings.join('\n'))
throw new NexeError('Parsing Error:\n' + warnings.join('\n'))
}
await each(Object.keys(files), (filename: string) => compiler.addResource(filename), {