sync.js (1291B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /* eslint-disable no-sync */ 20 21 'use strict'; 22 23 // MODULES // 24 25 var fs = require( 'fs' ); 26 27 28 // FUNCTIONS // 29 30 var fcn; 31 if ( typeof fs.accessSync === 'function' ) { 32 fcn = fs.accessSync; 33 } else { 34 fcn = fs.statSync; 35 } 36 37 38 // MAIN // 39 40 /** 41 * Synchronously tests whether a path exists on the filesystem. 42 * 43 * @param {(string|Buffer)} path - path to test 44 * @returns {boolean} boolean indicating whether the path exists 45 * 46 * @example 47 * var bool = existsSync( __dirname ); 48 * // returns <boolean> 49 */ 50 function existsSync( path ) { 51 try { 52 fcn( path ); 53 } catch ( err ) { // eslint-disable-line no-unused-vars 54 return false; 55 } 56 return true; 57 } 58 59 60 // EXPORTS // 61 62 module.exports = existsSync;