'use strict'

const fs = require('fs')
const path = require('path')

const _ = require('lodash')

// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------

/**
 * Create a directory if it doesn't exist yet.
 * @param {string} dir - The path of the directory to create.
 * @returns {boolean} - true if a directory has been created, false otherwise.
 */
function createDir (dir) {
  if (fs.existsSync(dir)) {
    return false
  }

  fs.mkdirSync(dir)
  return true
}

/**
 * Find all files in a directory structure.
 * @param {string} baseDir - The base directory to search in.
 * @param {Object} options - Options.
 * @returns {Array} The list of files.
 */
function findAllFiles (baseDir, options) {
  options = _.defaults(options || {}, {
    ignoredFolders: [],
    maxDepth: undefined
  })

  let list = []

  function search (dir, depth) {
    fs.readdirSync(dir).forEach(file => {
      file = path.join(dir, file)

      // File or directory? Ok.
      // Otherwise, discard the file.
      let stat = fs.statSync(file)
      if (stat.isFile()) {
        list.push(file)
      } else if (stat.isDirectory()) {
        // The directory should be ignored?
        if (options.ignoredFolders.includes(path.basename(file))) {
          return
        }

        // Should we stop at this depth?
        if (options.maxDepth && depth >= options.maxDepth) {
          return
        }

        search(file, depth + 1)
      }
    })
  }

  search(baseDir, 0)

  return list
}

// -------------------------------------------------------------
// Exports.
// -------------------------------------------------------------

module.exports = {
  createDir,
  findAllFiles
}

@@ -66,37 +66,8 @@
 ')%0A%0A
-const _ = require('lodash')%0A%0A
 // -
@@ -646,27 +646,8 @@
 ect%7D
- options - Options.
 %0A * 
@@ -722,61 +722,10 @@
 ir, 
-options) %7B%0A  options = _.defaults(options %7C%7C %7B%7D, %7B%0A  
+%7B%0A
   ig
@@ -740,16 +740,15 @@
 ders
-:
+ =
  %5B%5D,%0A
-  
   ma
@@ -757,9 +757,10 @@
 epth
-:
+ =
  und
@@ -766,21 +766,25 @@
 defined%0A
-  %7D)%0A
+%7D = %7B%7D) %7B
 %0A  let l
@@ -1154,24 +1154,16 @@
     if (
-options.
 ignoredF
@@ -1283,24 +1283,16 @@
     if (
-options.
 maxDepth
@@ -1295,16 +1295,17 @@
 epth && 
+(
 depth %3E=
@@ -1309,16 +1309,8 @@
  %3E= 
-options.
 maxD
@@ -1310,24 +1310,25 @@
 %3E= maxDepth)
+)
  %7B%0A         

Remove lodash from sfs