2

Here is my problem, I want to create a CLI that automatically runs a test. Without the CLI, I'm able to run everything perfectly with the node command:

node test.js

Basically, I want to do the exact same thing as the command before, so I googled for a technique that does this. I found this:

#!/usr/bin/env node
'use strict';
 const options = process.argv;
 const { execFile } = require('child_process');
 const child = execFile('node', ['../dist/test.js'], (error, stdout, stderr) => {
     if (error) {
        throw error;
     }
     console.log(stdout);
 });

This method doesn't work for me because, in the test.js file, I'm using the ora package. And because this package is making real-time animations, it doesn't come in stdout.

Is there any way of executing in real time (without subprocess) my test.js using Node? I'm open to other methods, but I want to publish the CLI on NPM, so keep in mind that it has to be in JavaScript .

You can find every file that I've talked here on GitHub. Normally, you wouldn't need this link, but I'm giving it to you if you need to have a closer look.

Arthur Guiot
  • 713
  • 10
  • 25

3 Answers3

0

You should simply call your test() function from your CLI code, after requiring the module that defines it. Have a look at mocha and jasmine: you will see that while both tools provide a CLI, they also provide instructions for invoking the test frameworks from arbitrary JS code.

Olivier Liechti
  • 3,138
  • 2
  • 19
  • 26
  • Well, no because my `test.js` isn’t a function, but a set of operations, I could pack up everything in one function, but it’s not what I’m looking. I’ll do that only if there no other way to do it – Arthur Guiot Oct 01 '17 at 04:58
  • It does not matter that you have more than one function. What I am saying is that your CLI is JS code that can require your core test framework modules and use it. You are in the same codebase, so you can simply `require` local modules. If someone else was developing the CLI, he would do a `npm install` to add a dependency on your test framework, and then would call your functions/classes from his code. – Olivier Liechti Oct 01 '17 at 05:03
  • I found a much better solution: see on top ;) – Arthur Guiot Oct 01 '17 at 08:46
0

I can't think of a way without a sub-process. but this may help. The child process exec will not work with the continuous output commands as it buffers the output the process will halt when that buffer is full.

The suitable solution is spwan :

var spwan = require('child_process').spwan
var child = spwan('node', ['../dist/test.js'])

child.stdout.on('data', function(data) {
  console.log(data)
})

child.stderr.on('data', function(data) {
  console.log(data)
})
madllamas
  • 508
  • 4
  • 10
0

Here is my solution, you can use the fs library to get the code of the file, and then, you simply use eval to execute in the same process.

const fs = require("fs");

function run(file) {
    fs.readFile(file, (err, data) => {
        eval(data.toString('utf8'))
    })
}
Arthur Guiot
  • 713
  • 10
  • 25
  • Just keep in mind that the use of `eval` is not recommended. [Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Don't_use_eval_needlessly!) Also, this [question](https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) – madllamas Oct 02 '17 at 12:47