From 9e5ab3e0993ad608dd3698e1d0673e0597b66b87 Mon Sep 17 00:00:00 2001 From: calebboyd Date: Thu, 31 Aug 2017 01:15:51 -0500 Subject: [PATCH] chore: options, and resource normalization --- src/bundling/fuse.ts | 16 +++++++++---- src/options.ts | 42 ++++++++++++++++++++++++++------- src/patches/third-party-main.ts | 21 ++++++++++++++++- src/steps/cli.ts | 20 +++------------- tasks/bump.ts | 0 tasks/releases.ts | 2 +- 6 files changed, 70 insertions(+), 31 deletions(-) delete mode 100644 tasks/bump.ts diff --git a/src/bundling/fuse.ts b/src/bundling/fuse.ts index 0e5fc79..f3b90eb 100644 --- a/src/bundling/fuse.ts +++ b/src/bundling/fuse.ts @@ -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 }) diff --git a/src/options.ts b/src/options.ts index eb02eb3..5bf2cc7 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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): Promise { if (argv.help || argv._.some((x: string) => x === 'version') || argv.version === true) { return new Promise(() => { @@ -166,8 +190,10 @@ function normalizeOptionsAsync(input?: Partial): Promise Promise 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)) { diff --git a/src/steps/cli.ts b/src/steps/cli.ts index f8803fd..284d9bc 100644 --- a/src/steps/cli.ts +++ b/src/steps/cli.ts @@ -32,29 +32,15 @@ function readStreamAsync(stream: NodeJS.ReadableStream): PromiseLike { * @param {*} next */ export default async function cli(compiler: NexeCompiler, next: () => Promise) { - 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` diff --git a/tasks/bump.ts b/tasks/bump.ts deleted file mode 100644 index e69de29..0000000 diff --git a/tasks/releases.ts b/tasks/releases.ts index ab1dc5b..7e17d5b 100644 --- a/tasks/releases.ts +++ b/tasks/releases.ts @@ -28,7 +28,7 @@ async function getJson (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' }