1

I have created a small CLI tool in typescript and have achieved to create a .exe out of it with nexe. A new use case is to write out some files that are bundled within the application: Let's say my CLI tool provides the user with empty template files that the user can then fill out.

A sample command would be: myapp.exe --action export-templates --outdir path/to/some/dir

What shall happen now is that the CLI tool will export the template files that it contains to this location.

I have bundled the files already, see an excerpt of my package.json:

"scripts": {
    "build": "npm run compile && nexe compiled/index.js --target windows-x64-10.16.0 --resource \"resources/**/*\""
  }

I tried to access the files with:

const fileBuffer = fs.readFileSync(path.join('__dirname', `/templates/mytemplate.doc`));

However, I come up with an exception: Error: ENOENT: no such file or directory, open 'C:\Users\Tom\compiled\templates\mytemplate.doc'

Can anyone tell me how to properly access the files inside my bundled .exe with fs?

Stefan
  • 33
  • 5

1 Answers1

1

okay, too bad I needed to find this out by myself, the documentation is really not great on this...

After stumbling upon some issues from 2016 an 2017 (mainly https://github.com/nexe/nexe/pull/93) I thought the solution would be to use nexeres. Well, turns out this maybe used to work, but sure does not any more. When added a require('nexeres') in my app it will run into an Error: Cannot find module 'nexeres' error.

So again I was searching issues and finally found the solution in https://github.com/nexe/nexe/issues/291: Simply use fs.readFile or fs.readFileSync with a relative path. My final code looks like this:

// iterate over all files in the 'templates' folder INSIDE the .exe
each(fs.readdirSync('templates'), (filename: string) => {
  const dataBuffer = fs.readFileSync(`templates/${filename}`);
  // do sth with that file data, e.g. export it to some location (outside the .exe)
  const stream = fs.createWriteStream(`${outDir}/${filename}`);
  stream.write(dataBuffer );
  stream.close();
});
Stefan
  • 33
  • 5