node.js - how to get the OS platforms user data folder - node.js

I am looking for a way to get the userdata folder using Node.js, that will work on both Windows and macOS.
The Node.js instance would be running on the user's machine.
I need something that returns the following:
C:\Documents and Settings\JohnD\Application Data (Windows XP)
C:\Users\JohnD\AppData\Roaming (Windows Vista and Up)
/Users/JohnD/Library/Preferences (macOS)
Is this possible?

Try the following:
process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share")
The expected result is:
OS X - '/Users/user/Library/Preferences'
Windows 8 - 'C:\Users\user\AppData\Roaming'
Windows XP - 'C:\Documents and Settings\user\Application Data'
Linux - '/home/user/.local/share'

You can check user environment which is stored in process.env
Also, take a look at process.platform
To be specific:
% node
> console.log(process.env.HOME)
/Users/miktam
> console.log(process.platform)
darwin
Having this information, you will be able to achieve what you need.

Related

Powershell operating system architecture vs NodeJs process.arch

I am using a NodeJs app which internally calls process.arch to get system architecture and then builds a path based on the result, eg. - path/to/file/x64/file (application is selenium-standalone NPM package).
Now I need to build the same path in PowerShell script so I also need to get system arcitecture. But doing that with f.ex. (Get-WmiObject Win32_Processor).AddressWidth returns result without x - just 64 so my path ends up path/to/file/64/file which is wrong. My current fix is to explicitly add x:
$arch = (Get-WmiObject Win32_Processor).AddressWidth
if ($arch -eq 32) { $arch = 'x32' }
if ($arch -eq 64) { $arch = 'x64' }
Do you consider this as as safe thing to do? Or is there a way from powerShell to get it already with x assigned?
process.arch describes the bitness (or word width, or architecture or whatever you wanna call it) of the current process - because it's entirely possible to run a 32-bit (or even 16-bit) process on a 64-bit operating system.
To figure out whether a running PowerShell process is 32- or 64-bit (regardless of OS architecture), use [Environment]::Is64BitProcess:
$arch = if([Environment]::Is64BitProcess) { 'x64' } else { 'x32' }

Selenium webdriver issue with file paths

I'm having an issue with Selenium standalone webdriver used with webdriver-manager npm module. I'm using the Firefox Gecko driver. I need to select a file from an HTML file input component. When I try this on my local machine or on BrowserStack I get the error:
"WebDriverError: File not found: /Users/christophergrigg/a.pdf"
const requestFile = By.id('requestFile');
driver.wait(until.elementLocated(requestFile));
const requestFileEl = driver.findElement(requestFile);
driver.wait(until.elementIsVisible(requestFileEl), TIMEOUT).click();
requestFileEl.sendKeys('/Users/christophergrigg/a.pdf');
requestFileEl.sendKeys(webdriver.Key.ENTER);
On Browser stack I'm using this path:
requestFileEl.sendKeys('C:\\Desktop\\documents\\pdf-sample2.pdf'); // Windows 7 / 8 / 8.1
You need to provide the full path of the file. And if the file is not present on the machine running the remote instance, you'll also have to set the file detector to automatically upload the file.
On mac OS X:
var remote = require('selenium-webdriver/remote');
driver.setFileDetector(new remote.FileDetector);
driver.sendKeys('/Users/christophergrigg/Desktop/a.pdf');
, or Windows:
var remote = require('selenium-webdriver/remote');
driver.setFileDetector(new remote.FileDetector);
driver.sendKeys('C:\\Users\\christophergrigg\\Desktop\\a.pdf');

LUAROCKS on windows installs rocks oddly

