No Response when calling FIDE-Ratings-Scraper NPM Package in JavaScript - node.js

I have used npm to install this package: https://github.com/xRuiAlves/fide-ratings-scraper using npm i fide-ratings-scraper. I use node to run a script with the following code in it:
var fideRatingsScraper = require("fide-ratings-scraper")
//Danii Dubov's FIDE player id: 24126055
console.log(fideRatingsScraper)
var fullInfo = fideRatingsScraper.getPlayerFullInfo('24126055');
console.log(fullInfo)
I get a response that shows that some code is executing. For the line where I print the variable fideRatingsScraper I get:
{
getPlayerFullInfo: [AsyncFunction: getPlayerFullInfo],
getPlayerElo: [AsyncFunction: getPlayerElo],
getPlayerHistory: [AsyncFunction: getPlayerHistory],
getPlayerRank: [AsyncFunction: getPlayerRank],
getPlayerPersonalData: [AsyncFunction: getPlayerPersonalData]
}
When I try to print out the variable fullInfo terminal prints out:
Promise { <pending> }
What am I missing here? I want to get the fullInfo for any player ID I list; I seem to be missing something simple.

Related

npm bin param throwing an unexpected error

I'm trying to create an npm package called loca-mapper and for some reason it doesn't run when I include in my scripts: map: loca-mapper and launch npm run map, but it behaves correctly if you:
cd node_modules/loca-mapper
npm i (after all some dependencies won't be there due to the auto-resolve)
node ./index.js
What i get is an error message as follows:
/Users/{USER}/{PATH_TO_PROJECT}/node_modules/.bin/loca-mapper: line 1: syntax error near unexpected token `('
/Users/{USER}/{PATH_TO_PROJECT}/node_modules/.bin/loca-mapper: line 1: `const { getData } = require("./getLanguages/getLanguages.js");'
The content of my index.js is structured like this:
const { getData } = require("./getLanguages/getLanguages.js");
const { getConfig } = require("./utils/getConfig");
getData(getConfig());
Adding #!/usr/bin/env node at the beginning of the file would solve the issue.
#!/usr/bin/env node
const { getData } = require("./getLanguages/getLanguages.js");
const { getConfig } = require("./utils/getConfig");
getData(getConfig());

How can I assing current url to a variable? NodeJS Selenium-webdriver

How can I assing current url or current title to a variable
let isRedirected = driver.getCurrentUrl()
console.log(isRedirected)
if (isRedirected = 'https://www.xxxx.com//redirect_main.html') {
await spinner.succeed(chalk.green(`Success: ${chalk.blue(line[0])}`));
} else {console.log('err')}
when i tried with this code i got
output:
Promise { <pending> }
and i tried it for currentTitle and i got same output, where is my fault

Electron app in production mode fails to run external script via child_process.spawnSync API in Mac but works perfectly in Linux

The app computes the sum of the exponentials of the two entered integers using an R code. The inputs are passed in the form of a JSON object to the R code via child_process.spawnSync API of node.js.
The app was packaged using electron-packager(v15.2.0) and its structure is as shown in the screenshot below. Source code to reproduce this issue can be obtained from this GitHub folder: https://github.com/wasimaftab/Utils/tree/master/test_js_r_interaction
index.js file contains the code to interact with R. Important note, you need to install rjson R package before attempting to run the electron app as it is used in R to extract the arguments from json object.
In Ubuntu (18.04) the output as expected, see the screenshot below,
The same code fails in Mac (Catalina 10.15.7) after packaging but, works perfectly in development mode, see the screenshot below.
The actual error is as follows:
Error: spawnSync Rscript ENOENT
at Object.spawnSync (internal/child_process.js:1041:20)
at Object.spawnSync (child_process.js:625:24)
at callSync (file:///Users/admin/Desktop/test_js_r_interaction/release-builds-mac/test_js_r_interaction-darwin-x64/test_js_r_interaction.app/Contents/Resources/app.asar/src/index.js:25:23)
at HTMLButtonElement.<anonymous> (file:///Users/admin/Desktop/test_js_r_interaction/release-builds-mac/test_js_r_interaction-darwin-x64/test_js_r_interaction.app/Contents/Resources/app.asar/src/index.js:84:20)
and the js code to interact with R is as follows:
const path = require("path");
const child_process = require('child_process');
const RSCRIPT = 'Rscript';
const defaultOptions = {
verboseResult: false
}
function parseStdout(output) {
try {
output = output.substr(output.indexOf('"{'), output.lastIndexOf('}"'));
return JSON.parse(JSON.parse(output));
} catch (err) {
return err;
}
}
function callSync(script, args, options) {
options = options || defaultOptions;
const result = args ?
child_process.spawnSync(RSCRIPT, [script, JSON.stringify(args)]) :
child_process.spawnSync(RSCRIPT, [script]);
if (result.status == 0) {
const ret = parseStdout(result.stdout.toString());
if (!(ret instanceof Error)) {
if (options.verboseResult) {
return {
pid: result.pid,
result: ret
};
} else {
return ret;
};
} else {
return {
pid: result.pid,
error: ret.message
};
}
} else if (result.status == 1) {
return {
pid: result.pid,
error: result.stderr.toString()
};
} else {
return {
pid: result.pid,
error: result.stderr.toString()
//error: result.stdout.toString()
};
}
}
I will appreciate any suggestion to fix this issue, thanks in advance
The error you're getting, ENOENT, suggests that your OS cannot find Rscript. This can result from the following scenarios:
Rscript is not even installed.
Rscript is installed, but not executable without a "shell". To check whether it is installed, open a Terminal and execute the command as your Electron application would.
If Rscript can be executed from within a Terminal, there could be something wrong with how the installation has set your paths up. There shouldn't be, really, but it might be necessary to execute spawnSync with additional options, such as { shell: true } to get the correct value of PATH.
If Rscript cannot be executed from within a Terminal, forcing Electron to spawn a shell via the above options (which really is what a Terminal does) will not solve this problem. In this case, try to use the complete path to Rscript as the command instead, if you happen to know where it should be installed.
If neither of those solutions help, try reinstalling Rscript altogether and try again. As I can see nothing which would be wrong with your code, I believe it is a problem of installation.
For more information on child_process.spawnSync (command, args, options), see its documentation.

Commander throws error for command with description

Here is a simple example of adding command in nodejs using commander:
'use strict';
const {Command} = require('commander');
const run = () => {
const program = new Command();
console.log('CMD');
program.command('cmd [opts...]')
.action((opts) => {
console.log('OPTS');
});
program.parse(process.argv);
};
run();
In this case everything works fine, but when I'm adding description and options, commander throws an error:
program.command('cmd [opts...]', 'DESCRIPTION', {isDefault: true})
node test-commander.js cmd opts
test-commander-cmd(1) does not exist, try --help
My env:
node v8.9.3
npm 5.3.0
commander 2.12.2
That is the declared behavior of commander. From the npm page under Git-style sub-commands...
When .command() is invoked with a description argument, no .action(callback) should be called to handle sub-commands, otherwise there will be an error. This tells commander that you're going to use separate executables for sub-commands, much like git(1) and other popular tools.
The commander will try to search the executables in the directory of the entry script (like ./examples/pm) with the name program-command, like pm-install, pm-search.
So, when you add a description like you have, it'll assume you have another executable file called test-commander-cmd for the sub command.
If commander's behavior is not what you were expecting, I might recommend looking into a package I published, called wily-cli... only if you're not committed to commander, of course ;)
Assuming your code rests in file.js, your example with wily-cli would look like this...
const cli = require('wily-cli');
const run = () => {
cli
.command('cmd [opts...]', 'DESCRIPTION', (options, parameters) => { console.log(parameters.opts); })
.defaultCommand('cmd');
};
run();
// "node file.js option1 option2" will output "[ 'option1', 'option2' ]"

Prevent NodeJS npm from printing details when retrieving package

Using the node npm module (https://www.npmjs.org/api/npm.html), I want to retrieve a package but NOT have it print the results to console. Setting loglevel silent does not seem to help.
var npm = require('npm');
npm.load(function (er, npm) {
// use the npm object, now that it's loaded.
npm.config.set('loglevel', 'silent');
npm.commands.view(["<package>#<version>"],function(err, npmBody) {
//at this point it is always logging npmBody contents to console!
});
});
This can't be done without hijacking stdout. See: https://github.com/npm/npm/blob/master/lib/install.js#L100
This was fixed once, but then reverted. It appears that npm is not designed to be use programmatically.
By "hijacking stdout" I mean something like this:
console.log('Begin');
console._stdout = { write : function () {} };
externalFn();
console._stdout = process.stdout;
console.log('End');
function externalFn () {
console.log('Annoying stuff');
}

Resources