How to do it…

An example, to get a directory listing of all JS files at a given path, could be as follows. (Yes, of course you could and should do this using fs.readDir(), but we want to show how to do it with a child process.)

As shown in the Using Promises instead of error first callbacks section earlier in this chapter, we will promisify() the call, to simplify coding:

// Source file: src/process_exec.js

const child_process = require("child_process");
const { promisify } = require("util");
child_process.exec = promisify(child_process.exec);

async function getDirectoryJs(path: ?string) {
try {
const cmd = "ls -ld -1 *.js";
const stdout = await child_process.exec(cmd, { cwd: path });
console.log("OUT", path || "");
console.log(stdout);
} catch (e) {
console.log("ERR", e.stderr);
}
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.100