I am working with Nodejs on Windows 10. Every time the app is opened I want to check if the files with the extension ext
are associated to my app. If not I would need to run this commands on cmd in order to make the association again:
assoc .ext=CustomExt
ftype CustomExt=app.exe %1 %*
But I would need administrator privileges to make this work.
I have read this thread where Jeff M says exactly what I want to achieve, but nobody answers him:
From a usability perspective, the ideal would be that the user is prompted for elevated privileges only when he takes an action within my node program that actually requires those privileges. The second best option would be if the user was prompted for elevated privileges when they first start the node program. The least ideal is if the user isn't prompted at all, that it would be up to the user to always remember to start the program by right-click run as administrator. This is the least ideal because it puts to the burden on the user to remember to always start the program in a non-typical way.
So, to get back to my original question, right now if I try to spawn a child process where the program being spawned requires elevated privileges (example code in my original message), the current behavior is that the user is not prompted for those privileges, and instead the node program crashes with an error. How can I get my node program to request those privileges instead of just dying?
My nodejs script:
const { spawn } = require('child_process');
const assoc = spawn( // how to run this as admin privileges?
'assoc', // command
['.ext=CustomExt'], // args
{'shell': true } // options: run this into the shell
);
assoc.stdout.on('data', (buffer) => {
var message = buffer.toString('utf8');
console.log(message);
});
assoc.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
assoc.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Any other way to create an extension association within Nodejs would be appreciated as well.
Update 21 Jan 2020
I believe that VSCode can do this. So I would need to explore its source code to find the solution. Now I am in other projects, so I do not have time to research for a while. When a file should be saved with root priviledges a message like this is prompted, and that's exactly what I need:
Update 24 June 2021
I have found a useful thread in VSCode issues. Using sudo-prompt
could be the solution. I haven't tried it yet, but the dependency is still in the package.json
, so it looks promising.