Execute createShortcut in a Squirrel event, in an Electron app - node.js

I'm trying to create shortcuts for my Electron app when I install it or update it, however I am having some trouble executing the command that is meant to create the shortcut.
By default Electron apps are "SquirrelAware", therefore I have to specify where i would like to create shortcuts.
My question is in relation to the accepted answer to this question.
Handle Squirrel's event on an Electron app
I have tried to use the exec module and the child_process module, however both did not seem to work. I am now currently attempting (and failing) to launch PowerShell and run a script in there that will create shortcuts on my Start Menu and Desktop, however I feel this is rather long and that there must be an easier way.
Here is my current attempt using the child_process module and PowerShell:
var spawn = require('child_process').spawn, child;
child = spawn("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",["C:\\ElectronSquirrelDemo\\AddAppShortcuts.ps1 -SourceExe "+ executionPath] );
child.stdout.on('data', function(data){
console.log("PowerShell Data: " + data);
});
child.stdout.on('data', function(data){
console.log("PowerShell Error: " + data);
});
child.stdout.on('exit', function(){
console.log('PowerShell script finished');
});
Any help on this would be much appreciated

It took me awhile to understand how to do this myself. The Squirrel.Windows Update.exe has the ability to create shortcuts to your app for you. I wrote a blog post called Creating a Windows Distribution of an Electron App using Squirrel and in it I have Squirrel create the shortcuts for me. If you want to go this route, this is simplified version of how to have Squirrel create the shortcuts for you:
var cp = require('child_process');
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var target = path.basename(process.execPath);
var child = cp.spawn(updateDotExe, ["--createShortcut", target], { detached: true });
child.on('close', function(code) {
app.quit();
});
You need to hack the electron executable using Resource Hacker, rcedit, or another application to change the ProductName and Icon resources. You'll want to call the above code on both the install and updated Squirrel events.

Related

Attach to current process on nodejs over ubuntu server terminal

I have a process running over a ubuntu server. I only have access on terminal.
I made this script:
index.js
let i = 0;
setInterval( () => {
i++;
console.log(`try ${i}`);
}, 1000);
I run with: node index.js &
Now I open a new terminal and I want to see the result on console.log.
How can I do it?
New edit:
The principal idea is send a console.log in a terminal and, recovery this console.log in another terminal. This is the goal. Recovery the console log in another terminal. How can I do it?
Of top of my head, I'd suggest going for a combination of https://www.npmjs.com/package/commander and some sort of IPC - redis-based,*mq or plain https://www.npmjs.com/package/node-ipc
If you don't have wide variety of commands you'd need to send to that process, you can also get away using signals, for example: SIGUSR1, SIGUSR2.

how to change node.js variables from terminal

I'm trying to run a nodeJS service that contains some parameters, then once it is running, stores a lot of session variables.
My script shouldn't be restarted because of this, all these working variables should remain the same.
Is it possible to change some global variables inside the running nodejs script while it is running? I was hoping the node would actually be a console where I can run JS in it but it appear it is not, it only outputs the console.log() method and errors, there's no input.
I know I could create a method inside my script to change these variables, but there are many, and I dont want to create a function that can handle them all because this would be very insecure.
Is there something I've missed about the node console inside terminal?
In fine, the script will be running with 'forever'
What do you mean no input?
node.js can read from the terminal like any other technology.
You can look at: https://nodejs.org/api/readline.html
They have a simple example:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of Node.js? ", function(answer) {
// TODO: Log the answer in a database
console.log("Thank you for your valuable feedback:", answer);
rl.close();
});
I wrote a command line installer for our app using this.

How to detect browser close event in node js?

I have used Electron(formerly Atom Shell) to create desktop app using NodeJS.
I am using following code to open an url into browser
var spawn = require('child_process').spawn
spawn('open', ['http://www.stackoverflow.com']);
Please note that I am not using Electron BrowserWindow, this is a regular browser window.
I want to perform an action when user closes this browser window. How do I detect close event of this spawned browser?
Try utilizing the event emitter
var spawn = require('child_process').spawn,
browser = spawn('open', ['http://www.stackoverflow.com']);
browser.on('close', function() {
// Handle event
});

How to access a Child Process' sockets in NodeJS?

I am currently running into a very strange problem.
I am trying to start VLC using a NodeJS child process and then accessing it's Remote Control (RC) interface using socket. The problem occurs when connecting to this socket. I get an error, connection refused. The port is open and the application is allowed from the firewall.
The tricky part is, when I open VLC manually using this interface, and only try to connect on the socket, it works. I am assuming something in the spawned process makes things different causing the error somehow.
Here is the code I am trying to run:
var spawn = require('child_process').spawn;
var file_dir = "V:\\TEST\\";
var files = ["Ika.mkv", "Nami.mkv", "Azu.mkv"];
var player = spawn("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe", ['--intf="rc"', '--rc-host="localhost:3000"', '--fullscreen', file_dir + files[0]]);
var net = require('net');
var client = net.createConnection(3000, "localhost");
client.on('connect', function() {
console.log('connected to VLC on port 3000');
client.write("add " + file_dir + files[1] + "\n");
client.write("enqueue " + file_dir + files[2] + "\n");
client.write("help" + "\n");
});
client.on('data', function(data) {
console.log(data.toString());
});
client.on('end', function() {
console.log('disconnected from server');
});
I have tried this code on two machines, and I am running into the same problem.
Some questions you may ask:
What operating system? Windows 8.1
Why do I need to use a socket?
VLC doesn't have any interfaces that read and write from standard in or standard out. I have tried many different options and they simply do nothing.
What am I trying to build?
A Media Center with an web interface to it. I am using VLC as a media player.
Can't you use the built in HTTP interface?
It doesn't suit what I want to build. I want more control over managing my media.
Any and all help would be welcome. My thanks.
Turns out for some strange reason not all command line arguments get passed to the VLC instance.
I solved it by grouping together the instancing of the RC interface and the setting the RC mode to localhost:3000
This is the new line to spawn a process
var player = spawn("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe", ['-I rc --rc-host=\"localhost:3000\"','--fullscreen', file_dir + files[0]]);
This works, only downfall is it also creates a RC console window, but I can live with it.
Thanks to #jfriend00 for helping solve the strange mystery.

suggestion on workflow and tools to work with node.js

I'm developing some really simple node.js libraries for learning purposes.
It's about functions like HexToBase64 and things like that.
Ideally, I'd like to program in a text editor, and play with it on the node repl, having the code automatically reloaded on the repl on every save.
Any module or tool to interactively play with node?
There are modules such as supervisor, nodemon and forever that can reload your application on a code change. Otherwise, you can create your own implementation like this:
var fs = require('fs');
var cluster = require('cluster');
if (cluster.isMaster) {
var worker = cluster.fork();
fs.watch(process.argv[1], function(event, filename) {
worker.kill();
worker = cluster.fork();
});
}
if (cluster.isWorker) {
// put your application logic here that will
// run when this file changes
}
As for using Node interactively, you can just run node in a terminal and you have an interactive console. If you needed to load a script and use it interactively, then you would use .load script.js.

Resources