List all the git branches I have written a toy script to list all the git branches in cwd. I am interested in knowing how can I make this code more modular and flexible.
For example I would like to have features like:


List only local branches.
List only remote branches.
Etc.




const os = require('os');
const cp = require('child_process');

const REGEX_STAR = /^\*\s*/g;

const promiseExec = (cmd) =&gt;
  new Promise((resolve, reject) =&gt; {
    cp.exec(cmd, (err, stdout, stderr) =&gt; {
      if (err) {
        return reject(err);
      }
      resolve(lines(`${stdout}`));
    });
  });

const lines = (str) =&gt;
  str.trim().split(os.EOL).map((line) =&gt;
    line.trim().replace(REGEX_STAR, '')
  );

const branches = () =&gt;
  promiseExec('git branch -a')
    .then((res) =&gt; {
      console.log(res);
    });

branches();
Just return the data and leave it to the consumer what to do with it. That way, branches becomes a generic data grabber. If you want to have printer functions, keep them separate from the raw command.

Your Promise wrapper appears to expect the output of the branches command. Move the use of lines to the branches function instead.

Also, arrow functions with single arguments can omit the parens.

const promiseExec = (cmd) =&gt; new Promise((resolve, reject) =&gt; {
  cp.exec(cmd, (err, stdout, stderr) =&gt; {
    if (err) reject(err);
    else resolve(stdout);
  });
});

const branchLines = (str) =&gt; str.trim().split(os.EOL).map(line =&gt; line.trim().replace(REGEX_STAR, ''));

const branches = () =&gt; promiseExec('git branch -a').then(res =&gt; branchLines(res));

const printBranches = () =&gt; branches().then(res =&gt; console.log(res));

// Print manually
branches().then(res =&gt; console.log(res));

// or have a printer function do it
printbranches();