diff --git a/index.js b/index.js index ecd4e6b..9577d9d 100755 --- a/index.js +++ b/index.js @@ -10,9 +10,7 @@ if (require.main === module) { process.stderr.write(showHelp ? options.help : options.version + eol) } else { const nexe = require('./lib/nexe') - nexe.compile(argv).catch((e) => { - process.stderr.write(eol + e.stack) - }) + nexe.compile(argv) } } else { module.exports = require('./lib/nexe') diff --git a/package.json b/package.json index 6196122..57d13bc 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ }, "dependencies": { "app-builder": "^5.1.0", + "caw": "^2.0.1", "chalk": "^1.1.3", "download": "^6.2.0", "fuse-box": "^2.3.4-beta.11", diff --git a/src/compiler.ts b/src/compiler.ts index ce371c5..30e1975 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -180,14 +180,15 @@ export class NexeCompiler { } private async _fetchPrebuiltBinaryAsync(target: NexeTarget) { - const downloadOptions: any = { - headers: { - 'User-Agent': 'nexe (https://www.npmjs.com/package/nexe)' - } - } + let downloadOptions = this.options.downloadOptions + if (this.options.ghToken) { - downloadOptions.headers.Authorization = 'token ' + this.options.ghToken + downloadOptions = Object.assign({}, downloadOptions) + downloadOptions.headers = Object.assign({}, downloadOptions.headers, { + Authorization: 'token ' + this.options.ghToken + }) } + const githubRelease = await getLatestGitRelease(downloadOptions) const assetName = target.toString() const asset = githubRelease.assets.find(x => x.name === assetName) @@ -196,19 +197,19 @@ export class NexeCompiler { throw new Error(`${assetName} not available, create it using the --build flag`) } const filename = this.getNodeExecutableLocation(target) - // Remove the authorization, as the download is done via amazon S3 and does not need the token - delete downloadOptions.headers.Authorization - await download(asset.browser_download_url, dirname(filename), downloadOptions).on( - 'response', - (res: IncomingMessage) => { - const total = +res.headers['content-length']! - let current = 0 - res.on('data', data => { - current += data.length - this.compileStep.modify(`Downloading...${(current / total * 100).toFixed()}%`) - }) - } - ) + + await download( + asset.browser_download_url, + dirname(filename), + this.options.downloadOptions + ).on('response', (res: IncomingMessage) => { + const total = +res.headers['content-length']! + let current = 0 + res.on('data', data => { + current += data.length + this.compileStep.modify(`Downloading...${(current / total * 100).toFixed()}%`) + }) + }) return createReadStream(filename) } diff --git a/src/nexe.ts b/src/nexe.ts index 4bc4c1a..f1494a6 100644 --- a/src/nexe.ts +++ b/src/nexe.ts @@ -10,6 +10,7 @@ import artifacts from './steps/artifacts' import patches from './patches' import { rimrafAsync } from './util' import { NexeTarget } from './target' +import { EOL } from 'os' async function compile( compilerOptions?: Partial, @@ -39,11 +40,21 @@ async function compile( ? void nexe(compiler).then( () => callback && callback(null), (e: Error) => { + if (compiler.options.loglevel !== 'silent') { + process.stderr.write(EOL + e.stack + EOL) + } + compiler.quit() if (callback) callback(e) else throw e } ) - : nexe(compiler) + : nexe(compiler).catch((e: Error) => { + if (compiler.options.loglevel !== 'silent') { + process.stderr.write(EOL + e.stack + EOL) + } + compiler.quit() + throw e + }) } export { argv, compile, version, NexeCompiler, NexeOptions, help } diff --git a/src/options.ts b/src/options.ts index b28ea47..501a77f 100644 --- a/src/options.ts +++ b/src/options.ts @@ -6,6 +6,8 @@ import { getTarget, NexeTarget } from './target' import { EOL, homedir } from 'os' import * as c from 'chalk' +const caw = require('caw') + export const version = '{{replace:0}}' export interface NexePatch { @@ -227,6 +229,15 @@ function normalizeOptions(input?: Partial): NexeOptions { options.make = flatten(options.vcBuild, options.make) options.configure = flatten(options.configure) options.resources = flatten(opts.resource, options.resources) + + options.downloadOptions = options.downloadOptions || {} + options.downloadOptions.headers = options.downloadOptions.headers || {} + options.downloadOptions.headers['User-Agent'] = 'nexe (https://www.npmjs.com/package/nexe)' + options.downloadOptions.agent = process.env.HTTPS_PROXY + ? caw(process.env.HTTPS_PROXY, { protocol: 'https' }) + : options.downloadOptions.agent || require('https').globalAgent + options.downloadOptions.rejectUnauthorized = process.env.HTTPS_PROXY ? false : true + options.rc = options.rc || extractCliMap(/^rc-.*/, options) options.output = (options.targets[0] as NexeTarget).platform === 'windows' diff --git a/yarn.lock b/yarn.lock index 72f5fdc..abeca62 100644 --- a/yarn.lock +++ b/yarn.lock @@ -363,7 +363,7 @@ caseless@~0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" -caw@^2.0.0: +caw@^2.0.0, caw@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/caw/-/caw-2.0.1.tgz#6c3ca071fc194720883c2dc5da9b074bfc7e9e95" dependencies: