如何使用 javascript 访问 nexe 编译的 .exe 中的捆绑文件
how to access bundled files in compiled .exe by nexe with javascript
我已经用打字稿创建了一个小的 CLI 工具,并且已经实现了用 nexe 从中创建一个 .exe。
一个新的用例是写出一些捆绑在应用程序中的文件:
假设我的 CLI 工具为用户提供了空模板文件,然后用户可以填写这些文件。
示例命令为:myapp.exe --action export-templates --outdir path/to/some/dir
现在应该发生的是 CLI 工具会将其包含的模板文件导出到此位置。
我已经打包了文件,请看我的摘录 package.json:
"scripts": {
"build": "npm run compile && nexe compiled/index.js --target windows-x64-10.16.0 --resource \"resources/**/*\""
}
我试图通过以下方式访问文件:
const fileBuffer = fs.readFileSync(path.join('__dirname', `/templates/mytemplate.doc`));
但是,我想到了一个例外:
Error: ENOENT: no such file or directory, open 'C:\Users\Tom\compiled\templates\mytemplate.doc'
谁能告诉我如何使用 fs 正确访问捆绑的 .exe 中的文件?
好吧,太糟糕了,我需要自己找到这个,文档在这方面真的不是很好...
在 2016 年和 2017 年(主要是 https://github.com/nexe/nexe/pull/93)遇到一些问题后,我认为解决方案是使用 nexeres
。好吧,事实证明这可能曾经有用,但肯定不再有用了。当在我的应用程序中添加 require('nexeres')
时,它将 运行 变成 Error: Cannot find module 'nexeres'
错误。
所以我再次搜索问题,最终在 https://github.com/nexe/nexe/issues/291 中找到了解决方案:只需使用 fs.readFile
或 fs.readFileSync
和相对路径即可。我的最终代码如下所示:
// 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();
});
我已经用打字稿创建了一个小的 CLI 工具,并且已经实现了用 nexe 从中创建一个 .exe。 一个新的用例是写出一些捆绑在应用程序中的文件: 假设我的 CLI 工具为用户提供了空模板文件,然后用户可以填写这些文件。
示例命令为:myapp.exe --action export-templates --outdir path/to/some/dir
现在应该发生的是 CLI 工具会将其包含的模板文件导出到此位置。
我已经打包了文件,请看我的摘录 package.json:
"scripts": {
"build": "npm run compile && nexe compiled/index.js --target windows-x64-10.16.0 --resource \"resources/**/*\""
}
我试图通过以下方式访问文件:
const fileBuffer = fs.readFileSync(path.join('__dirname', `/templates/mytemplate.doc`));
但是,我想到了一个例外:
Error: ENOENT: no such file or directory, open 'C:\Users\Tom\compiled\templates\mytemplate.doc'
谁能告诉我如何使用 fs 正确访问捆绑的 .exe 中的文件?
好吧,太糟糕了,我需要自己找到这个,文档在这方面真的不是很好...
在 2016 年和 2017 年(主要是 https://github.com/nexe/nexe/pull/93)遇到一些问题后,我认为解决方案是使用 nexeres
。好吧,事实证明这可能曾经有用,但肯定不再有用了。当在我的应用程序中添加 require('nexeres')
时,它将 运行 变成 Error: Cannot find module 'nexeres'
错误。
所以我再次搜索问题,最终在 https://github.com/nexe/nexe/issues/291 中找到了解决方案:只需使用 fs.readFile
或 fs.readFileSync
和相对路径即可。我的最终代码如下所示:
// 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();
});