From de42dd94adf9b1fe6bdf438359965a532540a5f8 Mon Sep 17 00:00:00 2001 From: calebboyd Date: Sat, 30 Mar 2019 13:29:58 -0700 Subject: [PATCH] refactor: run download for src and prebuilt before plugins or patches --- .editorconfig | 3 +-- package.json | 2 +- src/compiler.ts | 38 +-------------------------- src/nexe.ts | 3 ++- src/options.ts | 2 +- src/steps/download.ts | 61 ++++++++++++++++++++++++++++++++++++------- 6 files changed, 58 insertions(+), 51 deletions(-) diff --git a/.editorconfig b/.editorconfig index 9e42605..5b539ca 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,6 +4,5 @@ end_of_line = lf charset = utf-8 indent_style = space indent_size = 2 -#https://github.com/Microsoft/vscode/issues/23983#event-1087502108 -#trim_trailing_whitespace = true +trim_trailing_whitespace = true insert_final_newline = true diff --git a/package.json b/package.json index caf1e48..262e575 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "nexe", "description": "Create a single executable out of your Node.js application", "license": "MIT", - "version": "3.0.0-beta.16", + "version": "3.0.0-beta.17", "contributors": [ "Craig Condon (http://crcn.io)", "Jared Allard ", diff --git a/src/compiler.ts b/src/compiler.ts index 3a6f9eb..972893f 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -247,39 +247,6 @@ export class NexeCompiler { return createReadStream(this.getNodeExecutableLocation()) } - private async _fetchPrebuiltBinaryAsync(target: NexeTarget) { - let downloadOptions = this.options.downloadOptions - - if (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) - - if (!asset) { - throw new NexeError(`${assetName} not available, create it using the --build flag`) - } - const filename = this.getNodeExecutableLocation(target) - - 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) - } - private async _shouldCompileBinaryAsync( binary: NodeJS.ReadableStream | null, location: string | undefined @@ -307,10 +274,7 @@ export class NexeCompiler { const build = this.options.build const location = this.getNodeExecutableLocation(build ? undefined : target) let binary = (await pathExistsAsync(location)) ? createReadStream(location) : null - if (!build && !binary) { - step.modify('Fetching prebuilt binary') - binary = await this._fetchPrebuiltBinaryAsync(target) - } + if (await this._shouldCompileBinaryAsync(binary, location)) { binary = await this.build() step.log('Node binary compiled') diff --git a/src/nexe.ts b/src/nexe.ts index 315223f..936af6f 100644 --- a/src/nexe.ts +++ b/src/nexe.ts @@ -28,7 +28,8 @@ async function compile( cli, bundle, shim, - options.build ? [download, artifacts, ...patches, ...(options.patches as NexePatch[])] : [], + download, + options.build ? [artifacts, ...patches, ...(options.patches as NexePatch[])] : [], options.plugins as NexePatch[] )(compiler) } catch (e) { diff --git a/src/options.ts b/src/options.ts index b74f82f..e6190d8 100644 --- a/src/options.ts +++ b/src/options.ts @@ -165,7 +165,7 @@ function extractLogLevel(options: NexeOptions) { } function isName(name: string) { - return name && name !== 'index' + return name && name !== 'index' && name !== STDIN_FLAG } function extractName(options: NexeOptions) { diff --git a/src/steps/download.ts b/src/steps/download.ts index 37693f8..a84a74d 100644 --- a/src/steps/download.ts +++ b/src/steps/download.ts @@ -2,7 +2,9 @@ import download = require('download') import { pathExistsAsync } from '../util' import { LogStep } from '../logger' import { IncomingMessage } from 'http' -import { NexeCompiler } from '../compiler' +import { NexeCompiler, NexeError } from '../compiler' +import { getLatestGitRelease } from '../releases' +import { dirname } from 'path' function fetchNodeSourceAsync(dest: string, url: string, step: LogStep, options = {}) { const setText = (p: number) => step.modify(`Downloading Node: ${p.toFixed()}%...`) @@ -21,6 +23,39 @@ function fetchNodeSourceAsync(dest: string, url: string, step: LogStep, options .then(() => step.log(`Node source extracted to: ${dest}`)) } +async function fetchPrebuiltBinary (compiler: NexeCompiler, step: any) { + let downloadOptions = compiler.options.downloadOptions + const target = compiler.target + + if (compiler.options.ghToken) { + downloadOptions = Object.assign({}, downloadOptions) + downloadOptions.headers = Object.assign({}, downloadOptions.headers, { + Authorization: 'token ' + compiler.options.ghToken + }) + } + + const githubRelease = await getLatestGitRelease(downloadOptions) + const assetName = target.toString() + const asset = githubRelease.assets.find(x => x.name === assetName) + + if (!asset) { + throw new NexeError(`${assetName} not available, create it using the --build flag`) + } + const filename = compiler.getNodeExecutableLocation(target) + + await download(asset.browser_download_url, dirname(filename), compiler.options.downloadOptions).on( + 'response', + (res: IncomingMessage) => { + const total = +res.headers['content-length']! + let current = 0 + res.on('data', data => { + current += data.length + step!.modify(`Downloading...${((current / total) * 100).toFixed()}%`) + }) + } + ) +} + /** * Downloads the node source to the configured temporary directory * @param {*} compiler @@ -30,18 +65,26 @@ export default async function downloadNode(compiler: NexeCompiler, next: () => P const { src, log, - targets: [{ version }] - } = compiler - const { sourceUrl, downloadOptions } = compiler.options - const url = sourceUrl || `https://nodejs.org/dist/v${version}/node-v${version}.tar.gz` - const step = log.step(`Downloading Node.js source from: ${url}`) - if (await pathExistsAsync(src)) { - step.log('Source already downloaded') + target + } = compiler, + { version } = target, + { sourceUrl, downloadOptions, build } = compiler.options, + url = sourceUrl || `https://nodejs.org/dist/v${version}/node-v${version}.tar.gz` + const step = log.step(`Downloading ${build ? '' : 'pre-built '}Node.js${ + build ? `source from: ${url}` : '' + }`) + const exeLocation = compiler.getNodeExecutableLocation(build ? undefined : target) + const downloadExists = await pathExistsAsync(build ? src : exeLocation) + + if (downloadExists) { + step.log('Already downloaded...') return next() } - if (compiler.options.build) { + if (build) { await fetchNodeSourceAsync(src, url, step, downloadOptions) + } else { + await fetchPrebuiltBinary(compiler, step) } return next()