open Module Doesn't Work for Full Browser Path - node.js

const open = require('open')
const main = async () => {
await open('https://google.com', {
app: {
name: 'C:\Program Files\Mozilla Firefox\firefox.exe'
}
});
}
main();
Why this code doesn't work on W10? Passing full path browser .exe as an argument works only under Linux? Is that why the creator mentions WSL specifically?
You may also pass in the app's full path. For example on WSL, this can
be /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe for
the Windows installation of Chrome.
Source: https://www.npmjs.com/package/open

Related

electron app.getpath('exe') result is not what I expected

I want to start my app automatically on system startup.
I used auto-launch.
var AutoLaunch = require('auto-launch');
var autoLauncher = new AutoLaunch({
name: "App name",
path: app.getPath('exe'),
});
console.log(`app path : ${app.getPath('exe')}`)
//result : C:\Users\USER-PC\project_folder\dist\electron.exe
autoLauncher.isEnabled().then(function(isEnabled) {
if (isEnabled) return;
autoLauncher.enable();
}).catch(function (err) {
throw err;
});
The problem is, I am using electron-builder for building an exe file. And when I build an exe file its name is like : 'app-name 1.0.0.exe'.
So auto launch is not working properly because the option, 'path' is different from the actual exe file's path.
How can I solve this?
I tried to set the app name so app.getPath('exe') can return the actual .exe path. But it did not work.
app.setName('app-name')

Configure Puppeteer executablePath chrome in your local Windows

Puppeteer version : 1.11.0
Platform / OS version: Windows 10 pro
Node.js version: 12.6.6
When I did a local development test in windows, happen was problem in executablePath.
"Failed to launch chrome! spawn /usr/bin/chromium-browser ENOENT"
I saw for windows needs to get complete path. Otherwise cannot find chrome.exe
Default in code:
const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
In windows it worked thus:
const browser = await puppeteer.launch({executablePath: 'C:\\your_workspace\\node_modules\\puppeteer\\.local-chromium\\win64-(version)\\chrome-win\\chrome.exe'});
In visual code suggest the path
Visual Code view explorer
You can also set the environment variable PUPPETEER_EXECUTABLE_PATH.
This is useful in conjunction with PUPPETEER_SKIP_CHROMIUM_DOWNLOAD set to true
Maybe this can help:
const osPlatform = os.platform(); // possible values are: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'
console.log('Scraper running on platform: ', osPlatform);
let executablePath;
if (/^win/i.test(osPlatform)) {
executablePath = '';
} else if (/^linux/i.test(osPlatform)) {
executablePath = '/usr/bin/google-chrome';
}

Permission denied when running fs-plus module

I'm writing package for Atom editor.
Can anyone help with persmission denied error I get when using fs-plus makeTree method in my package when running it on Mac? No errors on Windows.
I use fs-plus, the same module that Atom uses in its tree-view package (and tree-view works on Mac).
UPD: Adding screenshot, and some code:
New Folder option shown on the picture is implemented using the same module - fs-plus, and it works on Mac. I use just the same module, and the same method (fs-plus.makeTree) to create directory, but my implementation fails with Permission denied error.
My code:
import util from 'util';
import { sep } from 'path';
import fs from 'fs-plus';
const makeTreeAsync = util.promisify(fs.makeTree);
createTutorial(data) {
const { initialPath } = this;
const { fileName } = data;
const filePath = `${initialPath}${sep}${fileName}${sep}${fileName}.md`;
return makeTreeAsync(fileName)
.then(() => writeFileAsync(filePath, this.getContent(data)))
.then(() => filePath);
},
code from tree-view package:
path = require 'path'
fs = require 'fs-plus'
onConfirm: (newPath) ->
newPath = newPath.replace(/\s+$/, '') # Remove trailing whitespace
endsWithDirectorySeparator = newPath[newPath.length - 1] is path.sep
unless path.isAbsolute(newPath)
// some path preprocessing
return unless newPath
try
if fs.existsSync(newPath)
#showError("'#{newPath}' already exists.")
else if #isCreatingFile
// some code, we are not interested in as we 're creating directory
else
fs.makeTreeSync(newPath)
#emitter.emit('did-create-directory', newPath)
#cancel()
catch error
#showError("#{error.message}.")
Important note: my package is installed manually (copied to ~/<username>/.atom/packages) and then run npm i
After some time of debugging, I have a solution. I tried to use native fs.mkdir with no luck, but after I added mode as the second argument - it worked.
const _0777 = parseInt('0777', 8);
const mode = _0777 & (~process.umask());
fs.mkdir(<folderPath>, mode, () => {});
Hope, this will help someone.

How to package an electron app and flask server into one executable