On Linux, luarocks installs rocks to
/usr/local/lib/luarocks/rock
and puts a corresponding lua file into
/usr/local/share/lua/5.3
On Windows(LUA 5.1), the rocks are in:
C:\Program Files (x86)\LuaRocks\systree\lib\luarocks
and the lua files are in:
C:\Program Files (x86)\LuaRocks\systree\share\lua\5.1
but lua cannot find them on the windows install.
I must have a PATH problem
This is some of my PATH:
Path=C:\Program Files (x86)\Lua\5.1\lua\;C:\Program Files (x86)\LuaRocks\2.2;C:\Program Files (x86)\LuaRocks\2.2\lua\luarocks;C:\Program Files (x86)\LuaRocks\systree\bin;C:\Perl64\site\bin;C:\UnxUpdts;C:\Perl64\bin;C:\Program Files (x86)\Lua\5.1;C:\Program Files (x86)\Lua\5.1\clibs
I am trying ZeroBraneStudio as the IDE and my system prefs specify this path
path.lua = 'C:\Program Files (x86)\Lua\5.1'
I ran
luarocks install inspect
and that generated the necessary files. Then I wrote this simple test code:
require "inspect"
assert(inspect(1) == "1")
assert(inspect("Hello") == '"Hello"')
and got this error
Program starting as '"E:\Anonamouse\ZeroBraneStudio\bin\lua.exe" -e "io.stdout:setvbuf('no')" "E:\Anonamouse\ZeroBraneStudio\myprograms\DemoInspectModule.lua"'.
Program 'lua.exe' started in 'E:\Anonamouse\ZeroBraneStudio\myprograms' (pid: 14776).
E:\Anonamouse\ZeroBraneStudio\bin\lua.exe: ...namouse\ZeroBraneStudio\myprograms\DemoInspectModule.lua:2: attempt to call global 'inspect' (a nil value)
stack traceback:
...namouse\ZeroBraneStudio\myprograms\DemoInspectModule.lua:2: in main chunk
[C]: at 0x00402a57
Program completed in 0.04 seconds (pid: 14776).
I get the same error when I execute the same simple app directly in the console.(That tells me that PATH variable for lua is working)
What am I missing?
Judging from the error message you quoted the require "inspect" worked just fine, but the module didn't set a global variable inspect. For some time now it has been policy to not set globals from within modules, but instead return something (usually the module table) from the module code, which in turn gets passed down via require. So probably something like
local inspect = require "inspect"
assert(inspect(1) == "1")
assert(inspect("Hello") == '"Hello"')
or
local inspect = require "inspect"
assert(inspect.inspect(1) == "1")
assert(inspect.inspect("Hello") == '"Hello"')
should work.

How to verify the machine configuration using PhantomJS

I have PhantomJS being used by karma-phantom-launcher plugin on my local machine. I want to get the machine configuration whether it is 32 bit or 64 bit.
Similar to what we can do with node.js i.e. after running node we can simply type
process.arch to get the machine configuration on which node is running.
The following code will meet your requirement
var system = require('system');
var os = system.os;
console.log(os.architecture); // '32bit'
console.log(os.name); // 'windows'
console.log(os.version); // '7'
You will get the informations in the console....
A little digging in the documentation would show that you can use the system module to do this:
var system = require('system');
console.log(system.os.architecture);

TiddlyWiki on node-webkit not displaying content

I followed the tutorial (In the "Getting Started" section) on how to use TiddlyWiki with node-webkit. When I then run nw.exe it doesn't display anything.
Im on windows (64bit) and have installed the 32bit version for windows. Not sure what Im doing wrong or if its just a bug.
I have also tried adding index.html and package.json to an archive (called app.nw) and run it with nw.exe, but still no luck.
I followed the instructions and couldn't get it to work either. I used TiddlyWiki 5.0.13-beta, Windows 64 bit, node-webkit 0.9.2. It throws an exception that it can't find sjcl.js. sjcl.js is packaged into TiddlyWiki.
I suggest to use TiddlyDesktop instead. It's node-webkit ready-made for TiddlyWiki. It works like a charm for me under Windows. You can get it here:
https://github.com/Jermolene/TiddlyDesktop/releases
I suspect the plain node-webkit solution has lost attention, now that there is TiddlyDesktop.
The offending code is in bootprefix.js. When bootprefix runs it checks if it is using node and then assumes it is a Node JS file based system. One solution, on a per-TiddlyWiki basis is to modify the following code near the top of bootprefix.js, which is in a script tag in single file TiddlyWiki.
// Detect platforms
$tw.browser = typeof(window) !== "undefined" ? {} : null;
$tw.node = typeof(process) === "object" ? {} : null;
$tw.nodeWebKit = $tw.node && global.window && global.window.nwDispatcher ? {} : null;
if($tw.nodeWebKit) $tw.node = null; //this is the line to add.

Resources