time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4569B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # Exists
     22 
     23 > Test whether a path exists on the filesystem.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var exists = require( '@stdlib/fs/exists' );
     31 ```
     32 
     33 #### exists( path, clbk )
     34 
     35 Asynchronously tests whether a path exists on the filesystem.
     36 
     37 ```javascript
     38 exists( __dirname, done );
     39 
     40 function done( bool ) {
     41     if ( bool ) {
     42         console.log( '...path exists.' );
     43     } else {
     44         console.log( '...path does not exist.' );
     45     }
     46 }
     47 ```
     48 
     49 The above callback signature matches the now **deprecated** [`fs.exists()`][node-fs-exists] API. The function also accepts the more conventional `error`-first style callback signature found in most asynchronous Node APIs.
     50 
     51 ```javascript
     52 exists( __dirname, done );
     53 
     54 function done( error, bool ) {
     55     if ( error ) {
     56         console.error( error.message );
     57     }
     58     if ( bool ) {
     59         console.log( '...path exists.' );
     60     } else {
     61         console.log( '...path does not exist.' );
     62     }
     63 }
     64 ```
     65 
     66 #### exists.sync( path )
     67 
     68 Synchronously tests whether a path exists on the filesystem.
     69 
     70 ```javascript
     71 var bool = exists.sync( __dirname );
     72 // returns <boolean>
     73 ```
     74 
     75 </section>
     76 
     77 <!-- /.usage -->
     78 
     79 <section class="notes">
     80 
     81 ## Notes
     82 
     83 -   The following is considered an anti-pattern:
     84 
     85     ```javascript
     86     var path = require( 'path' );
     87     var readFileSync = require( '@stdlib/fs/read-file' ).sync;
     88 
     89     var file = path.join( __dirname, 'foo.js' );
     90     if ( exists.sync( __dirname ) ) {
     91         file = readFileSync( file );
     92     }
     93     ```
     94 
     95     Because time elapses between checking for existence and performing IO, at the time IO is performed, the path is no longer guaranteed to exist. In other words, a race condition exists between the process attempting to read and another process attempting to delete.
     96 
     97     Instead, the following pattern is preferred, where `errors` are handled explicitly:
     98 
     99     ```javascript
    100     var path = require( 'path' );
    101     var readFileSync = require( '@stdlib/fs/read-file' ).sync;
    102 
    103     var file = path.join( __dirname, 'foo.js' );
    104     try {
    105         file = readFileSync( file );
    106     } catch ( error ) {
    107         console.log( 'unable to read file.' );
    108         console.error( error );
    109     }
    110     ```
    111 
    112 -   Nevertheless, use cases exist where one desires to check existence **without** performing IO. For example,
    113 
    114     <!-- run-disable -->
    115 
    116     ```javascript
    117     var path = require( 'path' );
    118     var writeFileSync = require( '@stdlib/fs/write-file' ).sync;
    119 
    120     var file = path.join( __dirname, 'foo.js' );
    121     if ( exists.sync( file ) ) {
    122         console.log( 'Don\'t overwrite the file!' );
    123     } else {
    124         writeFileSync( file, 'beep', {
    125             'encoding': 'utf8'
    126         });
    127     }
    128     ```
    129 
    130 </section>
    131 
    132 <!-- /.notes -->
    133 
    134 <section class="examples">
    135 
    136 ## Examples
    137 
    138 <!-- eslint no-undef: "error" -->
    139 
    140 ```javascript
    141 var exists = require( '@stdlib/fs/exists' );
    142 
    143 /* Sync */
    144 
    145 console.log( exists.sync( __dirname ) );
    146 // => true
    147 
    148 console.log( exists.sync( 'beepboop' ) );
    149 // => false
    150 
    151 /* Async */
    152 
    153 exists( __dirname, done );
    154 exists( 'beepboop', done );
    155 
    156 function done( error, bool ) {
    157     if ( error ) {
    158         console.error( error.message );
    159     } else {
    160         console.log( bool );
    161     }
    162 }
    163 ```
    164 
    165 </section>
    166 
    167 <!-- /.examples -->
    168 
    169 * * *
    170 
    171 <section class="cli">
    172 
    173 ## CLI
    174 
    175 <section class="usage">
    176 
    177 ### Usage
    178 
    179 ```text
    180 Usage: exists [options] <path>
    181 
    182 Options:
    183 
    184   -h,    --help                Print this message.
    185   -V,    --version             Print the package version.
    186 ```
    187 
    188 </section>
    189 
    190 <!-- /.usage -->
    191 
    192 <section class="notes">
    193 
    194 ### Notes
    195 
    196 -   Relative paths are resolved relative to the current working directory.
    197 -   Errors are written to `stderr`.
    198 -   Results are written to `stdout`.
    199 
    200 </section>
    201 
    202 <!-- /.notes -->
    203 
    204 <section class="examples">
    205 
    206 ### Examples
    207 
    208 ```bash
    209 $ exists ./../
    210 true || <error_message>
    211 ```
    212 
    213 </section>
    214 
    215 <!-- /.examples -->
    216 
    217 </section>
    218 
    219 <!-- /.cli -->
    220 
    221 <section class="links">
    222 
    223 [node-fs-exists]: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
    224 
    225 </section>
    226 
    227 <!-- /.links -->