So far (on my mac) I have managed to package my flask app into a single .app file using pyInstaller and can successfully package electron into one .app file. Now I would like to be able to package the flask executable and electron app together into one executable.
I have tried what some other stack overflow posts suggested and used the child_process module to spawn the flask .app, however that gave me the below error:
Uncaught Exception:
Error: spawn ../server/dist/server.app ENOENT
at _errnoException (util.js:1024:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Here is my electron entry point code that caused this error:
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const isDev = require('electron-is-dev');
const path = require('path');
const childSpawn = require('child_process').spawn;
let mainWindow;
const createWindow = () => {
childSpawn('../server/dist/server.app');
mainWindow = new BrowserWindow({ width: 900, height: 680 });
mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
app.setAboutPanelOptions({
applicationName: 'app_name',
applicationVersion: '0.0.1',
})
mainWindow.on('closed', () => mainWindow = null);
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
However, if that were to work, I don't see how I could bundle the flask server together with the electron app into one executable?
I'd appreciate some help from someone who has successfully done this.
The packaged flask .app is an executable, it cannot be spawned as a child process. You will have to execute the file using execFile. I referred this Following is the snippet I referred from the site.
packaging
Some people are asking for the packaging. This is easy: apply the knowledge of how to package Python applications and Electron applications.
Python part
Useing PyInstaller.
Run the following in the terminal:
pyinstaller pycalc/api.py --distpath pycalcdist
rm -rf build/
rm -rf api.spec
If everything goes well, the pycalcdist/api/ folder should show up, as well as the executable inside that folder. This is the complete independent Python executable that could be moved to somewhere else.
Attention: the independent Python executable has to be generated! Because the target machine we want to distribute to may not have correct Python shell and/or required Python libraries. It’s almost impossible to just copy the Python source codes.
Node.js / Electron part
This is tricky because of the Python executable.
In the above example code, I write
// part of main.js
let script = path.join(__dirname, 'pycalc', 'api.py')
pyProc = require('child_process').spawn('python', [script, port])
However, once we package the Python code, we should no longer spawn Python script. Instead, we should execFile the generated excutable.
Electron doesn’t provide functions to check whether the app is under distributed or not (at least I don’t find it). So I use a workaround here: check whether the Python executable has been generated or not.
In main.js, add the following functions:
// main.js
const PY_DIST_FOLDER = 'pycalcdist'
const PY_FOLDER = 'pycalc'
const PY_MODULE = 'api' // without .py suffix
const guessPackaged = () => {
const fullPath = path.join(__dirname, PY_DIST_FOLDER)
return require('fs').existsSync(fullPath)
}
const getScriptPath = () => {
if (!guessPackaged()) {
return path.join(__dirname, PY_FOLDER, PY_MODULE + '.py')
}
if (process.platform === 'win32') {
return path.join(__dirname, PY_DIST_FOLDER, PY_MODULE, PY_MODULE + '.exe')
}
return path.join(__dirname, PY_DIST_FOLDER, PY_MODULE, PY_MODULE)
}
And change the function createPyProc to this:
// main.js
// the improved version
const createPyProc = () => {
let script = getScriptPath()
let port = '' + selectPort()
if (guessPackaged()) {
pyProc = require('child_process').execFile(script, [port])
} else {
pyProc = require('child_process').spawn('python', [script, port])
}
if (pyProc != null) {
//console.log(pyProc)
console.log('child process success on port ' + port)
}
}
The key point is, check whether the *dist folder has been generated or not. If generated, it means we are in “production” mode, execFile the executable directly; otherwise, spawn the script using a Python shell.

selinium-webdriver issue with Error: The geckodriver.exe executable could not be found on the current PATH

Hey I want to get screen shot with nodejs selinium-webdriver firefox
I am getting error like that : Error: The geckodriver.exe executable could not be found on the current PATH.
I set up the enviornment variable, but no luck
You need to set the path to the geckodriver.exe prior to creating the driver instance.
In Java:
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");//"<PATH TO LOCATION>\\chromedriver.exe");
I had some successful result with this process:
1° - Check if your webdriver(geckodriver, chromedriver, etc.) is in the correct path (if you don't know how to do it, check the link, https://www.youtube.com/watch?v=fj0Ud16YJJw). I think this video is a little old, because in npm the code information is different, updated, but it also serves as instruction.
2°- I changed my type of license(in package.json) from “ISC” to “MIT”, you can do this manually. And for my surprise, this change made my code go well.
const webdriver = require("selenium-webdriver");
const firefox = require("selenium-webdriver/firefox");
const { Builder, Browser, By, Key, until } = require("selenium-webdriver");
async function example() {
let driver = await new Builder().forBrowser(Browser.FIREFOX).build();
try {
await driver.get("https://www.google.com/ncr");
await driver.findElement(By.name("q")).sendKeys("Selenium", Key.RETURN);
await driver.wait(until.titleIs("webdriver - Google Search"), 1000);
} finally {
await driver.quit();
}
}
example();
And here we have another link that goes to selenium webdriver dependency in npm website(https://www.npmjs.com/package/selenium-webdriver) for more information.

Resources