chore: options, and resource normalization

This commit is contained in:
calebboyd
2017-08-31 01:15:51 -05:00
parent f949bf3040
commit fc99386199
6 changed files with 70 additions and 31 deletions
+12 -4
View File
@@ -1,8 +1,9 @@
import { NexeCompiler } from '../compiler'
import { FuseBox, JSONPlugin, CSSPlugin, HTMLPlugin, QuantumPlugin } from 'fuse-box'
import { readFileAsync } from '../util'
//import NativeModulePlugin from './fuse-native-module-plugin'
function bundleProducer(filename: string, options: { name: string; minify: any }) {
function createBundle(filename: string, options: { name: string; minify: any; cwd: string }) {
const plugins: any = [JSONPlugin(), CSSPlugin(), HTMLPlugin()]
if (options.minify) {
plugins.push(
@@ -16,7 +17,7 @@ function bundleProducer(filename: string, options: { name: string; minify: any }
const fuse = FuseBox.init({
cache: false,
log: Boolean(process.env.NEXE_BUNDLE_DEBUG) || false,
homeDir: process.cwd(),
homeDir: options.cwd,
sourceMaps: false,
writeBundles: false,
output: '$name.js',
@@ -33,15 +34,22 @@ function bundleProducer(filename: string, options: { name: string; minify: any }
export default async function bundle(compiler: NexeCompiler, next: any) {
if (!compiler.options.bundle) {
compiler.input = await readFileAsync(compiler.options.input, 'utf-8')
return next()
}
let producer = bundleProducer
if (compiler.options.empty || !compiler.options.input) {
compiler.input = ''
return next()
}
let producer = createBundle
if (typeof compiler.options.bundle === 'string') {
producer = require(compiler.options.bundle).bundleProducer
producer = require(compiler.options.bundle).createBundle
}
compiler.input = await producer(compiler.options.input, {
cwd: compiler.options.cwd,
name: compiler.options.name,
minify: compiler.options.compress
})
+34 -8
View File
@@ -1,7 +1,7 @@
import * as parseArgv from 'minimist'
import { NexeCompiler } from './compiler'
import { isWindows } from './util'
import { basename, extname, join, isAbsolute } from 'path'
import { basename, extname, join, isAbsolute, relative } from 'path'
import { getTarget, NexeTarget } from './target'
import { EOL } from 'os'
@@ -18,6 +18,7 @@ export interface NexeOptions {
compress: boolean
targets: (string | NexeTarget)[]
name: string
cwd: string
version: string
flags: string[]
configure: string[]
@@ -51,16 +52,16 @@ function padRight(str: string, l: number) {
}
const defaults = {
temp: process.env.NEXE_TEMP || join(process.cwd(), '.nexe'),
version: process.version.slice(1),
flags: [],
cwd: process.cwd(),
configure: [],
make: [],
targets: [],
vcBuild: isWindows ? ['nosign', 'release', process.arch] : [],
enableNodeCli: false,
compress: false,
build: false,
build: true,
bundle: true,
patches: []
}
@@ -100,6 +101,7 @@ nexe --help CLI OPTIONS
--bundle =./path/to/config -- pass a module path that exports nexeBundle
--temp =./path/to/temp -- nexe temp files (for downloads and source builds)
--no-bundle -- set when input is already bundled
--cwd -- set the current working directory for the command
--ico -- file name for alternate icon file (windows)
--rc-* -- populate rc file options (windows)
--clean -- force download of sources
@@ -131,10 +133,10 @@ function extractCliMap(match: RegExp, options: any) {
}, {})
}
function tryResolveMainFileName() {
function tryResolveMainFileName(cwd: string) {
let filename
try {
const file = require.resolve(process.cwd())
const file = require.resolve(cwd)
filename = basename(file).replace(extname(file), '')
} catch (_) {}
@@ -150,13 +152,35 @@ function extractLogLevel(options: NexeOptions) {
function extractName(options: NexeOptions) {
let name = options.name
if (typeof options.input === 'string' && !name) {
if (!name && typeof options.input === 'string') {
name = basename(options.input).replace(extname(options.input), '')
}
name = name || tryResolveMainFileName()
name = name || tryResolveMainFileName(options.cwd)
return name.replace(/\.exe$/, '')
}
function isEntryFile(filename: string) {
return filename && !isAbsolute(filename) && filename !== 'node' && /\.(tsx?|jsx?)$/.test(filename)
}
function findInput(input: string, cwd: string) {
const maybeInput = argv._.slice().pop() || ''
if (input) {
return input
}
if (isEntryFile(maybeInput)) {
return maybeInput
}
try {
const main = require.resolve(cwd)
return './' + relative(cwd, main)
} catch (e) {
void e
}
return ''
}
function normalizeOptionsAsync(input?: Partial<NexeOptions>): Promise<NexeOptions | never> {
if (argv.help || argv._.some((x: string) => x === 'version') || argv.version === true) {
return new Promise(() => {
@@ -166,8 +190,10 @@ function normalizeOptionsAsync(input?: Partial<NexeOptions>): Promise<NexeOption
const options = Object.assign({}, defaults, input) as NexeOptions
const opts = options as any
options.loglevel = extractLogLevel(options)
options.temp = process.env.NEXE_TEMP || join(options.cwd, '.nexe')
options.name = extractName(options)
options.input = findInput(options.input, options.cwd)
options.loglevel = extractLogLevel(options)
options.flags = flattenFilter(opts.flag, options.flags)
options.targets = flattenFilter(opts.target, options.targets)
options.make = flattenFilter(options.vcBuild, options.make)
+20 -1
View File
@@ -7,6 +7,7 @@ export default async function main(compiler: NexeCompiler, next: () => Promise<v
'lib/_third_party_main.js',
`
const fs = require('fs')
const path = require('path')
const Buffer = require('buffer').Buffer
const isString = x => typeof x === 'string' || x instanceof String
@@ -45,8 +46,25 @@ Object.defineProperty(process, '__nexe', (function () {
if (resourceSize) {
const originalReadFile = fs.readFile
const originalReadFileSync = fs.readFileSync
let setupManifest = () => {
const manifest = process.__nexe && process.__nexe.resources
if (!manifest) {
return
}
Object.keys(manifest).forEach((key) => {
const absolutePath = path.resolve(key)
if (!manifest[absolutePath]) {
manifest[absolutePath] = manifest[key]
}
const normalizedPath = path.normalize(key)
if (!manifest[normalizedPath]) {
manifest[normalizedPath] = manifest[key]
}
})
normalizeManifest = () => {}
};
fs.readFile = function readFile (file, options, callback) {
setupManifest()
const manifest = process.__nexe
const entry = manifest && manifest.resources[file]
if (!manifest || !entry || !isString(file)) {
@@ -77,6 +95,7 @@ if (resourceSize) {
}
fs.readFileSync = function readFileSync (file, options) {
setupManifest()
const manifest = process.__nexe
const entry = manifest && manifest.resources[file]
if (!manifest || !entry || !isString(file)) {
+3 -17
View File
@@ -32,29 +32,15 @@ function readStreamAsync(stream: NodeJS.ReadableStream): PromiseLike<string> {
* @param {*} next
*/
export default async function cli(compiler: NexeCompiler, next: () => Promise<void>) {
const { input, output } = compiler.options
const { output } = compiler.options
const { log, input: bundledInput } = compiler
if (!input && !process.stdin.isTTY) {
if (!process.stdin.isTTY) {
log.step('Using stdin as input')
compiler.input = await readStreamAsync(process.stdin)
} else if (bundledInput) {
await next()
} else if (input) {
log.step(`Using input file as the main module: ${input}`)
compiler.input = await readFileAsync(normalize(input), 'utf-8')
} else if (!compiler.options.empty) {
const bundle = require.resolve(process.cwd())
log.step("Using the cwd's main file as the main module")
compiler.input = await readFileAsync(bundle, 'utf-8')
} else {
log.step('Using empty input as the main module')
compiler.input = ''
}
if (!bundledInput) {
await next()
}
await next()
compiler.output = isWindows
? `${(output || compiler.options.name).replace(/\.exe$/, '')}.exe`
View File
+1 -1
View File
@@ -28,7 +28,7 @@ async function getJson<T> (url: string) {
//TODO only build the latest of each major...?
function isBuildableVersion (version: string) {
const major = +version.split('.')[0]
return ![0, 1, 2, 3, 4, 5, 7].includes(major)
return !~[0, 1, 2, 3, 4, 5, 7].indexOf(major)
|| version === '4.8.4'
}