nodejs REPL output on same line - node.js

I'm building a CLI app with nodejs with native REPL. A dumb sample is:
const repl = require('repl')
const myApp = require('myApp')
repl.start({
eval: (input, context, filename, callback) => {
const result = myApp(input)
callback(null, result)
},
writer: (result) => result
? 'my result: ' + result
: '' // <--- empty result
})
Everything is fine, but a newline is created for every response of the app. I'd like to override this behavior, and decide whether to create a new line or not.
Example
Let's pretend the app to be a calculator:
$ node index.js
add 5
add 3
result
8
Because add command doesn't have any result, a new command should be inserted in the very next line. However, what I'm getting from the above code is:
$ node index.js
add 5
add 3
result
8
with an empty line added after any 0add call.
How can I prevent this?

So I found out there's a parameter I could set, ignoreUndefined. If true, when you call callback(null, undefined) (empty result case), it won't add a new line.
Thus, in the above example:
const repl = require('repl')
const myApp = require('myApp')
repl.start({
ignoreUndefined: true,
eval: (input, context, filename, callback) => {
const result = myApp(input)
callback(null, result)
},
writer: (result) => result
? 'my result: ' + result
: undefined // <--- empty result
})
docs: https://nodejs.org/api/repl.html
similar question: node.js REPL "undefined"

Related

npm firstline package do not return first line (string) but a promise object, how to get the first line string?

