/**
 * @author Toru Nagashima
 * @copyright 2015 Toru Nagashima. All rights reserved.
 * See LICENSE file in root directory for full license.
 */
"use strict"

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const fs = require("fs")
const path = require("path")

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const ROOT = /^(?:[/.]|\.\.|[A-Z]:\\|\\\\)(?:[/\\]\.\.)*$/

/**
 * Check whether the file exists or not.
 * @param {string} filePath The file path to check.
 * @returns {boolean} `true` if the file exists.
 */
function existsCaseSensitive(filePath) {
    let dirPath = filePath

    while (dirPath !== "" && !ROOT.test(dirPath)) {
        const fileName = path.basename(dirPath)
        dirPath = path.dirname(dirPath)

        if (fs.readdirSync(dirPath).indexOf(fileName) === -1) {
            return false
        }
    }

    return true
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

/**
 * Checks whether or not the file of a given path exists.
 *
 * @param {string} filePath - A file path to check.
 * @returns {boolean} `true` if the file of a given path exists.
 */
module.exports = function exists(filePath) {
    try {
        const relativePath = path.relative(process.cwd(), filePath)
        return (
            fs.statSync(relativePath).isFile() &&
            existsCaseSensitive(relativePath)
        )
    }
    catch (error) {
        if (error.code === "ENOENT") {
            return false
        }
        throw error
    }
}

@@ -385,16 +385,49 @@
 (%22path%22)
+%0Aconst Cache = require(%22./cache%22)
 %0A%0A//----
@@ -652,16 +652,42 @@
 %5C.%5C.)*$/
+%0Aconst cache = new Cache()
 %0A%0A/**%0A *
@@ -1575,24 +1575,91 @@
 filePath) %7B%0A
+    let result = cache.get(filePath)%0A    if (result == null) %7B%0A    
     try %7B%0A  
@@ -1656,16 +1656,20 @@
   try %7B%0A
+    
         
@@ -1740,17 +1740,27 @@
     
-return (%0A
+    result = (%0A    
     
@@ -1805,16 +1805,20 @@
 le() &&%0A
+    
         
@@ -1867,16 +1867,28 @@
     
-)%0A
+    )%0A   
     
+ 
 %7D%0A
+    
     
@@ -1907,24 +1907,28 @@
 ) %7B%0A        
+ 
+   
 if (error.co
@@ -1930,19 +1930,19 @@
 or.code 
+!
 ==
-=
  %22ENOENT
@@ -1962,55 +1962,134 @@
     
+  
-return false%0A        %7D%0A        throw error%0A    %7D
+  throw error%0A            %7D%0A            result = false%0A        %7D%0A        cache.set(filePath, result)%0A    %7D%0A%0A    return result
 %0A%7D%0A

add cache to exists()