diff --git a/src/fs/README.md b/src/fs/README.md index 543dc45..181c364 100644 --- a/src/fs/README.md +++ b/src/fs/README.md @@ -1,53 +1,40 @@ # nexe-fs -This module contains a set of patches used to create (and use) the nexe virtual filesystem - ---- - ### Getting Started: -To patch the module loader, monkey patches must be installed on a few methods at the time that node boots up. This requires a special build of node. Nexe provides these builds or the patch can be applied from this module manually in your own build setup. +This module contains a set of patches used to create (and use) the Nexe Virtual FileSystem -```javascript -const bootstrapPatch = fs.readFileSync(require.resolve('nexe-fs/bootstrap'), 'utf-8') -// insert boostrapPatch wherever node starts up. For reference, Nexe does this -// in third-party-main.ts -``` +In order for NVFS to work with node's `require` method. A custom build of node must be used. If using the `nexe` cli this will be handled automatically -To create a virtual file system use the `Bundle` Object +The customization should be applied as early as possbile when node is starting up. + +i.e. The contents of `nexe-fs/bootstrap` should be put [here](https://github.com/nodejs/node/blob/0827c80920311fa9d1e6989c8a73aaaeca962eb7/lib/internal/bootstrap/node.js#L27-L28). + +### Creating a NVFS: + +To create a virtual file system use the `Bundle` Object, in your build process: ```javascript const { Bundle } = require('nexe-fs') -const bundle = new Bundle({ cwd }) +const bundle = new Bundle() await bundle.addResource(absoluteFileName) bundle.toStream().pipe(fs.createWriteStream('./myFsBlob')) - -// You'll also want to save the bundle index to use at runtime -// It can be saved by accessing bundle.index (object/hash) or serializing the bundle with JSON.stringify(bundle) ``` -In the entrypoint of the custom node build, the fs patch needs to be applied -```javascript -const fakeFs = fs.readFileSync(require.resolve('nexe-fs/patch'), 'utf-8') -//apply this code to your entrypoint -``` - -The patch exposes two methods, `shimFs` and `restoreFs` to be used as follows: +In the entrypoint of your application, the fs patch needs to be applied (executed as early as possible) +For example, In the build process, the application's entrypoint could be prepended with the following: ```javascript +const codeToPrependToEntrypoint = fs.readFileSync(require.resolve('nexe-fs/patch'), 'utf-8') + ` shimFs({ blobPath: 'location/of/myFsBlob', - resources: bundle.index, + resources: ${JSON.stringify(bundle)}, layout: { - stat: blobStats //fs.Stats object about the blob at blobPath resourceStart: 0 // the offset within the blob that is referenced by the bundle index } -}) +})` ``` -At this point executing the original entrypoint shoudl result in usage of the virtual filesystem. - - - +Since a custom node build is being used. A great place for this code would be the node source file: `_third_party_main.js` diff --git a/test/fixture2/index.js b/test/fixture2/index.js new file mode 100644 index 0000000..e69de29 diff --git a/test/options.spec.ts b/test/options.spec.ts index b1434bc..d21e2fd 100644 --- a/test/options.spec.ts +++ b/test/options.spec.ts @@ -49,8 +49,8 @@ describe('options', () => { expect(options.output).to.equal(path.resolve(`./entry${ext}`)) }) it('should default to the folder/project name if filename is index', () => { - const options = normalizeOptions() - expect(options.output).to.equal(path.resolve(`./nexe${ext}`)) + const options = normalizeOptions({ cwd: './test/fixture2' }) + expect(options.output).to.equal(path.resolve(`./test/fixture2/fixture2${ext}`)) }) }) })