From a4b48611d964a83c96d9b1b00ffeae2cf0212355 Mon Sep 17 00:00:00 2001 From: calebboyd Date: Tue, 3 Oct 2017 00:39:33 -0500 Subject: [PATCH] feat: readdir and readdirSync --- src/steps/shim-fs.ts | 38 +++++++++++++++++++++++++++++++++++--- src/steps/shim.ts | 2 ++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/steps/shim-fs.ts b/src/steps/shim-fs.ts index ac0d5f0..c26bbd6 100644 --- a/src/steps/shim-fs.ts +++ b/src/steps/shim-fs.ts @@ -1,22 +1,27 @@ import { Stats } from 'fs' import { ok } from 'assert' -import { resolve, normalize } from 'path' +import { dirname, resolve, normalize, basename } from 'path' const binary = (process as any).__nexe as NexeBinary ok(binary) const manifest = binary.resources +const directories: { [key: string]: { [key: string]: boolean } } = {} const isString = (x: any): x is string => typeof x === 'string' || x instanceof String if (Object.keys(manifest).length) { const fs = require('fs') const originalReadFile = fs.readFile const originalReadFileSync = fs.readFileSync + const originalReaddir = fs.readdir + const originalReaddirSync = fs.readdirSync const resourceStart = binary.layout.resourceStart let setupManifest = () => { - const manifest = binary.resources Object.keys(manifest).forEach(key => { const absolutePath = resolve(key) + const dirPath = dirname(absolutePath) + directories[dirPath] = directories[dirPath] || {} + directories[dirPath][basename(absolutePath)] = true if (!manifest[absolutePath]) { manifest[absolutePath] = manifest[key] } @@ -27,8 +32,35 @@ if (Object.keys(manifest).length) { }) setupManifest = () => {} } - //TODO track inflight fs reqs?? + //naive patches intended to work for most use cases var nfs = { + readdir: function readdir(path: string | Buffer, options: any, callback: any) { + setupManifest() + path = path.toString() + if ('function' === typeof options) { + callback = options + options = { encoding: 'utf8' } + } + const dir = directories[resolve(path)] + if (dir) { + process.nextTick(() => { + callback(null, Object.keys(dir)) + }) + } else { + return originalReaddir.apply(fs, arguments) + } + }, + + readdirSync: function readdirSync(path: string | Buffer, options: any) { + setupManifest() + path = path.toString() + const dir = directories[resolve(path)] + if (dir) { + return Object.keys(dir) + } + return originalReaddirSync.apply(fs, arguments) + }, + readFile: function readFile(file: any, options: any, callback: any) { setupManifest() const entry = manifest[file] diff --git a/src/steps/shim.ts b/src/steps/shim.ts index 86cbe8f..312d6cc 100644 --- a/src/steps/shim.ts +++ b/src/steps/shim.ts @@ -21,6 +21,8 @@ export default function(compiler: NexeCompiler, next: () => Promise) { compiler.shims.push(wrap('{{replace:lib/steps/shim-fs.js}}')) } + //compiler.shims.push(wrap('{/{replace:lib/steps/shim-require.js}}')) + if (compiler.options.fakeArgv) { const nty = !process.stdin.isTTY const input = nty ? '[stdin]' : compiler.options.input