Skip to content

Commit 14dda68

Browse files
HRIDYANSHU054Hridyanshu7stdlib-botPlaneshifter
authored
feat: add fs/append-file
PR-URL: #1967 --------- Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Hridyanshu7 <himank7794@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent ec98887 commit 14dda68

File tree

22 files changed

+1928
-0
lines changed

22 files changed

+1928
-0
lines changed
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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 -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var join = require( 'path' ).join;
24+
var bench = require( '@stdlib/bench' );
25+
var unlink = require( '@stdlib/fs/unlink' ).sync;
26+
var pkg = require( './../package.json' ).name;
27+
var appendFile = require( './../lib' );
28+
29+
30+
// FIXTURES //
31+
32+
var TMP = join( __dirname, 'fixtures', 'temp.txt' );
33+
var DATA = 'boop beep\n';
34+
35+
36+
// MAIN //
37+
38+
bench( pkg, function benchmark( b ) {
39+
var i;
40+
41+
i = 0;
42+
b.tic();
43+
44+
return next();
45+
46+
function next() {
47+
i += 1;
48+
if ( i <= b.iterations ) {
49+
return appendFile( TMP, DATA, done );
50+
}
51+
b.toc();
52+
unlink( TMP );
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
}
56+
57+
function done( error ) {
58+
if ( error ) {
59+
b.fail( error.message );
60+
}
61+
next();
62+
}
63+
});
64+
65+
bench( pkg+':sync', function benchmark( b ) {
66+
var out;
67+
var i;
68+
69+
b.tic();
70+
for (i = 0; i < b.iterations; i++) {
71+
out = appendFile.sync( TMP, DATA );
72+
if ( out instanceof Error ) {
73+
b.fail( out.message );
74+
}
75+
}
76+
b.toc();
77+
unlink( TMP );
78+
if ( out instanceof Error ) {
79+
b.fail( out.message );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
});

lib/node_modules/@stdlib/fs/append-file/benchmark/fixtures/temp.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)