using shell.openItem() to open a file in an asar archive - node.js

I have an electron app that uses an app.asar file. I want to be able to open files (.xlsx) that are in the asar file in the default Windows application (which is Excel). I have tried
let xlsx = path.join(__dirname + '/path/to/app.asar/path/to/file');
shell.openItem(xlsx);
but it doesn't work (no error, the file doesn't open).
I can read the file with fs
let xlsx = path.join(__dirname + '/path/to/app.asar/path/to/file');
fs.readFileSync(xlsx);
but I can't get the file to open in Excel.

From the documentation for Application Packaging
An asar archive is a simple tar-like format that concatenates files into a single file. Electron can read arbitrary files from it without unpacking the whole file.
Only Electron can access those files, Excel and other applications simply can not deal with asar archives.
You could copy the file from the asar archive to the system's temp folder and open it from there. Like this:
const fs = require('fs')
const path = require('path')
const {shell, app} = require('electron')
let xlsx = path.join(__dirname,'/path/to/app.asar/path/to/file')
let xlsxtmp = üath.join(app.getPath('temp', 'file')
let ws = fs.createWriteStream(xlsxtmp)
fs.createReadStream(xlsx).pipe(ws)
ws.on('finish', () => {
shell.openItem(xlsxtmp);
}
Another option would be to not pack that xlsx into the archive and instead download it into the userData path.

Related

Nestjs read file from assets directory

I'm using nestjs 8. I want to access and read file from assets directory.
const fs = require('fs');
const data = fs.readFile('assets/audio/sound-1.mp3')
With this code it doesn't find the file.
How can I access resource file from assets directory?
Thanks in advance

Determining the path in fs.readFile()

var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
This is the code for my app (index.js) . It uses a file named config.json using a relative path ./config.json. When I compile this into my mac using $pkg index.js, I obtain a test-macos file that is my app. Both the app and the config.json file are located in the same folder named project (this is why I can use the relative path ./config.json). However, once runnning the app I run into an error:
/Users/amelie/Documents/project/test-macos ;
exit; internal/fs/utils.js:312
throw err;
^ Error: ENOENT: no such file or directory, open './config.json'.
Therefore it seems that my app cannot find the config.json file. I could determine an absolute path but I need these two files to always be together because I need the path in the variable
var obj = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
to be always the same since I share those files to other people and the absolute path would therefore change. Is there a way to do this ?

how to get exe files using fs.readdirsync() in electron js

I want to get the list of exe or desktop application(installed local PC) in electron js desktop application.I tried require('fs') but it list only folder from root path.
const fs = require('fs');
const root = fs.readdirSync('/')
console.log(root);
how to get only exe file
A simple way on Windows. Might take some time to retrieve the list.
const { execSync } = require('child_process');
let list = execSync('wmic product get name,version');
console.log(list.toString());
The best way would be writing a native node module using N-API to do this.

Path to Application Support on Mac in Node.js

I'm attempting to read a file on the path on Mac ~/Library/Application Support/file_name.cert but cannot reach it using the fs.readFileSync Node.js module.
How do I read this file?
From what you wrote I am guessing the '~' character is the culprit. Your shell normally replaces this with your home directory, but this is not the case with Node.js (or any other program). So you'll have to replace it with your home directory like so:
// Incorrect path due to ~
//const filename = '~/Library/Application Support/file_name.cert';
// Correct path
const filename = process.env.HOME + '/Library/Application Support/file_name.cert';
// Now read will succeed
const data = fs.readFileSync(filename, 'utf8');

Node fs.writefile from Mac to Windows Share

While developing a Node application on a Mac, I attempted to write a file into a Windows share using the fs module. It seems to be treating the entire path as a file name, and instead of saving to the file to the proper directory, it saves it to the working directory.
const fs = require('fs')
var filePath = '\\\\hostname\\share\\...\\test.txt'
fs.writeFile(filePath, 'testing', function(err){
if(err) console.error(err)
})
Is there a simple way to access this share from a Mac?

Resources