refactor: run download for src and prebuilt before plugins or patches
This commit is contained in:
+1
-2
@@ -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
|
||||
|
||||
+1
-1
@@ -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 <craig.j.condon@gmail.com> (http://crcn.io)",
|
||||
"Jared Allard <jaredallard@outlook.com>",
|
||||
|
||||
+1
-37
@@ -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')
|
||||
|
||||
+2
-1
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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) {
|
||||
|
||||
+52
-9
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user