diff --git a/lib/node_modules/@stdlib/fs/append-file/README.md b/lib/node_modules/@stdlib/fs/append-file/README.md new file mode 100644 index 000000000000..592f619dabc9 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/README.md @@ -0,0 +1,239 @@ + + +# Append File + +> Append data to a file. + +
+ +## Usage + +```javascript +var appendFile = require( '@stdlib/fs/append-file' ); +``` + +#### appendFile( file, data\[, options], clbk ) + +Asynchronously appends `data` to a `file`. + +```javascript +var join = require( 'path' ).join; + +var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' ); + +appendFile( fpath, 'beep boop\n', onAppend ); + +function onAppend( error ) { + if ( error ) { + console.log( error instanceof Error ); + // => false + } +} +``` + +The `data` argument may be either a `string` or a [`Buffer`][@stdlib/buffer/ctor]. + +```javascript +var join = require( 'path' ).join; +var string2buffer = require( '@stdlib/buffer/from-string' ); + +var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' ); + +appendFile( fpath, string2buffer( 'beep boop\n' ), onAppend ); + +function onAppend( error ) { + if ( error ) { + console.log( error instanceof Error ); + // => false + } +} +``` + +The function accepts the same `options` and has the same defaults as [`fs.appendFile()`][node-fs]. + +#### appendFile.sync( file, data\[, options] ) + +Synchronously appends `data` to a `file`. + +```javascript +var join = require( 'path' ).join; + +var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' ); + +var err = appendFile.sync( fpath, 'beep boop\n' ); +if ( err instanceof Error ) { + throw err; +} +``` + +The function accepts the same `options` and has the same defaults as [`fs.appendFileSync()`][node-fs]. + +
+ + + +
+ +## Notes + +- 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 + + + + ```javascript + var fs = require( 'fs' ); + + // Check for directory path existence to prevent an error being thrown... + if ( fs.existsSync( '/path/to' ) ) { + fs.appendFileSync( '/path/to/file.txt', 'beep boop\n' ); + } + ``` + + can be replaced by an approach which addresses existence via `error` handling. + + + + ```javascript + var appendFile = require( '@stdlib/fs/append-file' ); + + // Explicitly handle the error... + var err = appendFile.sync( '/path/to/file.txt', 'boop beep\n' ); + if ( err instanceof Error ) { + // You choose what to do... + throw err; + } + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var join = require( 'path' ).join; +var appendFile = require( '@stdlib/fs/append-file' ); + +var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' ); + +// Synchronously append data to a file: + +var error = appendFile.sync( fpath, 'beep boop\n', 'utf8' ); +// Function successfully executes and returns null + +console.log( error instanceof Error ); +// => false + +// Asynchronously append data to a file: + +appendFile( fpath, 'beep boop\n', onAppend ); + +function onAppend( error ) { + if ( error ) { + console.error( 'Error: %s', error.message ); + } + console.log( 'Success!!!' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: append-file [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --enc, --encoding encoding Encoding. Default: 'utf8'. + --flag flag Flag. Default: 'a'. + --mode mode Mode. Default: 0o666. +``` + +
+ + + +
+ +### Notes + +- Relative output file paths are resolved relative to the current working directory. +- Errors are written to `stderr`. +- File contents should be provided over `stdin` as part of a [standard stream][standard-stream] pipeline. + +
+ + + +
+ +### Examples + +```bash +$ printf 'beep boop\n' | append-file ./examples/fixtures/file.txt +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/fs/append-file/benchmark/benchmark.js b/lib/node_modules/@stdlib/fs/append-file/benchmark/benchmark.js new file mode 100644 index 000000000000..3735702516b8 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/benchmark/benchmark.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var join = require( 'path' ).join; +var bench = require( '@stdlib/bench' ); +var unlink = require( '@stdlib/fs/unlink' ).sync; +var pkg = require( './../package.json' ).name; +var appendFile = require( './../lib' ); + + +// FIXTURES // + +var TMP = join( __dirname, 'fixtures', 'temp.txt' ); +var DATA = 'boop beep\n'; + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var i; + + i = 0; + b.tic(); + + return next(); + + function next() { + i += 1; + if ( i <= b.iterations ) { + return appendFile( TMP, DATA, done ); + } + b.toc(); + unlink( TMP ); + b.pass( 'benchmark finished' ); + b.end(); + } + + function done( error ) { + if ( error ) { + b.fail( error.message ); + } + next(); + } +}); + +bench( pkg+':sync', function benchmark( b ) { + var out; + var i; + + b.tic(); + for (i = 0; i < b.iterations; i++) { + out = appendFile.sync( TMP, DATA ); + if ( out instanceof Error ) { + b.fail( out.message ); + } + } + b.toc(); + unlink( TMP ); + if ( out instanceof Error ) { + b.fail( out.message ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/fs/append-file/benchmark/fixtures/temp.txt b/lib/node_modules/@stdlib/fs/append-file/benchmark/fixtures/temp.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/node_modules/@stdlib/fs/append-file/bin/cli b/lib/node_modules/@stdlib/fs/append-file/bin/cli new file mode 100644 index 000000000000..74aaa2e85852 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/bin/cli @@ -0,0 +1,110 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/cli/ctor' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var cwd = require( '@stdlib/process/cwd' ); +var appendFile = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var fpath; + var args; + var opts; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync(resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + opts = {}; + if ( flags.encoding ) { + opts.encoding = flags.encoding; + } + if ( flags.flag ) { + opts.flag = flags.flag; + } + if ( flags.mode ) { + opts.mode = parseInt( flags.mode, 8 ); + } + fpath = resolve( cwd(), args[0] ); + + // Gather data from `stdin`... + stdin( onRead ); + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + if ( error ) { + return cli.error( error ); + } + appendFile( fpath, data, opts, onAppend ); + } + + /** + * Callback invoked upon appending a file. + * + * @private + * @param {Error} [err] - error object + * @returns {void} + */ + function onAppend( err ) { + if ( err ) { + return cli.error( err ); + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/fs/append-file/docs/repl.txt b/lib/node_modules/@stdlib/fs/append-file/docs/repl.txt new file mode 100644 index 000000000000..7bd1facfd5d4 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( path, data[, options], clbk ) + Asynchronously appends data to a file. + + Parameters + ---------- + path: string|Buffer|integer + Filename or file descriptor. + + data: string|Buffer + Data to append. + + options: Object|string (optional) + Options. If a string, the value is the encoding. + + options.encoding: string|null (optional) + Encoding. Default: 'utf8'. + + options.flag: string (optional) + Flag. Default: 'a'. + + options.mode: integer (optional) + Mode. Default: 0o666. + + clbk: Function + Callback to invoke upon appending data to the file. + + Examples + -------- + > function onAppend( err ) { + ... if ( err ) { + ... console.error( err.message ); + ... } + ... }; + > {{alias}}( './beep/boop.txt', 'beep boop', onAppend ); + + +{{alias}}.sync( path, data[, options] ) + Synchronously appends data to a file. + + Parameters + ---------- + path: string|Buffer|integer + Filename or file descriptor. + + data: string|Buffer + Data to append. + + options: Object|string (optional) + Options. If a string, the value is the encoding. + + options.encoding: string|null (optional) + Encoding. Default: 'utf8'. + + options.flag: string (optional) + Flag. Default: 'a'. + + options.mode: integer (optional) + Mode. Default: 0o666. + + Returns + ------- + err: Error|null + Error object or null. + + Examples + -------- + > var err = {{alias}}.sync( './beep/boop.txt', 'beep boop' ); + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/fs/append-file/docs/types/index.d.ts b/lib/node_modules/@stdlib/fs/append-file/docs/types/index.d.ts new file mode 100644 index 000000000000..16fd346cbff9 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/docs/types/index.d.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Buffer } from 'buffer'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Encoding (default: null). + */ + encoding?: string | null; + + /** + * Flag (default: 'a'). + */ + flag?: string; + + /** + * Mode (default: 0o666) + */ + mode?: number; +} + +/** +* Callback invoked upon writing data to a file. +* +* @param err - error object +*/ +type Callback = ( err: Error | null ) => void; + +/** +* Interface for appending a file. +*/ +interface AppendFile { + /** + * Asynchronously appends data to a file. + * + * @param path - file path or file descriptor + * @param data - data to append + * @param options - options (if a string, the value is the encoding) + * @param clbk - callback to invoke after appending data to a file + * + * @example + * function onAppend( err ) { + * if ( err ) { + * console.log( err.message ); + * } + * } + * + * var opts = { 'flag': 'r' }; + * appendFile( './beep/boop.txt', 'beep boop\n', opts, onAppend ); + */ + ( path: string | Buffer | number, data: string | Buffer, options: Options | string, clbk: Callback ): void; + + /** + * Asynchronously appends data to a file. + * + * @param path - file path or file descriptor + * @param data - data to append + * @param clbk - callback to invoke after appending data to a file + * + * @example + * function onAppend( err ) { + * if ( err ) { + * console.log( err.message ); + * } + * } + * + * appendFile( './beep/boop.txt', 'beep boop\n', onAppend ); + */ + ( path: string | Buffer | number, data: string | Buffer, clbk: Callback ): void; + + /** + * Synchronously appends data to a file. + * + * @param path - file path or file descriptor + * @param data - data to append + * @param options - options (if a string, the value is the encoding) + * @returns error object or null + * + * @example + * var err = appendFileSync( './beep/boop.txt', 'data to append\n' ); + * if ( err instanceof Error ) { + * throw err; + * } + */ + sync( path: string | Buffer | number, data: string | Buffer, options?: Options | string ): Error | null; +} + +/** +* Asynchronously appends data to a file. +* +* @param path - file path or file descriptor +* @param data - data to append +* @param options - options (if a string, the value is the encoding) +* @param clbk - callback to invoke after appending data to a file +* +* @example +* function onAppend( err ) { +* if ( err ) { +* console.log( err.message ); +* } +* } +* +* appendFile( './beep/boop.txt', 'beep boop\n', onAppend ); +*/ +declare var appendFile: AppendFile; + + +// EXPORTS // + +export = appendFile; diff --git a/lib/node_modules/@stdlib/fs/append-file/docs/types/test.ts b/lib/node_modules/@stdlib/fs/append-file/docs/types/test.ts new file mode 100644 index 000000000000..99d0ffa78e3b --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/docs/types/test.ts @@ -0,0 +1,193 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import appendFile = require( './index' ); + +const onAppend = ( err: Error | null ): void => { + if ( err ) { + throw err; + } +}; + + +// TESTS // + +// The function does not have a return value... +{ + appendFile( './beep/boop.txt', 'beepboop', onAppend ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor... +{ + appendFile( false, 'beepboop', onAppend ); // $ExpectError + appendFile( true, 'beepboop', onAppend ); // $ExpectError + appendFile( null, 'beepboop', onAppend ); // $ExpectError + appendFile( undefined, 'beepboop', onAppend ); // $ExpectError + appendFile( [], 'beepboop', onAppend ); // $ExpectError + appendFile( {}, 'beepboop', onAppend ); // $ExpectError + appendFile( ( x: number ): number => x, 'beepboop', onAppend ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a string or buffer... +{ + appendFile( './beep/boop.txt', false, onAppend ); // $ExpectError + appendFile( './beep/boop.txt', true, onAppend ); // $ExpectError + appendFile( './beep/boop.txt', null, onAppend ); // $ExpectError + appendFile( './beep/boop.txt', undefined, onAppend ); // $ExpectError + appendFile( './beep/boop.txt', [], onAppend ); // $ExpectError + appendFile( './beep/boop.txt', {}, onAppend ); // $ExpectError + appendFile( './beep/boop.txt', ( x: number ): number => x, onAppend ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature... +{ + appendFile( '/path/to/beepboop', 'beepboop', 'abc' ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', 1 ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', false ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', true ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', null ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', undefined ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', [] ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', {} ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object or string... +{ + appendFile( '/path/to/beepboop', 'beepboop', false, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', true, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', null, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', undefined, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', 123, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', [], onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', ( x: number ): number => x, onAppend ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `encoding` option which is not a string or null... +{ + appendFile( '/path/to/beepboop', 'beepboop', { 'encoding': 123 }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'encoding': true }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'encoding': false }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'encoding': [] }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'encoding': {} }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'encoding': ( x: number ): number => x }, onAppend ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `flag` option which is not a string... +{ + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': 123 }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': true }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': false }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': null }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': [] }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': {} }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'flag': ( x: number ): number => x },onAppend ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `mode` option which is not a number... +{ + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': 'abc' }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': true }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': false }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': null }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': [] }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': {} }, onAppend ); // $ExpectError + appendFile( '/path/to/beepboop', 'beepboop', { 'mode': ( x: number ): number => x }, onAppend ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + appendFile(); // $ExpectError + appendFile( 'C:\\foo\\bar\\baz\\beepboop' ); // $ExpectError + appendFile( 'C:\\foo\\bar\\baz\\beepboop', 'beepboop' ); // $ExpectError +} + +// Attached to main export is a `sync` method which returns an error or null... +{ + appendFile.sync( '/path/to/beepboop', 'beepboop' ); // $ExpectType Error | null +} + +// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor... +{ + appendFile.sync( false, 'beepboop' ); // $ExpectError + appendFile.sync( true, 'beepboop' ); // $ExpectError + appendFile.sync( null, 'beepboop' ); // $ExpectError + appendFile.sync( undefined, 'beepboop' ); // $ExpectError + appendFile.sync( [], 'beepboop' ); // $ExpectError + appendFile.sync( {}, 'beepboop' ); // $ExpectError + appendFile.sync( ( x: number ): number => x, 'beepboop' ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a second argument which is not a string or buffer... +{ + appendFile.sync( '/path/to/beepboop', false ); // $ExpectError + appendFile.sync( '/path/to/beepboop', true ); // $ExpectError + appendFile.sync( '/path/to/beepboop', null ); // $ExpectError + appendFile.sync( '/path/to/beepboop', undefined ); // $ExpectError + appendFile.sync( '/path/to/beepboop', [] ); // $ExpectError + appendFile.sync( '/path/to/beepboop', {} ); // $ExpectError + appendFile.sync( '/path/to/beepboop', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an options argument which is not an object or string... +{ + appendFile.sync( '/path/to/beepboop', 'beepboop', null ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', true ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', false ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', 123 ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', [] ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null... +{ + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'encoding': 123 } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'encoding': true } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'encoding': false } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'encoding': [] } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'encoding': {} } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'encoding': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string... +{ + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': 123 } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': true } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': false } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': null } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': [] } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': {} } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'flag': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a `mode` option which is not a number... +{ + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': 'abc' } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': true } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': false } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': null } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': [] } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': {} } ); // $ExpectError + appendFile.sync( '/path/to/beepboop', 'beepboop', { 'mode': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... +{ + appendFile.sync(); // $ExpectError + appendFile.sync( '/path/to/beepboop' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/fs/append-file/docs/usage.txt b/lib/node_modules/@stdlib/fs/append-file/docs/usage.txt new file mode 100644 index 000000000000..8fadf00fcab2 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/docs/usage.txt @@ -0,0 +1,10 @@ + +Usage: append-file [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --enc, --encoding encoding Encoding. Default: 'utf8'. + --flag flag Flag. Default: 'a'. + --mode mode Mode. Default: 0o666. \ No newline at end of file diff --git a/lib/node_modules/@stdlib/fs/append-file/etc/cli_opts.json b/lib/node_modules/@stdlib/fs/append-file/etc/cli_opts.json new file mode 100644 index 000000000000..d290ba89bdfb --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/etc/cli_opts.json @@ -0,0 +1,22 @@ +{ + "string": [ + "encoding", + "flag", + "mode" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ], + "encoding": [ + "enc" + ] + } +} diff --git a/lib/node_modules/@stdlib/fs/append-file/examples/fixtures/file.txt b/lib/node_modules/@stdlib/fs/append-file/examples/fixtures/file.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/node_modules/@stdlib/fs/append-file/examples/index.js b/lib/node_modules/@stdlib/fs/append-file/examples/index.js new file mode 100644 index 000000000000..3317113255db --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var join = require( 'path' ).join; +var appendFile = require( './../lib' ); + +var fpath = join( __dirname, 'fixtures', 'file.txt' ); + +// Synchronously appends data to a file: + +var err = appendFile.sync( fpath, 'beep boop\n', 'utf8' ); +// returns null + +console.log( err instanceof Error ); +// => false + +// Asynchronously append data to a file: + +appendFile( fpath, 'appended something\n', onAppend ); + +function onAppend( err ) { + if ( err ) { + console.error( 'Error: %s', err.message ); + } + console.log( 'Success!' ); +} diff --git a/lib/node_modules/@stdlib/fs/append-file/lib/async.js b/lib/node_modules/@stdlib/fs/append-file/lib/async.js new file mode 100644 index 000000000000..f6a1bb06b844 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/lib/async.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var append = require( 'fs' ).appendFile; + + +// MAIN // + +/** +* Asynchronously append data to a file, creating the file if it does not yet exist. +* +* @param {(string|Buffer|integer)} path - file path or file descriptor +* @param {(string|Buffer)} data - data to append +* @param {(Object|string)} [options] - options +* @param {Function} clbk - callback to invoke after appending data to the file +* +* @example +* function onAppend( err ) { +* if ( err ) { +* console.log( err.message ); +* } +* } +* +* appendFile( './beep/boop.txt', 'appended something\n', onAppend ); +*/ +function appendFile() { + var args; + var i; + args = []; + for (i = 0; i < arguments.length; i++) { + args.push( arguments[i] ); + } + append.apply( null, args ); +} + + +// EXPORTS // + +module.exports = appendFile; diff --git a/lib/node_modules/@stdlib/fs/append-file/lib/index.js b/lib/node_modules/@stdlib/fs/append-file/lib/index.js new file mode 100644 index 000000000000..cc1adb27a525 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/lib/index.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Append data to a file. +* +* @module @stdlib/fs/append-file +* +* @example +* var appendFile = require( '@stdlib/fs/append-file' ); +* +* function onAppend( err ) { +* if ( err ) { +* console.log( err.message ); +* } +* } +* +* appendFile( './beep/boop.txt', 'appended something\n', onAppend ); +* +* @example +* var appendFileSync = require( '@stdlib/fs/append-file' ).sync; +* +* var err = appendFileSync( './beep/boop.txt', 'data to append\n' ); +* if ( err instanceof Error ) { +* throw err; +* } +*/ + + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var async = require( './async.js' ); +var sync = require( './sync.js' ); + + +// MAIN // + +setReadOnly( async, 'sync', sync ); + + +// EXPORTS // + +module.exports = async; diff --git a/lib/node_modules/@stdlib/fs/append-file/lib/sync.js b/lib/node_modules/@stdlib/fs/append-file/lib/sync.js new file mode 100644 index 000000000000..42e9778027f7 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/lib/sync.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var appendFile = require( 'fs' ).appendFileSync; // eslint-disable-line node/no-sync + + +// MAIN // + +/** +* Synchronously append data to a file, creating the file if it does not yet exist. +* +* @param {(string|Buffer|integer)} path - file path or file descriptor +* @param {(string|Buffer)} data - data to append +* @param {(Object|string)} [options] - options +* @returns {(Error|null)} error object or null +* +* @example +* var err = appendFileSync( './beep/boop.txt', 'data to append\n' ); +* if ( err instanceof Error ) { +* throw err; +* } +*/ +function appendFileSync( path, data, options ) { + try { + if ( arguments.length > 2 ) { + appendFile( path, data, options ); + } else { + appendFile( path, data ); + } + } catch ( err ) { + return err; + } + return null; +} + + +// EXPORTS // + +module.exports = appendFileSync; diff --git a/lib/node_modules/@stdlib/fs/append-file/package.json b/lib/node_modules/@stdlib/fs/append-file/package.json new file mode 100644 index 000000000000..8df56b43810d --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/fs/append-file", + "version": "0.0.0", + "description": "Append data to a file.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "append-file": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdfs", + "fs", + "appendfile", + "appendfilesync", + "path", + "async", + "sync", + "file", + "append", + "appends", + "open", + "filesystem" + ] +} diff --git a/lib/node_modules/@stdlib/fs/append-file/test/fixtures/file.txt b/lib/node_modules/@stdlib/fs/append-file/test/fixtures/file.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/lib/node_modules/@stdlib/fs/append-file/test/fixtures/stdin_error.js.txt b/lib/node_modules/@stdlib/fs/append-file/test/fixtures/stdin_error.js.txt new file mode 100644 index 000000000000..946e04c23fc9 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/test/fixtures/stdin_error.js.txt @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; +proc.argv[ 2 ] = resolve( __dirname, 'fixtures', 'file.txt' ); + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/lib/node_modules/@stdlib/fs/append-file/test/fixtures/write_error.js.txt b/lib/node_modules/@stdlib/fs/append-file/test/fixtures/write_error.js.txt new file mode 100644 index 000000000000..bd1b7d203b09 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/test/fixtures/write_error.js.txt @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; +proc.argv[ 2 ] = resolve( __dirname, 'fixtures', 'file.txt' ); + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin, + './../lib': appendFile +}); + +function stdin( clbk ) { + clbk( null, 'beep boop foo' ); +} + +function appendFile() { + var clbk = arguments[ arguments.length-1 ]; + setTimeout( onTimeout, 0 ); + function onTimeout() { + clbk( new Error( 'beep' ) ); + } +} diff --git a/lib/node_modules/@stdlib/fs/append-file/test/test.async.js b/lib/node_modules/@stdlib/fs/append-file/test/test.async.js new file mode 100644 index 000000000000..1b422cb23980 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/test/test.async.js @@ -0,0 +1,201 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var join = require( 'path' ).join; +var tape = require( 'tape' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var writeFileSync = require( '@stdlib/fs/write-file' ).sync; +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var string2buffer = require( '@stdlib/buffer/from-string' ); +var appendFile = require( './../lib/async.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; +var fpath = join( __dirname, 'fixtures', 'file.txt' ); +var DATA = readFile( fpath, 'utf8' ); + + +// FUNCTIONS // + +/** +* Restores a fixture file. +* +* ## Notes +* +* - Every function which has the **potential** to affect the fixture file should invoke this function immediately before calling `t.end()`. +* +* @private +*/ +function restore() { + writeFileSync( fpath, DATA ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof appendFile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function appends data to a file (string)', opts, function test( t ) { + var expected = 'beep boop 1'; + appendFile( fpath, 'beep boop 1', onAppend ); + + function onAppend( error ) { + var actual; + if ( error ) { + t.fail( error.message ); + } + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'the function appends data to a file (buffer)', opts, function test( t ) { + var expected = 'beep boop 2'; + appendFile( fpath, string2buffer('beep boop 2'), onAppend ); + + function onAppend( error ) { + var actual; + if ( error ) { + t.fail( error.message ); + } + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'the function appends data to a file using provided options (option string)', opts, function test( t ) { + var expected = 'beep boop 3'; + appendFile( fpath, 'beep boop 3', 'utf8', onAppend ); + + function onAppend( error ) { + var actual; + if ( error ) { + t.fail( error.message ); + } + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'the function appends data to a file using provided options (option object)', opts, function test( t ) { + var expected; + var opts; + + expected = 'beep boop 4'; + opts = { + 'encoding': 'utf8' + }; + appendFile( fpath, 'beep boop 4', opts, onAppend ); + + function onAppend( error ) { + var actual; + if ( error ) { + t.fail( error.message ); + } + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'if the function encounters an error, the function returns the error', opts, function test( t ) { + var file = 'beepboopbapbop/dkfjldjfaklsjf/bdlfalfas/bkldflakfjas/travashHRD'; // non-existent directory path + appendFile( file, 'beepboopbapbop', onAppend ); + + function onAppend( error ) { + var expected; + var actual; + + t.strictEqual( error instanceof Error, true, error.message ); + + expected = DATA; + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'if the function encounters an error, the function returns the error (option string)', opts, function test( t ) { + var file = 'beepboopbapbop/dkfjldjfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + appendFile( file, 'beepboopbapbop', 'utf8', onAppend ); + + function onAppend( error ) { + var expected; + var actual; + + t.strictEqual( error instanceof Error, true, error.message ); + + expected = DATA; + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); + +tape( 'if the function encounters an error, the function returns the error (option object)', opts, function test( t ) { + var file; + var opts; + + file = 'beepboopbapbop/dkfjldjfaklsjf/bdlfalftisas/bkldflHRDakfjas'; // non-existent directory path + opts = { + 'encoding': 'utf8' + }; + appendFile( file, 'beepboopbapbop', opts, onAppend ); + + function onAppend( error ) { + var expected; + var actual; + + t.strictEqual( error instanceof Error, true, error.message ); + + expected = DATA; + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/fs/append-file/test/test.cli.js b/lib/node_modules/@stdlib/fs/append-file/test/test.cli.js new file mode 100644 index 000000000000..7523f3c2733f --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/test/test.cli.js @@ -0,0 +1,270 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var join = require( 'path' ).join; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var writeFileSync = require( '@stdlib/fs/write-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; +var FILE = join( __dirname, 'fixtures', 'file.txt' ); +var DATA = readFileSync( FILE ); + + +// FIXTURES // + +var PKG_VERSION = require( '@stdlib/fs/append-file/package.json' ).version; + + +// FUNCTIONS // + +/** +* Restores a fixture file. +* +* ## Notes +* +* - Every function which has the **potential** to affect the fixture file should invoke this function immediately before calling `t.end()`. +* +* @private +*/ +function restore() { + writeFileSync( FILE, DATA ); +} + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join(' '), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION + '\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface appends data received from `stdin` to a file', opts, function test( t ) { + var expected; + var opts; + var cmd; + + cmd = [ + 'printf "beep boop 1"', + '|', + EXEC_PATH, + fpath, + FILE + ]; + opts = {}; + + expected = 'beep boop 1'; + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + var actual; + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + actual = readFileSync( FILE, 'utf8' ); + t.strictEqual(actual, expected, 'file contains expected contents'); + + restore(); + t.end(); + } +}); + +tape( 'if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, "'", '"' ); + + cmd = [ + EXEC_PATH, + '-e', + "'" + script + "'" + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + + restore(); + t.end(); + } +}); + +tape( 'if an error is encountered when appending to file, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'write_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, "'", '"' ); + + cmd = [ + EXEC_PATH, + '-e', + "'" + script + "'" + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + + restore(); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/fs/append-file/test/test.js b/lib/node_modules/@stdlib/fs/append-file/test/test.js new file mode 100644 index 000000000000..4717bee28bf4 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var appendFile = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof appendFile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method that synchronously appends data to a file', function test( t ) { + t.strictEqual( typeof appendFile.sync, 'function', 'has method' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/fs/append-file/test/test.sync.js b/lib/node_modules/@stdlib/fs/append-file/test/test.sync.js new file mode 100644 index 000000000000..ba717ba041b2 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/append-file/test/test.sync.js @@ -0,0 +1,191 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var join = require( 'path' ).join; +var tape = require( 'tape' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var writeFile = require( '@stdlib/fs/write-file' ).sync; +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var string2buffer = require( '@stdlib/buffer/from-string' ); +var appendFile = require( './../lib/sync.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; +var fpath = join( __dirname, 'fixtures', 'file.txt' ); +var DATA = readFile( fpath, 'utf8' ); + + +// FUNCTIONS // + +/** +* Restores a fixture file. +* +* ## Notes +* +* - Every function which has the **potential** to affect the fixture file should invoke this function immediately before calling `t.end()`. +* +* @private +*/ +function restore() { + writeFile( fpath, DATA ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof appendFile, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function appends data to a file (string)', opts, function test( t ) { + var expected; + var actual; + + expected = 'beep boop 1'; + + appendFile( fpath, 'beep boop 1' ); + actual = readFile( fpath, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'the function appends data to a file (buffer)', opts, function test( t ) { + var expected; + var actual; + + expected = 'beep boop 2'; + + appendFile( fpath, string2buffer( 'beep boop 2' ) ); + actual = readFile( fpath, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'the function appends data to a file using provided options (string)', opts, function test( t ) { + var expected; + var actual; + + expected = 'beep boop 3'; + + appendFile( fpath, 'beep boop 3', 'utf8' ); + actual = readFile( fpath, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'the function appends data to a file using provided options (object)', opts, function test( t ) { + var expected; + var actual; + var opts; + + expected = 'beep boop 4'; + + opts = { + 'encoding': 'utf8' + }; + appendFile( fpath, 'beep boop 4', opts ); + actual = readFile( fpath, 'utf8' ); + + t.strictEqual( actual, expected, 'file contains expected data' ); + + restore(); + t.end(); +}); + +tape( 'if the function encounters an error, the function returns the error', opts, function test( t ) { + var expected; + var actual; + var file; + var err; + + file = 'beepboopbapbop/dkfjldjfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + err = appendFile( file, 'beepboopbapbop' ); + + t.strictEqual( err instanceof Error, true, err.message ); + + expected = DATA; + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); +}); + +tape( 'if the function encounters an error, the function throws the error (option string)', opts, function test( t ) { + var expected; + var actual; + var file; + var err; + + file = 'beepboopbapbop/dkfjldjfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + err = appendFile( file, 'beepboopbapbop', 'utf8' ); + + t.strictEqual( err instanceof Error, true, err.message ); + + expected = DATA; + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); +}); + +tape( 'if the function encounters an error, the function throws the error (option object)', opts, function test( t ) { + var expected; + var actual; + var file; + var opts; + var err; + + file = 'beepboopbapbop/dkfjldjfaklsjf/bdlfalfas/bkldflakfjas'; // non-existent directory path + + opts = { + 'encoding': 'utf8' + }; + + err = appendFile( file, 'beepboopbapbop', opts ); + + t.strictEqual( err instanceof Error, true, err.message ); + + expected = DATA; + actual = readFile( fpath, 'utf8' ); + t.strictEqual( actual, expected, 'file contains expected contents' ); + + restore(); + t.end(); +});