how to get exe files using fs.readdirsync() in electron js - node.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.

Related

How can I read a pdf file with express and node

I'm trying to read a pdf... and my idea is to convert it to a text. I have read the pdf-parser documentation and I don't understand why it is giving me this error, has anyone ever used pdf-parser?
Has anyone had this error?
It would be very helpful, I have never worked with it and the videos I watched use it very easily, but it breaks the code for me.
const url = require("./prueba.pdf");
const pdf = require("pdf-parse");
const fs = require("fs");
const pdffile = fs.readFileSync(url);
console.log(pdffile);
pdf(pdffile).then(function (data) {
console.log(data.text);
});
The path ./prueba.pdf is not correct, unless you run the node command from the ..\api\src\routes subdirectory, which I doubt.
Unlike paths in a require command, paths in an fs.readFileSync command are interpreted relative to the directory where you started the node process.

How to bundle and require non JS dependencies in Firebase Cloud Functions?

I have an http cloud function that returns some dynamic HTML. I want to use Handlebars as the templating engine. The template is sufficiently big that it's not practical to have it in a const variable on top of my function.
I've tried something like:
const template = fs.readFileSync('./template.hbs', 'utf-8');
But when deploying the function I always get an error that the file does not exist:
Error: ENOENT: no such file or directory, open './template.hbs'
The template.hbs is in the same directory as my index.js file so I imagine the problem is that the Firebase CLI is not bundling this file along the rest of files.
According to the docs of Google Cloud Functions it is possible to bundle local modules with "mymodule": "file:mymodule". So I've tried creating a templates folder at the root of the project and added "templates": "file:./templates" to the package.json.
My file structure being something like this:
/my-function
index.js
/templates
something.hbs
index.js //this is the entry point
And then:
const template = fs.readFileSync('../node_modules/templates/something.hbs', 'utf-8');
But I'm getting the same not found error.
What is the proper way of including and requiring a non JS dependencies in a Firebase Cloud Function?
The Firebase CLI will package up all the files in your functions folder, except for node_modules, and send the entire archive to Cloud Functions. It will reconstitue node_modules by running npm install while building the docker image that runs your function.
If your something.hbs is in /templates under your functions folder, you should be able to refer to it as ./templates/something.hbs from the top-level index.js. If your JS is in another folder, you might have to work you way out first with ../templates/something.hbs. The files should all be there - just figure out the path. I wouldn't try to do anything fancy is your package.json. Just take advantage of the fact that the CLI deploys everything but node_modules.
This code works fine for me if I have a file called 'foo' at the root of my functions folder:
import * as fs from 'fs'
export const test = functions.https.onRequest((req, res) => {
const foo = fs.readFileSync('./foo', 'utf-8')
console.log(foo)
res.send(foo)
})
The solution was to use path.join(__dirname,'template.hbs').
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname,'template.hbs'), 'utf-8');
As #doug-stevenson pointed out all files are included in the final bundle but for some reason using the relative path did not work. Forcing an absolute path with __dirname did the trick.

Electron-builder - path to include sqlite database in dev && production

I'm using this template
https://github.com/SimulatedGREG/electron-vue
and including a sqlite file in static/db/database.sqlite
in my main.js I have this
const SQL = require('sqlite3').verbose();
var path = require('path');
const db = new SQL.Database(path.join(__static, '/db/database.sqlite'));
it works fine for dev, but when building for production, I receive this message in devtools:
SQLITE_CANTOPEN: unable to open database file
Looks like another case of messed up path variables after packaging. I would recommend to debug your paths, for instance with fs:
var fs = require('fs');
fs.writeFileSync('mylog.txt', __dirname);
Reference: Error while running execFileSync in packaged Electron app
Sorry, forgot to update this here:
https://github.com/SimulatedGREG/electron-vue/issues/630
Basically, it's supposed to user the userData folder in order to use read/write operations.. so... _static is not intended to be a folder to write content to.

Working when run, but not when built

Im on macOS. I am creating a simple electron app. When I run the app with electron . everything works perfectly with no errors. Now that my app is finished, I wanted to build and distribute it. So I setup electron-builder and I got that to work just fine. However, when I run the MyApp.app in the build folder, I get an error saying:
Uncaught Error: ENOENT: no such file or directory, scandir './img/'
I call scandir here:
const fs = require('fs');
var files = [];
fs.readdirSync("./img/").forEach(file => {
files.push(file);
})
Why is this working when I run it with node, but is not working in the build? How can I fix this issue?
Why is this working when I run it with node, but is not working in the
build? How can I fix this issue?
It's difficult to tell without having more information about the whole app's structure, it may depend on how your code is actually called or required from the html file.
Anyway, using the __dirname global variable to build the directory path usually solves this kind of problem. Please try:
const fs = require('fs');
const path = require('path');
var files = [];
fs.readdirSync(path.join(__dirname, 'img')).forEach(file => {
files.push(file);
});

Get application full path in Node.js

I'm wondering if there is a best way or best practice to get the full path of application in Node.js. Example: I have a a module in sub folder /apps/myapp/data/models/mymodel.js, and I would like to get the full path of the app (not full path of the file), which will return me /apps/myapp, how do I do that? I know _dirname or _file is only relative to the file itself but not the full path of the app.
There's probably a better solution, BUT this should work:
var path = require('path');
// find the first module to be loaded
var topModule = module;
while(topModule.parent)
topModule = topModule.parent;
var appDir = path.dirname(topModule.filename);
console.log(appDir);
EDIT: Andreas proposed a better solution in the comments:
path.dirname(require.main.filename)
EDIT: another solution by Nam Nguyen
path.dirname(process.mainModule.filename)
This worked for me.. With supervisor running the app from a different dir.
require('path').dirname(Object.keys(require.cache)[0])
example.. files:
/desktop/ya/node.js
require('./ya2/submodule')();
/desktop/ya/ya2/submodule.js
module.exports = function(){
console.log(require('path').dirname(Object.keys(require.cache)[0]))
}
$ node node.js
=> /desktop/ya
$ (from /desktop) supervisor ya/node.js
=> /desktop/ya

Resources