nodeJS, fs, path - creating new directorties I wrote this function that takes 2 arguments - the path to an existing file - file, and the newRoot name. The function splits the new path into directories, replaces the first directory in the path to the file with newRoot, , then loops trough all the directories checking if the director exists, and if not creates one:

/**
 * Creates directories for the file in newRoot if not there
 * @param {String} file - the path to the file
 * @param {String} newRoot - the new root directory for the file
 */
functions.makeDirectory = (file, newRoot) =&gt; {
  let dirPath = path.dirname(file);
  let directories = dirPath.split('/');
  let currentPath = '';
  directories[0] = newRoot;
  directories.map(directory =&gt; {
    currentPath = path.join(currentPath, directory);
    if(!fs.existsSync(currentPath)) {
      fs.mkdirSync(currentPath);
    };
  });
};


The string operations here seem a bit redundat, but I can't figure out how to get rid of them. Is there a more optimal solution for this or am I on the right track?

Would it increase performance to rewrite the fs function as a promise?
There are util libraries for this:fs-extra. 

From its doc:


  mkdirs(dir, callback) Creates a directory. If the parent hierarchy
  doesn't exist, it's created. Like mkdir -p.
  
  Alias: mkdirp()
  
  Sync: mkdirsSync() / mkdirpSync()
  
  Examples:

var fs = require('fs-extra')

fs.mkdirs('/tmp/some/long/path/that/prob/doesnt/exist', function (err) {
  if (err) return console.error(err)
  console.log("success!")
})

fs.mkdirsSync('/tmp/another/path')



If you write function as a promise it would enable other code to run between each step of your function (eg. between each subdir being created) so it will not block other code like sending some request after calling this function.