Sha256: 205434eb52938a4a381f6f7d0e25f061970537f7121cc012732cf2b3fd7a3d22

Contents?: true

Size: 1.44 KB

Versions: 43

Compression:

Stored size: 1.44 KB

Contents

/**
 * @author Toru Nagashima
 * See LICENSE file in root directory for full license.
 */
"use strict"

const fs = require("fs")
const path = require("path")
const Cache = require("./cache")

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

/**
 * 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
}

/**
 * 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) {
    let result = cache.get(filePath)
    if (result == null) {
        try {
            const relativePath = path.relative(process.cwd(), filePath)
            result =
                fs.statSync(relativePath).isFile() &&
                existsCaseSensitive(relativePath)
        } catch (error) {
            if (error.code !== "ENOENT") {
                throw error
            }
            result = false
        }
        cache.set(filePath, result)
    }

    return result
}

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
govuk_publishing_components-17.6.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.5.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.4.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.3.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.2.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.1.1 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.1.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-17.0.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.29.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.28.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.27.1 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.27.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.26.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.25.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.24.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.23.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.22.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.21.0 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.20.1 node_modules/eslint-plugin-node/lib/util/exists.js
govuk_publishing_components-16.20.0 node_modules/eslint-plugin-node/lib/util/exists.js