time-to-botec

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

README.md (4868B)


      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 # Write File
     22 
     23 > Write data to a file.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var writeFile = require( '@stdlib/fs/write-file' );
     31 ```
     32 
     33 #### writeFile( file, data\[, options], clbk )
     34 
     35 Asynchronously write `data` to a `file`.
     36 
     37 ```javascript
     38 var join = require( 'path' ).join;
     39 
     40 var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
     41 
     42 writeFile( fpath, 'beep boop\n', onWrite );
     43 
     44 function onWrite( error ) {
     45     if ( error ) {
     46         throw error;
     47     }
     48 }
     49 ```
     50 
     51 The `data` argument may be either a `string` or a [`Buffer`][@stdlib/buffer/ctor].
     52 
     53 ```javascript
     54 var join = require( 'path' ).join;
     55 var string2buffer = require( '@stdlib/buffer/from-string' );
     56 
     57 var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
     58 
     59 writeFile( fpath, string2buffer( 'beep boop\n' ), onWrite );
     60 
     61 function onWrite( error ) {
     62     if ( error ) {
     63         throw error;
     64     }
     65 }
     66 ```
     67 
     68 The function accepts the same `options` and has the same defaults as [`fs.writeFile()`][node-fs].
     69 
     70 #### writeFile.sync( file, data\[, options] )
     71 
     72 Synchronously writes `data` to a `file`.
     73 
     74 ```javascript
     75 var join = require( 'path' ).join;
     76 
     77 var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
     78 
     79 var err = writeFile.sync( fpath, 'beep boop\n' );
     80 if ( err instanceof Error ) {
     81     throw err;
     82 }
     83 ```
     84 
     85 The function accepts the same `options` and has the same defaults as [`fs.writeFileSync()`][node-fs].
     86 
     87 </section>
     88 
     89 <!-- /.usage -->
     90 
     91 <section class="notes">
     92 
     93 ## Notes
     94 
     95 -   The difference between this `writeFile.sync` and [`fs.writeFileSync()`][node-fs] is that [`fs.writeFileSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent directory path) and this API will return an `error`. Hence, the following anti-pattern
     96 
     97     <!-- run-disable -->
     98 
     99     ```javascript
    100     var fs = require( 'fs' );
    101 
    102     // Check for directory path existence to prevent an error being thrown...
    103     if ( fs.existsSync( '/path/to' ) ) {
    104         fs.writeFileSync( '/path/to/file.txt', 'beep boop\n' );
    105     }
    106     ```
    107 
    108     can be replaced by an approach which addresses existence via `error` handling.
    109 
    110     <!-- run-disable -->
    111 
    112     ```javascript
    113     var writeFile = require( '@stdlib/fs/write-file' );
    114 
    115     // Explicitly handle the error...
    116     var err = writeFile.sync( '/path/to/file.txt', 'beep boop\n' );
    117     if ( err instanceof Error ) {
    118         // You choose what to do...
    119         throw err;
    120     }
    121     ```
    122 
    123 </section>
    124 
    125 <!-- /.notes -->
    126 
    127 <section class="examples">
    128 
    129 ## Examples
    130 
    131 <!-- eslint no-undef: "error" -->
    132 
    133 ```javascript
    134 var join = require( 'path' ).join;
    135 var writeFile = require( '@stdlib/fs/write-file' );
    136 
    137 var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
    138 
    139 // Synchronously write data to a file:
    140 var err = writeFile.sync( fpath, 'beep boop\n', 'utf8' );
    141 // returns null
    142 
    143 console.log( err instanceof Error );
    144 // => false
    145 
    146 // Asynchronously write data to a file:
    147 writeFile( fpath, 'beep boop\n', onWrite );
    148 
    149 function onWrite( error ) {
    150     if ( error ) {
    151         console.error( 'Error: %s', error.message );
    152     }
    153     console.log( 'Success!' );
    154 }
    155 ```
    156 
    157 </section>
    158 
    159 <!-- /.examples -->
    160 
    161 * * *
    162 
    163 <section class="cli">
    164 
    165 ## CLI
    166 
    167 <section class="usage">
    168 
    169 ### Usage
    170 
    171 ```text
    172 Usage: write-file [options] <filepath>
    173 
    174 Options:
    175 
    176   -h,    --help                Print this message.
    177   -V,    --version             Print the package version.
    178   --enc, --encoding encoding   Encoding. Default: 'utf8'.
    179          --flag flag           Flag. Default: 'r'.
    180          --mode mode           Mode. Default: 0o666.
    181 ```
    182 
    183 </section>
    184 
    185 <!-- /.usage -->
    186 
    187 <section class="notes">
    188 
    189 ### Notes
    190 
    191 -   Relative output file paths are resolved relative to the current working directory.
    192 -   Errors are written to `stderr`.
    193 -   File contents should be provided over `stdin` as part of a [standard stream][standard-stream] pipeline.
    194 
    195 </section>
    196 
    197 <!-- /.notes -->
    198 
    199 <section class="examples">
    200 
    201 ### Examples
    202 
    203 ```bash
    204 $ printf 'beep boop\n' | write-file ./examples/fixtures/file.txt
    205 ```
    206 
    207 </section>
    208 
    209 <!-- /.examples -->
    210 
    211 </section>
    212 
    213 <!-- /.cli -->
    214 
    215 <section class="links">
    216 
    217 [node-fs]: https://nodejs.org/api/fs.html
    218 
    219 [@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer-ctor
    220 
    221 [standard-stream]: http://en.wikipedia.org/wiki/Pipeline_%28Unix%29
    222 
    223 </section>
    224 
    225 <!-- /.links -->