├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── fixtures └── .gitkeep ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [test/{actual/fixtures}/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | "globals": { 13 | "document": false, 14 | "navigator": false, 15 | "window": false 16 | }, 17 | "rules": { 18 | "accessor-pairs": 2, 19 | "arrow-spacing": [ 20 | 2, 21 | { 22 | "before": true, 23 | "after": true 24 | } 25 | ], 26 | "block-spacing": [ 27 | 2, 28 | "always" 29 | ], 30 | "brace-style": [ 31 | 2, 32 | "1tbs", 33 | { 34 | "allowSingleLine": true 35 | } 36 | ], 37 | "comma-dangle": [ 38 | 2, 39 | "never" 40 | ], 41 | "comma-spacing": [ 42 | 2, 43 | { 44 | "before": false, 45 | "after": true 46 | } 47 | ], 48 | "comma-style": [ 49 | 2, 50 | "last" 51 | ], 52 | "constructor-super": 2, 53 | "curly": [ 54 | 2, 55 | "multi-line" 56 | ], 57 | "dot-location": [ 58 | 2, 59 | "property" 60 | ], 61 | "eol-last": 2, 62 | "eqeqeq": [ 63 | 2, 64 | "allow-null" 65 | ], 66 | "generator-star-spacing": [ 67 | 2, 68 | { 69 | "before": true, 70 | "after": true 71 | } 72 | ], 73 | "handle-callback-err": [ 74 | 2, 75 | "^(err|error)$" 76 | ], 77 | "indent": [ 78 | 2, 79 | 2, 80 | { 81 | "SwitchCase": 1 82 | } 83 | ], 84 | "key-spacing": [ 85 | 2, 86 | { 87 | "beforeColon": false, 88 | "afterColon": true 89 | } 90 | ], 91 | "keyword-spacing": [ 92 | 2, 93 | { 94 | "before": true, 95 | "after": true 96 | } 97 | ], 98 | "new-cap": [ 99 | 2, 100 | { 101 | "newIsCap": true, 102 | "capIsNew": false 103 | } 104 | ], 105 | "new-parens": 2, 106 | "no-array-constructor": 2, 107 | "no-caller": 2, 108 | "no-class-assign": 2, 109 | "no-cond-assign": 2, 110 | "no-const-assign": 2, 111 | "no-control-regex": 2, 112 | "no-debugger": 2, 113 | "no-delete-var": 2, 114 | "no-dupe-args": 2, 115 | "no-dupe-class-members": 2, 116 | "no-dupe-keys": 2, 117 | "no-duplicate-case": 2, 118 | "no-empty-character-class": 2, 119 | "no-eval": 2, 120 | "no-ex-assign": 2, 121 | "no-extend-native": 2, 122 | "no-extra-bind": 2, 123 | "no-extra-boolean-cast": 2, 124 | "no-extra-parens": [ 125 | 2, 126 | "functions" 127 | ], 128 | "no-fallthrough": 2, 129 | "no-floating-decimal": 2, 130 | "no-func-assign": 2, 131 | "no-implied-eval": 2, 132 | "no-inner-declarations": [ 133 | 2, 134 | "functions" 135 | ], 136 | "no-invalid-regexp": 2, 137 | "no-irregular-whitespace": 2, 138 | "no-iterator": 2, 139 | "no-label-var": 2, 140 | "no-labels": 2, 141 | "no-lone-blocks": 2, 142 | "no-mixed-spaces-and-tabs": 2, 143 | "no-multi-spaces": 2, 144 | "no-multi-str": 2, 145 | "no-multiple-empty-lines": [ 146 | 2, 147 | { 148 | "max": 1 149 | } 150 | ], 151 | "no-native-reassign": 0, 152 | "no-negated-in-lhs": 2, 153 | "no-new": 2, 154 | "no-new-func": 2, 155 | "no-new-object": 2, 156 | "no-new-require": 2, 157 | "no-new-wrappers": 2, 158 | "no-obj-calls": 2, 159 | "no-octal": 2, 160 | "no-octal-escape": 2, 161 | "no-proto": 0, 162 | "no-redeclare": 2, 163 | "no-regex-spaces": 2, 164 | "no-return-assign": 2, 165 | "no-self-compare": 2, 166 | "no-sequences": 2, 167 | "no-shadow-restricted-names": 2, 168 | "no-spaced-func": 2, 169 | "no-sparse-arrays": 2, 170 | "no-this-before-super": 2, 171 | "no-throw-literal": 2, 172 | "no-trailing-spaces": 0, 173 | "no-undef": 2, 174 | "no-undef-init": 2, 175 | "no-unexpected-multiline": 2, 176 | "no-unneeded-ternary": [ 177 | 2, 178 | { 179 | "defaultAssignment": false 180 | } 181 | ], 182 | "no-unreachable": 2, 183 | "no-unused-vars": [ 184 | 2, 185 | { 186 | "vars": "all", 187 | "args": "none" 188 | } 189 | ], 190 | "no-useless-call": 0, 191 | "no-with": 2, 192 | "one-var": [ 193 | 0, 194 | { 195 | "initialized": "never" 196 | } 197 | ], 198 | "operator-linebreak": [ 199 | 0, 200 | "after", 201 | { 202 | "overrides": { 203 | "?": "before", 204 | ":": "before" 205 | } 206 | } 207 | ], 208 | "padded-blocks": [ 209 | 0, 210 | "never" 211 | ], 212 | "quotes": [ 213 | 2, 214 | "single", 215 | "avoid-escape" 216 | ], 217 | "radix": 2, 218 | "semi": [ 219 | 2, 220 | "always" 221 | ], 222 | "semi-spacing": [ 223 | 2, 224 | { 225 | "before": false, 226 | "after": true 227 | } 228 | ], 229 | "space-before-blocks": [ 230 | 2, 231 | "always" 232 | ], 233 | "space-before-function-paren": [ 234 | 2, 235 | "never" 236 | ], 237 | "space-in-parens": [ 238 | 2, 239 | "never" 240 | ], 241 | "space-infix-ops": 2, 242 | "space-unary-ops": [ 243 | 2, 244 | { 245 | "words": true, 246 | "nonwords": false 247 | } 248 | ], 249 | "spaced-comment": [ 250 | 0, 251 | "always", 252 | { 253 | "markers": [ 254 | "global", 255 | "globals", 256 | "eslint", 257 | "eslint-disable", 258 | "*package", 259 | "!", 260 | "," 261 | ] 262 | } 263 | ], 264 | "use-isnan": 2, 265 | "valid-typeof": 2, 266 | "wrap-iife": [ 267 | 2, 268 | "any" 269 | ], 270 | "yoda": [ 271 | 2, 272 | "never" 273 | ] 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | 10 | # npm 11 | node_modules 12 | npm-debug.log 13 | 14 | # misc 15 | _gh_pages 16 | benchmark 17 | bower_components 18 | vendor 19 | temp 20 | tmp 21 | TODO.md 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | os: 4 | - linux 5 | - osx 6 | node_js: 7 | - "6" 8 | - "5" 9 | - "4" 10 | - "0.12" 11 | - "0.10" 12 | matrix: 13 | fast_finish: true 14 | allow_failures: 15 | - node_js: "0.10" 16 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var exists = require('{%= name %}'); 5 | 6 | console.log(exists('.')); 7 | //=> true 8 | console.log(exists(process.cwd())); 9 | //=> true 10 | console.log(exists('README.md')); 11 | //=> true 12 | console.log(exists('foo.txt')); 13 | //=> false 14 | console.log(exists('')); 15 | //=> false 16 | console.log(exists()); 17 | //=> false 18 | ``` 19 | 20 | ## Why another "exists" lib? 21 | 22 | I just want a _simple replacement for `fs.existsSync`_. Here is what I found: 23 | 24 | - [path-exists][]: Has dependencies to support a promises API 25 | - [fs-exists][]: async only 26 | - [file-exists][]: returns `false` if the path exists but is a directory. 27 | - [exists][]: nothing to do with `fs`. 28 | 29 | ## API 30 | {%= apidocs("index.js") %} 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fs-exists-sync [![NPM version](https://img.shields.io/npm/v/fs-exists-sync.svg?style=flat)](https://www.npmjs.com/package/fs-exists-sync) [![NPM downloads](https://img.shields.io/npm/dm/fs-exists-sync.svg?style=flat)](https://npmjs.org/package/fs-exists-sync) [![Build Status](https://img.shields.io/travis/jonschlinkert/fs-exists-sync.svg?style=flat)](https://travis-ci.org/jonschlinkert/fs-exists-sync) 2 | 3 | > Drop-in replacement for `fs.existsSync` with zero dependencies. Other libs I found either have crucial differences from fs.existsSync, or unnecessary dependencies. See README.md for more info. 4 | 5 | ## Install 6 | Install with [npm](https://www.npmjs.com/): 7 | 8 | ```sh 9 | $ npm install fs-exists-sync --save 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | var exists = require('fs-exists-sync'); 16 | 17 | console.log(exists('.')); 18 | //=> true 19 | console.log(exists(process.cwd())); 20 | //=> true 21 | console.log(exists('README.md')); 22 | //=> true 23 | console.log(exists('foo.txt')); 24 | //=> false 25 | console.log(exists('')); 26 | //=> false 27 | console.log(exists()); 28 | //=> false 29 | ``` 30 | 31 | ## Why another "exists" lib? 32 | 33 | I just want a _simple replacement for `fs.existsSync`_. Here is what I found: 34 | 35 | - [path-exists][]: Has dependencies to support a promises API 36 | - [fs-exists][]: async only 37 | - [file-exists][]: returns `false` if the path exists but is a directory. 38 | - [exists][]: nothing to do with `fs`. 39 | 40 | ## API 41 | 42 | ## Related projects 43 | 44 | You might also be interested in these projects: 45 | 46 | * [is-absolute](https://www.npmjs.com/package/is-absolute): Polyfill for node.js `path.isAbolute`. Returns true if a file path is absolute. | [homepage](https://github.com/jonschlinkert/is-absolute) 47 | * [parse-filepath](https://www.npmjs.com/package/parse-filepath): Pollyfill for node.js `path.parse`, parses a filepath into an object. | [homepage](https://github.com/jonschlinkert/parse-filepath) 48 | * [try-open](https://www.npmjs.com/package/try-open): Tries to open a file using fs.openSync (only necessary with sync), fails gracefully if the… [more](https://www.npmjs.com/package/try-open) | [homepage](https://github.com/jonschlinkert/try-open) 49 | 50 | ## Contributing 51 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/fs-exists-sync/issues/new). 52 | 53 | ## Building docs 54 | Generate readme and API documentation with [verb][]: 55 | 56 | ```sh 57 | $ npm install verb && npm run docs 58 | ``` 59 | 60 | Or, if [verb][] is installed globally: 61 | 62 | ```sh 63 | $ verb 64 | ``` 65 | 66 | ## Running tests 67 | Install dev dependencies: 68 | 69 | ```sh 70 | $ npm install -d && npm test 71 | ``` 72 | 73 | ## Author 74 | **Jon Schlinkert** 75 | 76 | + [github/jonschlinkert](https://github.com/jonschlinkert) 77 | + [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 78 | 79 | ## License 80 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). 81 | Released under the [MIT license](https://github.com/jonschlinkert/fs-exists-sync/blob/master/LICENSE). 82 | 83 | *** 84 | 85 | _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 09, 2016._ 86 | 87 | [verb]: https://github.com/verbose/verb 88 | [path-exists]: https://github.com/sindresorhus/path-exists 89 | [fs-exists]: https://github.com/meryn/fs-exists 90 | [file-exists]: https://github.com/scottcorgan/file-exists 91 | [exists]: https://github.com/tjmehta/exists 92 | 93 | -------------------------------------------------------------------------------- /fixtures/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonschlinkert/fs-exists-sync/2270e575660896c92d8f4a0cbbdbcf6b3b8a9f20/fixtures/.gitkeep -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * fs-exists-sync (https://github.com/jonschlinkert/fs-exists-sync) 3 | * 4 | * Copyright (c) 2016, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var fs = require('fs'); 11 | var path = require('path'); 12 | 13 | /** 14 | * Check if the given `filepath` exists. 15 | * 16 | * ```js 17 | * var res = exists('package.json'); 18 | * console.log(res); 19 | * //=> "package.json" 20 | * 21 | * var res = exists('fake-file.json'); 22 | * console.log(res) 23 | * //=> false 24 | * ``` 25 | * 26 | * @name exists 27 | * @param {String} `filepath` filepath to check for. 28 | * @return {String|Boolean} Returns the found filepath if it exists, otherwise returns `false`. 29 | * @api public 30 | */ 31 | 32 | module.exports = function(filepath) { 33 | if (!filepath || (typeof filepath !== 'string')) { 34 | return false; 35 | } 36 | 37 | try { 38 | (fs.accessSync || fs.statSync)(filepath); 39 | return filepath; 40 | } catch (err) {} 41 | 42 | if (process.platform === 'linux') { 43 | return exists(filepath); 44 | } 45 | return false; 46 | }; 47 | 48 | /** 49 | * Check if the filepath exists by falling back to reading in the entire directory. 50 | * Returns the real filepath (for case sensitive file systems) if found. 51 | * 52 | * @param {String} `filepath` filepath to check. 53 | * @return {String|Boolean} Returns found filepath if exists, otherwise false. 54 | */ 55 | 56 | function exists(filepath) { 57 | filepath = path.resolve(filepath); 58 | var res = tryReaddir(filepath); 59 | if (res === null) { 60 | return false; 61 | } 62 | 63 | // "filepath" is a directory, an error would be 64 | // thrown if it doesn't exist. if we're here, it exists 65 | if (res.path === filepath) { 66 | return res.path; 67 | } 68 | 69 | // "fp" is not a directory 70 | var lower = filepath.toLowerCase(); 71 | var len = res.files.length; 72 | var idx = -1; 73 | 74 | while (++idx < len) { 75 | var fp = path.resolve(res.path, res.files[idx]); 76 | if (filepath === fp || lower === fp) { 77 | return fp; 78 | } 79 | var fpLower = fp.toLowerCase(); 80 | if (filepath === fpLower || lower === fpLower) { 81 | return fp; 82 | } 83 | } 84 | 85 | return false; 86 | } 87 | 88 | /** 89 | * Try to read the filepath as a directory first, then fallback to the filepath's dirname. 90 | * 91 | * @param {String} `filepath` path of the directory to read. 92 | * @return {Object} Object containing `path` and `files` if successful. Otherwise, null. 93 | */ 94 | 95 | function tryReaddir(filepath) { 96 | var ctx = { path: filepath, files: [] }; 97 | try { 98 | ctx.files = fs.readdirSync(filepath); 99 | return ctx; 100 | } catch (err) {} 101 | try { 102 | ctx.path = path.dirname(filepath); 103 | ctx.files = fs.readdirSync(ctx.path); 104 | return ctx; 105 | } catch (err) {} 106 | return null; 107 | } 108 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fs-exists-sync", 3 | "description": "Drop-in replacement for `fs.existsSync` with zero dependencies. Other libs I found either have crucial differences from fs.existsSync, or unnecessary dependencies. See README.md for more info.", 4 | "version": "0.1.0", 5 | "homepage": "https://github.com/jonschlinkert/fs-exists-sync", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/fs-exists-sync", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/fs-exists-sync/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "devDependencies": { 23 | "gulp-format-md": "^0.1.7", 24 | "mocha": "^2.4.5" 25 | }, 26 | "keywords": [ 27 | "access", 28 | "check", 29 | "exist", 30 | "exists", 31 | "file", 32 | "file-system", 33 | "filepath", 34 | "filesystem", 35 | "fs", 36 | "is-file", 37 | "isfile", 38 | "path", 39 | "stat", 40 | "sync" 41 | ], 42 | "verb": { 43 | "layout": "default", 44 | "plugins": [ 45 | "gulp-format-md" 46 | ], 47 | "reflinks": [ 48 | "verb", 49 | "path-exists", 50 | "fs-exists", 51 | "file-exists", 52 | "exists" 53 | ], 54 | "related": { 55 | "list": [ 56 | "try-open", 57 | "parse-filepath", 58 | "is-absolute" 59 | ] 60 | }, 61 | "run": true, 62 | "toc": false, 63 | "tasks": [ 64 | "readme" 65 | ], 66 | "lint": { 67 | "reflinks": true 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var path = require('path'); 5 | var assert = require('assert'); 6 | var exists = require('./'); 7 | var isLinux = process.platform === 'linux'; 8 | 9 | describe('fs-exists-sync', function() { 10 | it('should export a function', function() { 11 | assert.equal(typeof exists, 'function'); 12 | }); 13 | 14 | it('should be truthy a file exists', function() { 15 | assert(exists('README.md')); 16 | assert(exists('LICENSE')); 17 | }); 18 | 19 | it('should not be case sensitive', function() { 20 | assert(exists('readme.md')); 21 | assert(exists('license')); 22 | }); 23 | 24 | it('should return filepath when a file exists', function() { 25 | assert.equal(exists('README.md'), 'README.md'); 26 | assert.equal(exists('LICENSE'), 'LICENSE'); 27 | }); 28 | 29 | it('should handle case sensitive names on linux', function() { 30 | assert.equal(exists('readme.md'), (isLinux ? path.resolve('README.md') : 'readme.md')); 31 | assert.equal(exists('license'), (isLinux ? path.resolve('LICENSE') : 'license')); 32 | }); 33 | 34 | it('should return true when a file exists', function() { 35 | assert(exists('.')); 36 | assert(exists('fixtures')); 37 | assert(exists(process.cwd())); 38 | }); 39 | 40 | it('should return false when a file does not exist', function() { 41 | assert(!exists()); 42 | assert(!exists('')); 43 | assert(!exists('foofofo')); 44 | assert(!exists('foofofo.txt')); 45 | }); 46 | 47 | it('should return false when a directory does not exist', function() { 48 | assert(!exists('lib/')); 49 | assert(!exists('whatever/')); 50 | }); 51 | }); 52 | --------------------------------------------------------------------------------