I am trying to extract the first line of a file using npm firstline (https://www.npmjs.com/package/firstline) package using the following line of code
const firstline = require("firstline");
console.log("First line of the file: " + firstline("home/assets/sample.txt");
however it returns First line of the file: [object Promise] as output.
content of file "home/assets/sample.txt" is as follows
abc
def
ghi
Therefore, I expect "abc" as output. what I am missing?
Promises in javascript need to be resolved to get the value out of them:
const firstline = require("firstline");
firstline("home/assets/sample.txt").then(line => {
console.log("First line of the file: " + line);
});
As shown above you can use .then or await to get the value out.
Example using await:
const firstline = require("firstline");
const main = async () => {
const line = await firstline("home/assets/sample.txt");
console.log("First line of the file: " + line);
};
main();
note that await need to be in an async function to work, otherwhise you will have a javascript syntax error
Since the firstline package returns a promise you cannot just console.log the result.
Instead you can use then or await.
Maybe this can answer your question.
How does .then(console.log()) and .then(() => console.log()) in a promise chain differ in execution

Crash when trying to load a file using Node's repl library

I am getting a crash at the indicated point below when the code attempts to load a file. The file contents are read and displayed on the console. But when the line
app.ports.receiveData.send(data.toString());
is (tried to) execute, the code crashes. I've attached the error message below the code. The JS code here is used to run some Elm code "headless". The app.ports... function call is supposed send data back to the Elm app. (The Elm Code is further down).
JS CODE:
const repl = require('repl');
const fs = require('fs')
// Link to Elm code
var Elm = require('./main').Elm;
var main = Elm.Tool.init();
// Eval function for the repl
function eval(cmd, _, _, callback) {
main.ports.put.subscribe(
function putCallback (data) {
main.ports.put.unsubscribe(putCallback)
callback(null, data)
}
)
main.ports.get.send(cmd)
}
main.ports.sendFileName.subscribe(function(data) {
var path = data
// console.log(path)
fs.readFile(path, { encoding: 'utf8' }, (err, data) => {
if (err) {
console.error(err)
return
}
console.log(data.toString())
// Crash on next line !!!!
app.ports.receiveData.send(data.toString());
})
});
function myWriter(output) {
return output
}
console.log("\nType 'h' for help\n")
repl.start({ prompt: '> ', eval: eval, writer: myWriter});
Elm CODE
Here are the parts the Elm code that are relevant.
This code is called when the user wants to load a file.
loadFileCmd : String -> Cmd msg
loadFileCmd fileName =
sendFileName (E.string <| "./source/" ++ fileName)
These are the ports used to communicate with JS
port get : (String -> msg) -> Sub msg
port put : String -> Cmd msg
port sendFileName : E.Value -> Cmd msg
port receiveData : (E.Value -> msg) -> Sub msg
The get port listens for commands the user gives to the repl and gives these commands to Elm to process. The put port sends data that Elm computes to the repl.
The sendFileName port sends a file path to the repl. The receiveData port listens for the file contents. (But we crash before this can happen).
Here are the subscriptions:
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.batch [ Command.get Input, Command.receiveData ReceiveFileContents ]
ERROR MESSAGE:
repl.js:573
const lines = errStack.split(/(?<=\n)/);
^
TypeError: errStack.split is not a function
at Domain.debugDomainError (repl.js:573:30)
at Domain.emit (events.js:321:20)
at Domain.EventEmitter.emit (domain.js:485:12)
at Domain._errorHandler (domain.js:253:23)
at Object.<anonymous> (domain.js:156:29)
at process._fatalException (internal/process/execution.js:164:29)
From the comments, the answer was to replace the line
app.ports.receiveData.send(data.toString());
with
main.ports.receiveData.send(data.toString());
as the Elm app is named main, not app.

How to access variable in another file in node js

I have 2 files: export.js and server.js
I'm trying to access a variable in export.js in server.js, but I get undefined.
Note that I'm using knexjs and I gave it the name 'db';
export.js
let count;
const livePending = (db) => {
db('cart')
.count('id').where('status','=','Pending')
.then(data => {
if (data[0]) {
count = data[0].count;
}
}).catch(err => res.status(400).send('DB Connection failed!'));
}
module.exports = {
livePending: livePending,
pCount: count
}
server.js
[...]
const anotherFile = require('./export');
anotherFile.livePending(db);
console.log(import.pCount);
When I try to console log inside the livePending function in export.js, I get the desired count which is 1.
The reason I'm doing this is to lessen the lines of code in my server.js.
If I do the exact same function in my server.js, I get the correct result.
I created a small node app to test a variation of your code. Two important findings:
1.
const import = require('./export');
import is reserved (as well as export). Node will throw a SyntaxError: Unexpected token if you attempt to use either one as a variable name.
2.
console.log(import.count);
In your code, you're attempting to log a variable that's already been returned. You'll notice the log statement will return undefined. Instead, create a function that you can call to get the value from the actual variable in the other file.
To make things clearer, here's a little demo to show these concepts in action.
export.js
let count;
const setCount = num => count = num;
const getCount = () => count;
// Shortcut: If the key: value is the same, we can just omit the value
module.exports = {
setCount,
getCount
}
server.js
const ex = require('./export');
ex.setCount(5);
console.log(ex.getCount()); // 5

Null Commond line argument

I am passing around 9 parameter via command line to Node JS script.
Here is my Command:
node awsInvokeDelete.js DELETE https://Test1234.execute-api.us-west-2.amazonaws.com us-west-2 /qa/transit-connectivity/api/v1/sites/tdcloudtsttd03 AKIAJ4Y5DGqwewqeqw CFdAgsdtqweqwe/SKqDezdqweewofWrUXXBbQoMy '{\"change_request\":\"chg0123456\"}'
I am passing query parameter as JSON in command line argument which is process.argv[9] in node JS script. It works perfectly If I pass value to all parameters but in some cases process.argv[8] will be empty. When I am passing empty value in process.argv[8], its actually takes argv[9] as argv[8].
how Can I pass empty argument value in command line for below script.
var apigClientFactory = require('aws-api-gateway-client').default;
let awsMethod = process.argv[2],
awsEndpoint = process.argv[3],
awsRegion = process.argv[4],
awsPathTemplate = process.argv[5],
awsAccessKey = process.argv[6],
awsSecreteKey = process.argv[7],
awsPathParams = process.argv[8],
awsAdditionalParams = JSON.parse(process.argv[9] || '{}');
var apigClient = apigClientFactory.newClient({
invokeUrl: awsEndpoint, // REQUIRED
accessKey: awsAccessKey, // REQUIRED
secretKey: awsSecreteKey, // REQUIRED
region: awsRegion, // REQUIRED: The region where the API is deployed.
retryCondition: (err) => { // OPTIONAL: Callback to further control if
request should be retried. Uses axon-retry plugin.
return err.response && err.response.status === 500;
}
});
var param = awsPathParams;
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = awsPathTemplate;
var method = awsMethod;
var additionalParams = { queryParams: awsAdditionalParams, };
console.log(additionalParams);
var body = {};
apigClient.invokeApi(param, pathTemplate, method, additionalParams, body)
.then(function(result) {
//console.log(result.data + ": " +result)
console.log(result.response.data)
}).catch(function(result) {
console.log(result.response.data)
});
Here is output: args[8]'s value should be displayed as args[9]
args[8]: {"change_request":"chg0123456"}
args[9]: [object Object]
Your script is not working on the input that you have provided (if you actually add the missing argument) because '{\"change_request\":\"chg0123456\"}' is not something that JS can parse as a JSON string. Furthermore, you are not passing any empty value in your input to the script (just an empty space is not considered as an actual input).
You need to change it to this '{"change_request":"chg0123456"}' and pass empty value as an empty string ''.
This input works correctly.
node index.js DELETE https://Test1234.execute-api.us-west-2.amazonaws.com us-west-2 /qa/transit-connectivity/api/v1/sites/tdcloudtsttd03 AKIAJ4Y5DGqwewqeqw CFdAgsdtqweqwe/SKqDezdqweewofWrUXXBbQoMy '' '{"change_request":"chg0123456"}'
If you really need the object in that format, then you need to remove \ characters from it before you can call JSON.parse on it.
awsAdditionalParams = JSON.parse(
process.argv[9].split('\\').join('') || '{}'
);
If you first want to check whether the last argument is not empty and only then run the code above, the you can use ternary operator like this.
awsAdditionalParams = process.argv[9]
? JSON.parse(process.argv[9].split('\\').join('') || '{}')
: '';
3 options:
change the script to switch around arguments 8 and 9. then you always have the same number even if 9 is empty.
pass the argument as "" instead of nothing.
change the script so you parse your own command line and change things around whichever way you like.

How to pass command line arguments to NodeJS launched from an executable script

How to set what would otherwise be command-line arguments to node for a NodeJS process run from a launcher script? (The sh/CMD scripts npm places into node_modules/.bin.)
Plenty of NodeJS libraries / frameworks come with their own runner script, e.g. zeit/micro or moleculer that's usually executed from a npm script. This presents a problem in development, since in my case I want to do the equivalent of:
node --inspect -r ts-node/register -r dotenv-safe/config src/index.ts
(Except, of course, that does nothing since index.ts just exports something for the runner to pick up.)
Is there some "clean", preferably generic (i.e. not specific to a given framework's runner exposing those command line params) way that I'm missing to do this, ideally one that works as a npm script? The only thing that seems like it would work would be for e.g. micro:
node-dev -r ts-node/register ./node_modules/micro-dev/bin/micro-dev.js ./src/index.ts
which is kind of a mouthful from the Redundant Department of Redundancy Department and seems to obviate the point of having those launcher scripts. (It also won't work if the runner spawns other Node processes, but that's not a problem I'm actually having.) I'd like to not have to duplicate what the launcher scripts are already doing. I'm also aware of npx having --node-arg but npx is a whole another can of worms. (On Windows it's five seconds of startup time and one spurious error message just to run a script I already have installed; it also won't find an already installed package if it can't find its .cmd launcher script, e.g. when using Docker to run the dev environment. In short I'd rather not use npx for this.)
To clear up the confusion that seems to crop up in the comments: I want to override the command line parameters that affect the behaviour of the NodeJS runtime itself executing the runner script, not pass parameters to the script itself or to my code. That is, the options listed here: https://nodejs.org/api/cli.html
One option is to write a little wrapper script that uses the current process execPath to run child_process.execFile.
So the sample here is to be able to do
node --expose-http2 --zero-fill-buffers -r ./some-module.js ./test.js
but not actually write that out, instead have wrap.js inject the args:
node ./wrap.js ./test.js
I tested running this via npm in a package.json, and it works fine. I tested that it was working by having some-module.js stick a value on the global object, and then logging it in test.js.
Files involved:
wrap.js
const child_process = require('child_process');
const nodeArgs = ['--expose-http2', '--zero-fill-buffers', '-r', './some-module.js'];
const runTarget = process.argv[2];
console.log('going to wrap', runTarget, 'with', nodeArgs);
const finalArgs = nodeArgs.concat(runTarget).concat(process.argv.slice(2));
const child = child_process.execFile(
process.execPath,
finalArgs,
{
env: process.env,
cwd: process.cwd(),
stdio: 'inherit'
}, (e, stdout, stderr) => {
console.log('process completed');
if (e) {
process.emit('uncaughtException', e);
}
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
and
some-module.js
global.testval = 2;
and
test.js
console.log('hi guys, did the wrap work?', global.testval)
EDIT: So upon further thought, this solution really only satisfies wrapping the initial runner. But most tools, such as mocha re-spawn a sub process which would then lose this effect. To really get the job done, you can proxy each of the child process calls and somewhat enforce that calls to spawn and such also include your args.
I rewrote the code to reflect this. Here's a new setup:
package.json
{
"scripts": {
"test": "node -r ./ensure-wrapped.js node_modules/mocha/$(npm view mocha bin.mocha) ./test.js"
},
"dependencies": {
"mocha": "^5.1.0"
}
}
ensure-wrapped.js
const child_process = require('child_process');
// up here we can require code or do whatever we want;
global.testvalue = 'hi there'
const customParams = ['--zero-fill-buffers'];
// the code below injects itself into any child process's spawn/fork/exec calls
// so that it propogates
const matchNodeRe = /((:?\s|^|\/)node(:?(:?\.exe)|(:?\.js)|(:?\s+)|$))/;
const ensureWrappedLocation = __filename;
const injectArgsAndAddToParamsIfPathMatchesNode = (cmd, args, params) => {
params.unshift(...customParams);
params.unshift(args);
if (!Array.isArray(args)) { // all child_proc functions do [] optionally, then other params
args = []
params.unshift(args);
}
if (!matchNodeRe.test(cmd)) {
return params;
}
args.unshift(ensureWrappedLocation);
args.unshift('-r');
return params;
}
child_process._exec = child_process.exec;
child_process.exec = (cmd, ...params) => {
// replace node.js node.exe or /path/to/node to inject -r ensure-wrapped.js ...args..
// leaves alone exec if it isn't calling node
cmd = cmd.replace(matchNodeRe, '$1 -r ' + ensureWrappedLocation + ' ');
return child_process._exec(cmd, ...params)
}
child_process._execFile = child_process.execFile;
child_process.execFile = (path, args, ...params) => {
params = injectArgsAndAddToParamsIfPathMatchesNode(path, args, params);
return child_process._execFile(path, ...params)
}
child_process._execFileSync = child_process.execFileSync;
child_process.execFileSync = (path, args, ...params) => {
params = injectArgsAndAddToParamsIfPathMatchesNode(path, args, params);
return child_process._execFileSync(path, ...params);
}
child_process._execSync = child_process.execSync;
child_process.execSync = (cmd, ...params) => {
cmd = cmd.replace(matchNodeRe, '$1 -r ' + ensureWrappedLocation + ' ');
return child_process._exec(bin, ...args)
}
child_process._fork = child_process.fork;
child_process.fork = (module, args, ...params) => {
params = injectArgsAndAddToParamsIfPathMatchesNode(process.execPath, args, params);
return child_process._fork(module, ...params);
}
child_process._spawn = child_process.spawn;
child_process.spawn = (cmd, args, ...params) => {
params = injectArgsAndAddToParamsIfPathMatchesNode(cmd, args, params);
return child_process._spawn(cmd, ...params)
}
child_process._spawnSync = child_process.spawnSync;
child_process.spawnSync = (cmd, args, ...params) => {
params = injectArgsAndAddToParamsIfPathMatchesNode(cmd, args, params);
return child_process._spawnSync(cmd, ...params);
}
test.js
describe('test', () => {
it('should have the global value pulled in by some-module.js', (done) => {
if (global.testvalue !== 'hi there') {
done(new Error('test value was not globally set'))
}
return done();
})
})
Please never put code like this into a node module that's published. modifying the global library functions is pretty bad.
Everything passed in the command line AFTER your nodejs application is parsed into an array called process.argv. So...
node myapp.js foo bar hello 5000
In your nodejs code...
const args = process.argv;
console.log(args[0]);
console.log(args[1]);
console.log(args[2]);
console.log(args[3]);
would yield...
foo
bar
hello
5000
I didnt get clear scenario of your problem,but as your question title ,we can execute the any cmd command from nodejs using npm libraries like:
import Promise from 'bluebird'
import cmd from 'node-cmd'
const getAsync = Promise.promisify(cmd.get, { multiArgs: true, context: cmd })
getAsync('node -v').then(data => {
console.log('cmd data', data)
}).catch(err => {
console.log('cmd err', err)
})

Resources