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