From c5afb00b7abb61dc4394b78d35022589d575dda5 Mon Sep 17 00:00:00 2001 From: blessed Date: Sat, 21 Mar 2026 11:38:09 +0100 Subject: [PATCH 1/4] feat: add fs/stat --- lib/node_modules/@stdlib/fs/README.md | 3 + .../@stdlib/fs/docs/types/index.d.ts | 39 +++ lib/node_modules/@stdlib/fs/lib/index.js | 9 + lib/node_modules/@stdlib/fs/stat/README.md | 256 ++++++++++++++++++ .../@stdlib/fs/stat/benchmark/benchmark.js | 44 +++ lib/node_modules/@stdlib/fs/stat/bin/cli | 84 ++++++ .../@stdlib/fs/stat/docs/types/index.d.ts | 147 ++++++++++ .../@stdlib/fs/stat/docs/types/test.ts | 120 ++++++++ .../@stdlib/fs/stat/docs/usage.txt | 7 + .../@stdlib/fs/stat/etc/cli_opts.json | 14 + .../@stdlib/fs/stat/examples/index.js | 100 +++++++ lib/node_modules/@stdlib/fs/stat/lib/async.js | 93 +++++++ lib/node_modules/@stdlib/fs/stat/lib/index.js | 84 ++++++ lib/node_modules/@stdlib/fs/stat/lib/sync.js | 88 ++++++ lib/node_modules/@stdlib/fs/stat/package.json | 67 +++++ .../@stdlib/fs/stat/test/test.async.js | 249 +++++++++++++++++ .../@stdlib/fs/stat/test/test.cli.js | 183 +++++++++++++ lib/node_modules/@stdlib/fs/stat/test/test.js | 47 ++++ .../@stdlib/fs/stat/test/test.sync.js | 217 +++++++++++++++ 19 files changed, 1851 insertions(+) create mode 100644 lib/node_modules/@stdlib/fs/stat/README.md create mode 100644 lib/node_modules/@stdlib/fs/stat/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/fs/stat/bin/cli create mode 100644 lib/node_modules/@stdlib/fs/stat/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/fs/stat/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/fs/stat/docs/usage.txt create mode 100644 lib/node_modules/@stdlib/fs/stat/etc/cli_opts.json create mode 100644 lib/node_modules/@stdlib/fs/stat/examples/index.js create mode 100644 lib/node_modules/@stdlib/fs/stat/lib/async.js create mode 100644 lib/node_modules/@stdlib/fs/stat/lib/index.js create mode 100644 lib/node_modules/@stdlib/fs/stat/lib/sync.js create mode 100644 lib/node_modules/@stdlib/fs/stat/package.json create mode 100644 lib/node_modules/@stdlib/fs/stat/test/test.async.js create mode 100644 lib/node_modules/@stdlib/fs/stat/test/test.cli.js create mode 100644 lib/node_modules/@stdlib/fs/stat/test/test.js create mode 100644 lib/node_modules/@stdlib/fs/stat/test/test.sync.js diff --git a/lib/node_modules/@stdlib/fs/README.md b/lib/node_modules/@stdlib/fs/README.md index 7c0bd618462f..002736e1e284 100644 --- a/lib/node_modules/@stdlib/fs/README.md +++ b/lib/node_modules/@stdlib/fs/README.md @@ -57,6 +57,7 @@ var f = fs; - [`resolveParentPathBy( path[, options], predicate, clbk )`][@stdlib/fs/resolve-parent-path-by]: resolve a path according to a predicate function by walking parent directories. - [`resolveParentPath( path[, options], clbk )`][@stdlib/fs/resolve-parent-path]: resolve a path by walking parent directories. - [`resolveParentPaths( paths[, options], clbk )`][@stdlib/fs/resolve-parent-paths]: resolve paths from a set of paths by walking parent directories. +- [`stat( path[, options], clbk )`][@stdlib/fs/stat]: show full details on the statistics of a file or directory. - [`unlink( path, clbk )`][@stdlib/fs/unlink]: remove a directory entry. - [`writeFile( file, data[, options], clbk )`][@stdlib/fs/write-file]: write data to a file. @@ -129,6 +130,8 @@ console.log( objectKeys( fs ) ); [@stdlib/fs/resolve-parent-paths]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/resolve-parent-paths +[@stdlib/fs/stat]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/stat + [@stdlib/fs/unlink]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/unlink [@stdlib/fs/write-file]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/write-file diff --git a/lib/node_modules/@stdlib/fs/docs/types/index.d.ts b/lib/node_modules/@stdlib/fs/docs/types/index.d.ts index 7f82739b4ba0..445975b763bf 100644 --- a/lib/node_modules/@stdlib/fs/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/fs/docs/types/index.d.ts @@ -34,6 +34,7 @@ import rename = require( '@stdlib/fs/rename' ); import resolveParentPath = require( '@stdlib/fs/resolve-parent-path' ); import resolveParentPathBy = require( '@stdlib/fs/resolve-parent-path-by' ); import resolveParentPaths = require( '@stdlib/fs/resolve-parent-paths' ); +import stat = require( '@stdlib/fs/stat' ); import unlink = require( '@stdlib/fs/unlink' ); import writeFile = require( '@stdlib/fs/write-file' ); @@ -449,6 +450,44 @@ interface Namespace { */ resolveParentPaths: typeof resolveParentPaths; + /** + * Asynchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @param clbk - callback to invoke after retrieving file statistics + * + * @example + * var stat = require( '@stdlib/fs/stat' ); + * + * stat( __filename, onStat ); + * stat( __dirname, onStat ); + * + * function onStat( error, stats ) { + * if ( error ) { + * throw error; + * } + * console.log( stats.isFile() ); + * console.log( stats.isDirectory() ); + * console.log( stats ); + * } + * + * @example + * var out = stat.sync( __filename ); + * if ( out instanceof Error ) { + * throw out; + * } + * console.log( out.isFile() ); + * console.log( out ); + * + * out = stat.sync( __dirname ); + * if ( out instanceof Error ) { + * throw out; + * } + * console.log( out.isDirectory() ); + * console.log( out ); + */ + stat: typeof stat; + /** * Asynchronously removes a directory entry. * diff --git a/lib/node_modules/@stdlib/fs/lib/index.js b/lib/node_modules/@stdlib/fs/lib/index.js index b17416a1ea67..64aec56c6cca 100644 --- a/lib/node_modules/@stdlib/fs/lib/index.js +++ b/lib/node_modules/@stdlib/fs/lib/index.js @@ -162,6 +162,15 @@ setReadOnly( fs, 'resolveParentPathBy', require( '@stdlib/fs/resolve-parent-path */ setReadOnly( fs, 'resolveParentPaths', require( '@stdlib/fs/resolve-parent-paths' ) ); +/** +* @name stat +* @memberof fs +* @readonly +* @type {Function} +* @see {@link module:@stdlib/fs/stat} +*/ +setReadOnly( fs, 'stat', require( '@stdlib/fs/stat' ) ); + /** * @name unlink * @memberof fs diff --git a/lib/node_modules/@stdlib/fs/stat/README.md b/lib/node_modules/@stdlib/fs/stat/README.md new file mode 100644 index 000000000000..dedc210b04fa --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/README.md @@ -0,0 +1,256 @@ + + +# Stat + +> Get full details on a file or directory. + +
+ +## Usage + +```javascript +var stat = require( '@stdlib/fs/stat' ); +``` + +#### stat( path, clbk ) + +Asynchronously returns file system statistics for a file or directory. + +The callback is invoked with an `fs.Stats` object containing methods and values describing the target path. + +```javascript +stat( __filename, onStat ); + +function onStat( error, stats ) { + if ( error ) { + throw error; + } + console.log( stats.isFile() ); + console.log( stats.isDirectory() ); + console.log( stats.isSymbolicLink() ); + console.log( stats.size ); + console.log( stats.mode ); + console.log( stats.atime ); +} +``` + +#### stat( path, options, clbk ) + +Asynchronously returns file system statistics for a file or directory with options. + +```javascript +stat( __filename, { + 'bigint': true +}, onStat ); + +function onStat( error, stats ) { + if ( error ) { + throw error; + } + console.log( stats.size ); + console.log( stats.atimeNs ); +} +``` + +Options: + +- **bigint**: `boolean` flag indicating whether numeric values should be returned as `bigint`. Default: `false`. + +```javascript +stat( __filename, onStat ); + +function onStat( error, stats ) { + if ( error ) { + throw error; + } + console.log( stats.isFile() ); +} +``` + +#### stat.sync( path ) + +Synchronously returns file system statistics for a file or directory. + +```javascript +var out = stat.sync( __filename ); +if ( out instanceof Error ) { + throw out; +} +console.log( out.isFile() ); +console.log( out.isSocket() ); +console.log( out.size ); +console.log( out.mtimeMs ); +``` + +#### stat.sync( path, options ) + +Synchronously returns file system statistics for a file or directory with options. + +```javascript +var out = stat.sync( __filename, { + 'bigint': true +}); +if ( out instanceof Error ) { + throw out; +} +console.log( out.size ); +console.log( out.birthtimeNs ); +``` + +The returned `fs.Stats` instance provides these type-check methods: + +- `isFile()` +- `isDirectory()` +- `isBlockDevice()` +- `isCharacterDevice()` +- `isFIFO()` +- `isSocket()` +- `isSymbolicLink()` + +The returned `fs.Stats` instance also provides values such as: + +- `dev`, `ino`, `mode`, `nlink`, `uid`, `gid`, `rdev`, `size`, `blksize`, `blocks` +- `atime`, `mtime`, `ctime`, `birthtime` +- `atimeMs`, `mtimeMs`, `ctimeMs`, `birthtimeMs` +- `atimeNs`, `mtimeNs`, `ctimeNs`, `birthtimeNs` (available when `bigint: true`) + +```javascript +var out = stat.sync( __filename ); +if ( out instanceof Error ) { + throw out; +} +console.log( out.isFile() ); +``` + +
+ + + +
+ +## Examples + + + +```javascript +var stat = require( '@stdlib/fs/stat' ); + +var out = stat.sync( __filename ); +if ( out instanceof Error ) { + throw out; +} +console.log( out.isFile() ); + +stat( __filename, onStat ); + +function onStat( error, stats ) { + if ( error ) { + throw error; + } + console.log( stats.isFile() ); + console.log( stats.isDirectory() ); + console.log( stats.isBlockDevice() ); + console.log( stats.isCharacterDevice() ); + console.log( stats.isFIFO() ); + console.log( stats.isSocket() ); + console.log( stats.isSymbolicLink() ); + console.log( stats.size ); + console.log( stats.mode ); + console.log( stats.uid ); + console.log( stats.gid ); + console.log( stats.atimeMs ); + console.log( stats.mtimeMs ); + console.log( stats.ctimeMs ); + console.log( stats.birthtimeMs ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: stat [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Notes + +- Relative paths are resolved relative to the current working directory. +- Errors are written to `stderr`. +- Results are written to `stdout` as JSON. + +
+ + + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/fs/stat/benchmark/benchmark.js b/lib/node_modules/@stdlib/fs/stat/benchmark/benchmark.js new file mode 100644 index 000000000000..71adfe52ff03 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/benchmark/benchmark.js @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2018 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 bench = require( '@stdlib/bench' ); +var stat = require( './../lib' ).sync; + +bench( 'stat.sync', function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = stat( __filename ); + if ( out instanceof Error ) { + b.fail( out.message ); + } + if ( typeof out.isFile !== 'function' ) { + b.fail( 'unexpected return value' ); + } + } + b.toc(); + if ( out instanceof Error ) { + b.fail( out.message ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/fs/stat/bin/cli b/lib/node_modules/@stdlib/fs/stat/bin/cli new file mode 100644 index 000000000000..fcaf7bb9e3c1 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/bin/cli @@ -0,0 +1,84 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 cwd = require( '@stdlib/process/cwd' ); +var stat = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var fpath; + var args; + 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(); + fpath = resolve( cwd(), args[ 0 ] ); + + stat( fpath, onStat ); + + /** + * Callback invoked upon retrieving file statistics. + * + * @private + * @param {(Error|null)} error - error object + * @param {Stats} stats - file statistics + * @returns {void} + */ + function onStat( error, stats ) { + if ( error ) { + return cli.error( error ); + } + // Print a JSON representation to enable stable testing: + console.log( JSON.stringify( stats, null, 2 ) ); // eslint-disable-line no-console + } +} + +main(); diff --git a/lib/node_modules/@stdlib/fs/stat/docs/types/index.d.ts b/lib/node_modules/@stdlib/fs/stat/docs/types/index.d.ts new file mode 100644 index 000000000000..5ae934b6de99 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/docs/types/index.d.ts @@ -0,0 +1,147 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; +import { Stats, BigIntStats } from 'fs'; + +/** +* Options for retrieving file statistics. +*/ +interface Options { + /** + * Whether the numeric values in the returned Stats object should be bigint. + * + * Default: false + */ + bigint?: boolean; +} + +/** +* Options for retrieving file statistics with bigint numeric values. +*/ +interface BigIntOptions { + /** + * Whether the numeric values in the returned Stats object should be bigint. + */ + bigint: true; +} + +/** +* Callback invoked after retrieving file statistics. +* +* @param err - error argument +* @param stats - file statistics +*/ +type Callback = ( err: Error | null, stats: Stats ) => void; + +/** +* Callback invoked after retrieving file statistics with bigint numeric values. +* +* @param err - error argument +* @param stats - file statistics +*/ +type BigIntCallback = ( err: Error | null, stats: BigIntStats ) => void; + +/** +* Interface for retrieving file statistics. +*/ +interface Stat { + /** + * Asynchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @param clbk - callback to invoke after retrieving file statistics + */ + ( path: string | Buffer, clbk: Callback ): void; + + /** + * Asynchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @param options - function options + * @param clbk - callback to invoke after retrieving file statistics + */ + ( path: string | Buffer, options: Options, clbk: Callback ): void; + + /** + * Asynchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @param options - function options + * @param clbk - callback to invoke after retrieving file statistics + */ + ( path: string | Buffer, options: BigIntOptions, clbk: BigIntCallback ): void; + + /** + * Synchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @returns file statistics or an error + */ + sync( path: string | Buffer ): Stats | Error; + + /** + * Synchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @param options - function options + * @returns file statistics or an error + */ + sync( path: string | Buffer, options: Options ): Stats | Error; + + /** + * Synchronously returns file system statistics for a file or directory. + * + * @param path - file path + * @param options - function options + * @returns file statistics or an error + */ + sync( path: string | Buffer, options: BigIntOptions ): BigIntStats | Error; +} + +/** +* Asynchronously returns file system statistics for a file or directory. +* +* @param path - file path +* @param clbk - callback to invoke after retrieving file statistics +* +* @example +* var stat = require( '@stdlib/fs/stat' ); +* +* stat( __filename, onStat ); +* stat( __dirname, onStat ); +* +* function onStat( error, stats ) { +* if ( error ) { +* throw error; +* } +* console.log( stats.isFile() ); +* console.log( stats.isDirectory() ); +* console.log( stats ); +* } +*/ +declare var stat: Stat; + + +// EXPORTS // + +export = stat; diff --git a/lib/node_modules/@stdlib/fs/stat/docs/types/test.ts b/lib/node_modules/@stdlib/fs/stat/docs/types/test.ts new file mode 100644 index 000000000000..8902c6ee43b4 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/docs/types/test.ts @@ -0,0 +1,120 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 { Stats } from 'fs'; +import stat = require( './index' ); + +const done = ( error: Error | null, stats: Stats ) => { + if ( error ) { + throw error; + } + if ( stats.isFile() !== true && stats.isFile() !== false ) { + throw new Error( 'unexpected stats object' ); + } +}; + + +// TESTS // + +// The function does not have a return value... +{ + stat( 'beepboop', done ); // $ExpectType void +} + +// The function supports options... +{ + stat( 'beepboop', { 'bigint': false }, done ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument of an unsupported type... +{ + stat( false, done ); // $ExpectError + stat( true, done ); // $ExpectError + stat( null, done ); // $ExpectError + stat( undefined, done ); // $ExpectError + stat( [], done ); // $ExpectError + stat( {}, done ); // $ExpectError + stat( ( x: number ): number => x, done ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not either an options object or callback... +{ + stat( 'beepboop', 123, done ); // $ExpectError + stat( 'beepboop', false, done ); // $ExpectError + stat( 'beepboop', true, done ); // $ExpectError + stat( 'beepboop', null, done ); // $ExpectError + stat( 'beepboop', undefined, done ); // $ExpectError + stat( 'beepboop', [], done ); // $ExpectError + stat( 'beepboop', ( x: number ): number => x, done ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature... +{ + stat( 'beepboop', 1 ); // $ExpectError + stat( 'beepboop', 'abc' ); // $ExpectError + stat( 'beepboop', false ); // $ExpectError + stat( 'beepboop', true ); // $ExpectError + stat( 'beepboop', null ); // $ExpectError + stat( 'beepboop', undefined ); // $ExpectError + stat( 'beepboop', [] ); // $ExpectError + stat( 'beepboop', {} ); // $ExpectError + stat( 'beepboop', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + stat(); // $ExpectError + stat( 'beepboop' ); // $ExpectError +} + +// Attached to main export is a `sync` method which returns either a Stats object or an error... +{ + stat.sync( 'beepboop' ); // $ExpectType Error | Stats +} + +// The `sync` method supports options... +{ + stat.sync( 'beepboop', { 'bigint': false } ); // $ExpectType Error | Stats +} + +// The compiler throws an error if the `sync` method is provided a first argument of an unsupported type... +{ + stat.sync( false ); // $ExpectError + stat.sync( true ); // $ExpectError + stat.sync( null ); // $ExpectError + stat.sync( undefined ); // $ExpectError + stat.sync( [] ); // $ExpectError + stat.sync( {} ); // $ExpectError + stat.sync( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an options argument which is not an object... +{ + stat.sync( 'beepboop', 123 ); // $ExpectError + stat.sync( 'beepboop', false ); // $ExpectError + stat.sync( 'beepboop', true ); // $ExpectError + stat.sync( 'beepboop', null ); // $ExpectError + stat.sync( 'beepboop', undefined ); // $ExpectError + stat.sync( 'beepboop', [] ); // $ExpectError + stat.sync( 'beepboop', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... +{ + stat.sync(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/fs/stat/docs/usage.txt b/lib/node_modules/@stdlib/fs/stat/docs/usage.txt new file mode 100644 index 000000000000..a2eebe5e0db7 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/docs/usage.txt @@ -0,0 +1,7 @@ + +Usage: stat [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. diff --git a/lib/node_modules/@stdlib/fs/stat/etc/cli_opts.json b/lib/node_modules/@stdlib/fs/stat/etc/cli_opts.json new file mode 100644 index 000000000000..f245a17e6317 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/lib/node_modules/@stdlib/fs/stat/examples/index.js b/lib/node_modules/@stdlib/fs/stat/examples/index.js new file mode 100644 index 000000000000..94d19409cc1d --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/examples/index.js @@ -0,0 +1,100 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2018 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 stat = require( './../lib' ); + +/* Sync */ + +var out = stat.sync( __filename ); +if ( out instanceof Error ) { + throw out; +} +console.log( out.isFile() ); +console.log( out.isDirectory() ); +console.log( out.isBlockDevice() ); +console.log( out.isCharacterDevice() ); +console.log( out.isFIFO() ); +console.log( out.isSocket() ); +console.log( out.isSymbolicLink() ); +console.log( out.size ); +console.log( out.mode ); +console.log( out.uid ); +console.log( out.gid ); +console.log( out.atime ); +console.log( out.mtime ); +console.log( out.ctime ); +console.log( out.birthtime ); +console.log( out ); + +out = stat.sync( __dirname ); +if ( out instanceof Error ) { + throw out; +} +console.log( out.isDirectory() ); +console.log( out.isFile() ); +console.log( out.isBlockDevice() ); +console.log( out.isCharacterDevice() ); +console.log( out.isFIFO() ); +console.log( out.isSocket() ); +console.log( out.isSymbolicLink() ); +console.log( out.size ); +console.log( out ); + +/* Async */ + +stat( __filename, onStat ); +stat( __dirname, onStat ); + +function onStat( error, stats ) { + if ( error ) { + throw error; + } + console.log( stats.isFile() ); + console.log( stats.isDirectory() ); + console.log( stats.isBlockDevice() ); + console.log( stats.isCharacterDevice() ); + console.log( stats.isFIFO() ); + console.log( stats.isSocket() ); + console.log( stats.isSymbolicLink() ); + console.log( stats.size ); + console.log( stats.mode ); + console.log( stats.uid ); + console.log( stats.gid ); + console.log( stats.atimeMs ); + console.log( stats.mtimeMs ); + console.log( stats.ctimeMs ); + console.log( stats.birthtimeMs ); + console.log( stats ); +} + +stat( __filename, { + 'bigint': true +}, onBigIntStat ); + +function onBigIntStat( error, stats ) { + if ( error ) { + throw error; + } + console.log( stats.size ); + console.log( stats.atimeNs ); + console.log( stats.mtimeNs ); + console.log( stats.ctimeNs ); + console.log( stats.birthtimeNs ); +} diff --git a/lib/node_modules/@stdlib/fs/stat/lib/async.js b/lib/node_modules/@stdlib/fs/stat/lib/async.js new file mode 100644 index 000000000000..215e541f9d1a --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/lib/async.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 stat = require( 'fs' ).stat; +var isFunction = require( '@stdlib/assert/is-function' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Asynchronously returns full details on the statistics of a file or directory. +* +* @param {(string|Buffer)} path - file path +* @param {Object} [options] - function options (Node >=10.5.0) +* @param {boolean} [options.bigint=false] - whether to return `BigInt` numeric values +* @param {Function} clbk - callback to invoke after retrieving file statistics +* @throws {TypeError} callback argument must be a function +* @throws {TypeError} options argument must be an object +* @throws {TypeError} options are not supported by the current environment +* @returns {(Stats|Error)} file statistics or an error +* +* @example +* stat( __filename, onStat ); +* stat( __dirname, onStat ); +* +* function onStat( error, stats ) { +* if ( error ) { +* throw error; +* } +* console.log( stats.isFile() ); +* console.log( stats.isDirectory() ); +* console.log( stats.isSocket() ); +* console.log( stats.isSymbolicLink() ); +* console.log( stats.size ); +* console.log( stats.mode ); +* console.log( stats.atimeMs ); +* console.log( stats ); +* } +*/ +function statWrapper( path, options, clbk ) { + var nargs; + var cb; + + nargs = arguments.length; + if ( nargs === 2 ) { + cb = options; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + return stat( path, cb ); + } + if ( nargs > 2 ) { + cb = clbk; + if ( !isFunction( cb ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) ); + } + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + // Only pass options if supported by the runtime: + if ( stat.length < 3 ) { + throw new TypeError( 'invalid argument. Options argument is not supported by the current runtime.' ); + } + return stat( path, options, cb ); + } + throw new TypeError( 'invalid invocation. Must provide a file path and a callback.' ); +} + + +// EXPORTS // + +module.exports = statWrapper; diff --git a/lib/node_modules/@stdlib/fs/stat/lib/index.js b/lib/node_modules/@stdlib/fs/stat/lib/index.js new file mode 100644 index 000000000000..4b1de20204be --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/lib/index.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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'; + +/** +* Return full details on the statistics of a file or directory. For more information, see the [archive][1]. +* +* [1]: https://github.com/nodejs/node-v0.x-archive/blob/d8baf8a2a4481940bfed0196308ae6189ca18eee/lib/fs.js#L782 +* +* @module @stdlib/fs/stat +* +* @example +* var stat = require( '@stdlib/fs/stat' ); +* +* stat( __filename, onStat ); +* stat( __dirname, onStat ); +* +* function onStat( error, stats ) { +* if ( error ) { +* throw error; +* } +* console.log( stats.isFile() ); +* console.log( stats.isDirectory() ); +* console.log( stats.isSocket() ); +* console.log( stats.isSymbolicLink() ); +* console.log( stats.size ); +* console.log( stats.mode ); +* console.log( stats.atimeMs ); +* console.log( stats ); +* } +* +* @example +* var stat = require( '@stdlib/fs/stat' ); +* +* var out = stat.sync( __filename ); +* if ( out instanceof Error ) { +* throw out; +* } +* console.log( out.isFile() ); +* console.log( out.isSocket() ); +* console.log( out.size ); +* console.log( out ); +* +* out = stat.sync( __dirname ); +* if ( out instanceof Error ) { +* throw out; +* } +* console.log( out.isDirectory() ); +* console.log( out.isSymbolicLink() ); +* console.log( out.mode ); +* console.log( out ); +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var stat = require( './async.js' ); +var sync = require( './sync.js' ); + + +// MAIN // + +setReadOnly( stat, 'sync', sync ); + + +// EXPORTS // + +module.exports = stat; diff --git a/lib/node_modules/@stdlib/fs/stat/lib/sync.js b/lib/node_modules/@stdlib/fs/stat/lib/sync.js new file mode 100644 index 000000000000..e1249cb2f1fb --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/lib/sync.js @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 stat = require( 'fs' ).statSync; // eslint-disable-line node/no-sync +var isObject = require( '@stdlib/assert/is-plain-object' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Synchronously returns full details on the statistics of a file or directory. +* +* @param {(string|Buffer)} path - file path +* @param {Object} [options] - function options (Node >=10.5.0) +* @param {boolean} [options.bigint=false] - whether to return `BigInt` numeric values +* @throws {TypeError} options argument must be an object +* @throws {TypeError} options are not supported by the current environment +* @returns {(Stats|Error)} file statistics or an error +* +* @example +* var stat = require( '@stdlib/fs/stat' ); +* +* var out = stat.sync( __filename ); +* if ( out instanceof Error ) { +* throw out; +* } +* console.log( out.isFile() ); +* console.log( out.isSocket() ); +* console.log( out.size ); +* console.log( out ); +* +* out = stat.sync( __dirname ); +* if ( out instanceof Error ) { +* throw out; +* } +* console.log( out.isDirectory() ); +* console.log( out.isSymbolicLink() ); +* console.log( out.mode ); +* console.log( out ); +*/ +function statSync( path, options ) { + var nargs; + var out; + + nargs = arguments.length; + try { + if ( nargs === 1 ) { + out = stat( path ); + } else { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + // Only pass options if supported by the runtime: + if ( stat.length < 2 ) { + throw new TypeError( 'invalid argument. Options argument is not supported by the current runtime.' ); + } + out = stat( path, options ); + } + } catch ( err ) { + return err; + } + return out; +} + + +// EXPORTS // + +module.exports = statSync; diff --git a/lib/node_modules/@stdlib/fs/stat/package.json b/lib/node_modules/@stdlib/fs/stat/package.json new file mode 100644 index 000000000000..997a3da89d1a --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/fs/stat", + "version": "0.0.0", + "description": "Get full details on a file or directory.", + "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": { + "stat": "./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", + "stat", + "stats", + "filesystem", + "file", + "directory", + "async", + "sync", + "node" + ] + } diff --git a/lib/node_modules/@stdlib/fs/stat/test/test.async.js b/lib/node_modules/@stdlib/fs/stat/test/test.async.js new file mode 100644 index 000000000000..ca8e9251f72a --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/test/test.async.js @@ -0,0 +1,249 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 proxyquire = require( 'proxyquire' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var stat = require( './../lib/async.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof stat, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns file stats (success)', opts, function test( t ) { + stat( __filename, onStat ); + + function onStat( error, stats ) { + t.strictEqual( error, null, 'error is null' ); + t.strictEqual( typeof stats.isFile, 'function', 'returns a Stats-like object' ); + t.strictEqual( stats.isFile(), true, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function returns directory stats (success)', opts, function test( t ) { + stat( __dirname, onStat ); + + function onStat( error, stats ) { + t.strictEqual( error, null, 'error is null' ); + t.strictEqual( typeof stats.isDirectory, 'function', 'returns a Stats-like object' ); + t.strictEqual( stats.isDirectory(), true, 'returns expected value' ); + t.strictEqual( stats.isFile(), false, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function returns an error (failure)', opts, function test( t ) { + stat( 'beepboopbebop', onStat ); + + function onStat( error ) { + t.ok( error instanceof Error, 'returns an error' ); + t.end(); + } +}); + +tape( 'the function throws if provided an invalid callback', function test( t ) { + t.throws( badValue, TypeError, 'throws a type error' ); + t.end(); + + function badValue() { + stat( __filename, 'beep' ); + } +}); + +tape( 'the function throws if provided options which are not an object', function test( t ) { + t.throws( badValue, TypeError, 'throws a type error' ); + t.end(); + + function badValue() { + stat( __filename, 'beep', function noop() {} ); + } +}); + +tape( 'the function passes options through when supported', function test( t ) { + var called = false; + var stat = proxyquire( './../lib/async.js', { + 'fs': { + 'stat': function stub( path, opts, cb ) { + called = true; + t.strictEqual( path, __filename, 'path argument' ); + t.deepEqual( opts, { + 'bigint': false + }, 'options argument' ); + t.strictEqual( typeof cb, 'function', 'callback argument' ); + cb( null, { + 'isFile': function isFile() { return true; } + }); + } + } + }); + stat( __filename, { + 'bigint': false + }, done ); + + function done( error, stats ) { + t.strictEqual( error, null, 'error is null' ); + t.strictEqual( stats.isFile(), true, 'returns expected value' ); + t.strictEqual( called, true, 'invoked underlying stat' ); + t.end(); + } +}); + +tape( 'the function throws if options are provided and options are unsupported', function test( t ) { + var stat = proxyquire( './../lib/async.js', { + 'fs': { + // If a function only accepts (path, cb), its `.length` will be 2: + 'stat': function stat2( path, cb ) { + return cb( null, {} ); + } + } + }); + + t.throws( badValue, TypeError, 'throws a type error' ); + t.end(); + + function badValue() { + stat( __filename, { + 'bigint': false + }, function noop() {} ); + } +}); + +tape( 'the function returns file stats methods', opts, function test( t ) { + stat( __filename, onStat ); + + function onStat( error, stats ) { + t.strictEqual( error, null, 'error is null' ); + t.strictEqual( typeof stats.isBlockDevice, 'function', 'has expected method' ); + t.strictEqual( typeof stats.isCharacterDevice, 'function', 'has expected method' ); + t.strictEqual( typeof stats.isFIFO, 'function', 'has expected method' ); + t.strictEqual( typeof stats.isSocket, 'function', 'has expected method' ); + t.strictEqual( typeof stats.isSymbolicLink, 'function', 'has expected method' ); + t.strictEqual( stats.isBlockDevice(), false, 'returns expected value' ); + t.strictEqual( stats.isCharacterDevice(), false, 'returns expected value' ); + t.strictEqual( stats.isFIFO(), false, 'returns expected value' ); + t.strictEqual( stats.isSocket(), false, 'returns expected value' ); + t.strictEqual( stats.isSymbolicLink(), false, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function returns file stats properties', opts, function test( t ) { + stat( __filename, onStat ); + + function onStat( error, stats ) { + t.strictEqual( error, null, 'error is null' ); + t.strictEqual( typeof stats.dev, 'number', 'has expected property' ); + t.strictEqual( typeof stats.ino, 'number', 'has expected property' ); + t.strictEqual( typeof stats.mode, 'number', 'has expected property' ); + t.strictEqual( typeof stats.nlink, 'number', 'has expected property' ); + t.strictEqual( typeof stats.uid, 'number', 'has expected property' ); + t.strictEqual( typeof stats.gid, 'number', 'has expected property' ); + t.strictEqual( typeof stats.rdev, 'number', 'has expected property' ); + t.strictEqual( typeof stats.size, 'number', 'has expected property' ); + t.strictEqual( typeof stats.blksize, 'number', 'has expected property' ); + t.strictEqual( typeof stats.blocks, 'number', 'has expected property' ); + t.ok( stats.atime instanceof Date, 'has expected property' ); + t.ok( stats.mtime instanceof Date, 'has expected property' ); + t.ok( stats.ctime instanceof Date, 'has expected property' ); + t.ok( stats.birthtime instanceof Date, 'has expected property' ); + t.strictEqual( typeof stats.atimeMs, 'number', 'has expected property' ); + t.strictEqual( typeof stats.mtimeMs, 'number', 'has expected property' ); + t.strictEqual( typeof stats.ctimeMs, 'number', 'has expected property' ); + t.strictEqual( typeof stats.birthtimeMs, 'number', 'has expected property' ); + t.end(); + } +}); + +tape( 'the function supports the bigint option', opts, function test( t ) { + var stat = proxyquire( './../lib/async.js', { + 'fs': { + 'stat': function stub( path, opts, cb ) { + t.strictEqual( path, __filename, 'path argument' ); + t.deepEqual( opts, { + 'bigint': true + }, 'options argument' ); + cb( null, { + 'dev': BigInt( '1' ), + 'ino': BigInt( '2' ), + 'mode': BigInt( '3' ), + 'nlink': BigInt( '4' ), + 'uid': BigInt( '5' ), + 'gid': BigInt( '6' ), + 'rdev': BigInt( '7' ), + 'size': BigInt( '8' ), + 'blksize': BigInt( '9' ), + 'blocks': BigInt( '10' ), + 'atimeMs': BigInt( '11' ), + 'mtimeMs': BigInt( '12' ), + 'ctimeMs': BigInt( '13' ), + 'birthtimeMs': BigInt( '14' ), + 'atimeNs': BigInt( '15' ), + 'mtimeNs': BigInt( '16' ), + 'ctimeNs': BigInt( '17' ), + 'birthtimeNs': BigInt( '18' ) + }); + } + } + }); + + stat( __filename, { + 'bigint': true + }, onStat ); + + function onStat( error, stats ) { + t.strictEqual( error, null, 'error is null' ); + t.strictEqual( typeof stats.dev, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.ino, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.mode, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.nlink, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.uid, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.gid, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.rdev, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.size, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.blksize, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.blocks, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.atimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.mtimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.ctimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.birthtimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.atimeNs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.mtimeNs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.ctimeNs, 'bigint', 'has expected property' ); + t.strictEqual( typeof stats.birthtimeNs, 'bigint', 'has expected property' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/fs/stat/test/test.cli.js b/lib/node_modules/@stdlib/fs/stat/test/test.cli.js new file mode 100644 index 000000000000..291af12292e7 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/test/test.cli.js @@ -0,0 +1,183 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 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 readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// 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 prints file statistics as JSON', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + __filename + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var out; + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + out = JSON.parse( stdout.toString() ); + t.strictEqual( typeof out.size, 'number', 'contains expected fields' ); + t.strictEqual( typeof out.mode, 'number', 'contains expected fields' ); + t.strictEqual( typeof out.mtimeMs, 'number', 'contains expected fields' ); + } + t.end(); + } +}); + +tape( 'when provided a non-existent path, the command-line interface prints an error to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + 'beepboopbebop' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + t.ok( error, 'returns an error' ); + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.ok( stderr.toString().length > 0, 'prints to `stderr`' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/fs/stat/test/test.js b/lib/node_modules/@stdlib/fs/stat/test/test.js new file mode 100644 index 000000000000..a0a9d54e139f --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/test/test.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 stat = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof stat, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'export includes a function to stat synchronously', function test( t ) { + t.strictEqual( typeof stat.sync, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached `sync` property is non-enumerable and read-only', function test( t ) { + var desc = Object.getOwnPropertyDescriptor( stat, 'sync' ); + t.strictEqual( typeof desc, 'object', 'returns a descriptor' ); + t.strictEqual( desc.enumerable, false, 'non-enumerable' ); + t.strictEqual( desc.configurable, false, 'non-configurable' ); + t.strictEqual( desc.writable, false, 'read-only' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/fs/stat/test/test.sync.js b/lib/node_modules/@stdlib/fs/stat/test/test.sync.js new file mode 100644 index 000000000000..f91cc16199f4 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/stat/test/test.sync.js @@ -0,0 +1,217 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 proxyquire = require( 'proxyquire' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var stat = require( './../lib/sync.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof stat, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns file stats (success)', opts, function test( t ) { + var out = stat( __filename ); + t.strictEqual( out instanceof Error, false, 'does not return an error' ); + t.strictEqual( typeof out.isFile, 'function', 'returns a Stats-like object' ); + t.strictEqual( out.isFile(), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns directory stats (success)', opts, function test( t ) { + var out = stat( __dirname ); + t.strictEqual( out instanceof Error, false, 'does not return an error' ); + t.strictEqual( typeof out.isDirectory, 'function', 'returns a Stats-like object' ); + t.strictEqual( out.isDirectory(), true, 'returns expected value' ); + t.strictEqual( out.isFile(), false, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns an error (failure)', opts, function test( t ) { + var out = stat( 'beepboopbebop' ); + t.strictEqual( out instanceof Error, true, 'returns an error' ); + t.end(); +}); + +tape( 'the function returns an error if provided options which are not an object', function test( t ) { + var out = stat( __filename, 'beep' ); + t.strictEqual( out instanceof Error, true, 'returns an error' ); + t.strictEqual( out.name, 'TypeError', 'returns a type error' ); + t.end(); +}); + +tape( 'the function passes options through when supported', function test( t ) { + var called = false; + var stat = proxyquire( './../lib/sync.js', { + 'fs': { + 'statSync': function stub( path, opts ) { + called = true; + t.strictEqual( path, __filename, 'path argument' ); + t.deepEqual( opts, { + 'bigint': false + }, 'options argument' ); + return { + 'isFile': function isFile() { + return true; + } + }; + } + } + }); + + var out = stat( __filename, { + 'bigint': false + }); + t.strictEqual( out instanceof Error, false, 'does not return an error' ); + t.strictEqual( out.isFile(), true, 'returns expected value' ); + t.strictEqual( called, true, 'invoked underlying statSync' ); + t.end(); +}); + +tape( 'the function returns an error if options are provided and options are unsupported', function test( t ) { + var stat = proxyquire( './../lib/sync.js', { + 'fs': { + // If a function only accepts (path), its `.length` will be 1: + 'statSync': function stat1( path ) { // eslint-disable-line no-unused-vars + return {}; + } + } + }); + + var out = stat( __filename, { + 'bigint': false + }); + t.strictEqual( out instanceof Error, true, 'returns an error' ); + t.strictEqual( out.name, 'TypeError', 'returns a type error' ); + t.end(); +}); + +tape( 'the function returns file stats methods', opts, function test( t ) { + var out = stat( __filename ); + t.strictEqual( out instanceof Error, false, 'does not return an error' ); + t.strictEqual( typeof out.isBlockDevice, 'function', 'has expected method' ); + t.strictEqual( typeof out.isCharacterDevice, 'function', 'has expected method' ); + t.strictEqual( typeof out.isFIFO, 'function', 'has expected method' ); + t.strictEqual( typeof out.isSocket, 'function', 'has expected method' ); + t.strictEqual( typeof out.isSymbolicLink, 'function', 'has expected method' ); + t.strictEqual( out.isBlockDevice(), false, 'returns expected value' ); + t.strictEqual( out.isCharacterDevice(), false, 'returns expected value' ); + t.strictEqual( out.isFIFO(), false, 'returns expected value' ); + t.strictEqual( out.isSocket(), false, 'returns expected value' ); + t.strictEqual( out.isSymbolicLink(), false, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns file stats properties', opts, function test( t ) { + var out = stat( __filename ); + t.strictEqual( out instanceof Error, false, 'does not return an error' ); + t.strictEqual( typeof out.dev, 'number', 'has expected property' ); + t.strictEqual( typeof out.ino, 'number', 'has expected property' ); + t.strictEqual( typeof out.mode, 'number', 'has expected property' ); + t.strictEqual( typeof out.nlink, 'number', 'has expected property' ); + t.strictEqual( typeof out.uid, 'number', 'has expected property' ); + t.strictEqual( typeof out.gid, 'number', 'has expected property' ); + t.strictEqual( typeof out.rdev, 'number', 'has expected property' ); + t.strictEqual( typeof out.size, 'number', 'has expected property' ); + t.strictEqual( typeof out.blksize, 'number', 'has expected property' ); + t.strictEqual( typeof out.blocks, 'number', 'has expected property' ); + t.ok( out.atime instanceof Date, 'has expected property' ); + t.ok( out.mtime instanceof Date, 'has expected property' ); + t.ok( out.ctime instanceof Date, 'has expected property' ); + t.ok( out.birthtime instanceof Date, 'has expected property' ); + t.strictEqual( typeof out.atimeMs, 'number', 'has expected property' ); + t.strictEqual( typeof out.mtimeMs, 'number', 'has expected property' ); + t.strictEqual( typeof out.ctimeMs, 'number', 'has expected property' ); + t.strictEqual( typeof out.birthtimeMs, 'number', 'has expected property' ); + t.end(); +}); + +tape( 'the function supports the bigint option', opts, function test( t ) { + var stat = proxyquire( './../lib/sync.js', { + 'fs': { + 'statSync': function stub( path, opts ) { + t.strictEqual( path, __filename, 'path argument' ); + t.deepEqual( opts, { + 'bigint': true + }, 'options argument' ); + return { + 'dev': BigInt( '1' ), + 'ino': BigInt( '2' ), + 'mode': BigInt( '3' ), + 'nlink': BigInt( '4' ), + 'uid': BigInt( '5' ), + 'gid': BigInt( '6' ), + 'rdev': BigInt( '7' ), + 'size': BigInt( '8' ), + 'blksize': BigInt( '9' ), + 'blocks': BigInt( '10' ), + 'atimeMs': BigInt( '11' ), + 'mtimeMs': BigInt( '12' ), + 'ctimeMs': BigInt( '13' ), + 'birthtimeMs': BigInt( '14' ), + 'atimeNs': BigInt( '15' ), + 'mtimeNs': BigInt( '16' ), + 'ctimeNs': BigInt( '17' ), + 'birthtimeNs': BigInt( '18' ) + }; + } + } + }); + + var out = stat( __filename, { + 'bigint': true + }); + t.strictEqual( out instanceof Error, false, 'does not return an error' ); + t.strictEqual( typeof out.dev, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.ino, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.mode, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.nlink, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.uid, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.gid, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.rdev, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.size, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.blksize, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.blocks, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.atimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.mtimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.ctimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.birthtimeMs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.atimeNs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.mtimeNs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.ctimeNs, 'bigint', 'has expected property' ); + t.strictEqual( typeof out.birthtimeNs, 'bigint', 'has expected property' ); + t.end(); +}); From 1cfc50b92023cc83b1ff1ac4106b5565b45381cd Mon Sep 17 00:00:00 2001 From: blessed Date: Sun, 22 Mar 2026 12:23:17 +0100 Subject: [PATCH 2/4] fixup! feat: add fs/stat corrections: Removed changes from @stdlib/fs/README.md, @stdlib/fs/docs/types/index.d.ts, @stdlib/fs/stat/lib/index.js files proper indentation on the @stdlib/fs/stat/package.json file consistent indentation using 2 spaces on the @stdlib/fs/stat/docs/types/index.d.ts file removed see also section from the @stdlib/fs/stat/README.md file remove check for runtime support of arg and let let the runtime handle the error. --- lib/node_modules/@stdlib/fs/README.md | 3 - .../@stdlib/fs/docs/types/index.d.ts | 39 ------ lib/node_modules/@stdlib/fs/lib/index.js | 9 -- lib/node_modules/@stdlib/fs/stat/README.md | 23 +--- .../@stdlib/fs/stat/docs/types/index.d.ts | 12 +- lib/node_modules/@stdlib/fs/stat/lib/async.js | 6 +- lib/node_modules/@stdlib/fs/stat/package.json | 128 +++++++++--------- 7 files changed, 77 insertions(+), 143 deletions(-) diff --git a/lib/node_modules/@stdlib/fs/README.md b/lib/node_modules/@stdlib/fs/README.md index 002736e1e284..7c0bd618462f 100644 --- a/lib/node_modules/@stdlib/fs/README.md +++ b/lib/node_modules/@stdlib/fs/README.md @@ -57,7 +57,6 @@ var f = fs; - [`resolveParentPathBy( path[, options], predicate, clbk )`][@stdlib/fs/resolve-parent-path-by]: resolve a path according to a predicate function by walking parent directories. - [`resolveParentPath( path[, options], clbk )`][@stdlib/fs/resolve-parent-path]: resolve a path by walking parent directories. - [`resolveParentPaths( paths[, options], clbk )`][@stdlib/fs/resolve-parent-paths]: resolve paths from a set of paths by walking parent directories. -- [`stat( path[, options], clbk )`][@stdlib/fs/stat]: show full details on the statistics of a file or directory. - [`unlink( path, clbk )`][@stdlib/fs/unlink]: remove a directory entry. - [`writeFile( file, data[, options], clbk )`][@stdlib/fs/write-file]: write data to a file. @@ -130,8 +129,6 @@ console.log( objectKeys( fs ) ); [@stdlib/fs/resolve-parent-paths]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/resolve-parent-paths -[@stdlib/fs/stat]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/stat - [@stdlib/fs/unlink]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/unlink [@stdlib/fs/write-file]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/write-file diff --git a/lib/node_modules/@stdlib/fs/docs/types/index.d.ts b/lib/node_modules/@stdlib/fs/docs/types/index.d.ts index 445975b763bf..7f82739b4ba0 100644 --- a/lib/node_modules/@stdlib/fs/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/fs/docs/types/index.d.ts @@ -34,7 +34,6 @@ import rename = require( '@stdlib/fs/rename' ); import resolveParentPath = require( '@stdlib/fs/resolve-parent-path' ); import resolveParentPathBy = require( '@stdlib/fs/resolve-parent-path-by' ); import resolveParentPaths = require( '@stdlib/fs/resolve-parent-paths' ); -import stat = require( '@stdlib/fs/stat' ); import unlink = require( '@stdlib/fs/unlink' ); import writeFile = require( '@stdlib/fs/write-file' ); @@ -450,44 +449,6 @@ interface Namespace { */ resolveParentPaths: typeof resolveParentPaths; - /** - * Asynchronously returns file system statistics for a file or directory. - * - * @param path - file path - * @param clbk - callback to invoke after retrieving file statistics - * - * @example - * var stat = require( '@stdlib/fs/stat' ); - * - * stat( __filename, onStat ); - * stat( __dirname, onStat ); - * - * function onStat( error, stats ) { - * if ( error ) { - * throw error; - * } - * console.log( stats.isFile() ); - * console.log( stats.isDirectory() ); - * console.log( stats ); - * } - * - * @example - * var out = stat.sync( __filename ); - * if ( out instanceof Error ) { - * throw out; - * } - * console.log( out.isFile() ); - * console.log( out ); - * - * out = stat.sync( __dirname ); - * if ( out instanceof Error ) { - * throw out; - * } - * console.log( out.isDirectory() ); - * console.log( out ); - */ - stat: typeof stat; - /** * Asynchronously removes a directory entry. * diff --git a/lib/node_modules/@stdlib/fs/lib/index.js b/lib/node_modules/@stdlib/fs/lib/index.js index 64aec56c6cca..b17416a1ea67 100644 --- a/lib/node_modules/@stdlib/fs/lib/index.js +++ b/lib/node_modules/@stdlib/fs/lib/index.js @@ -162,15 +162,6 @@ setReadOnly( fs, 'resolveParentPathBy', require( '@stdlib/fs/resolve-parent-path */ setReadOnly( fs, 'resolveParentPaths', require( '@stdlib/fs/resolve-parent-paths' ) ); -/** -* @name stat -* @memberof fs -* @readonly -* @type {Function} -* @see {@link module:@stdlib/fs/stat} -*/ -setReadOnly( fs, 'stat', require( '@stdlib/fs/stat' ) ); - /** * @name unlink * @memberof fs diff --git a/lib/node_modules/@stdlib/fs/stat/README.md b/lib/node_modules/@stdlib/fs/stat/README.md index dedc210b04fa..6b945deadc8a 100644 --- a/lib/node_modules/@stdlib/fs/stat/README.md +++ b/lib/node_modules/@stdlib/fs/stat/README.md @@ -34,7 +34,7 @@ var stat = require( '@stdlib/fs/stat' ); Asynchronously returns file system statistics for a file or directory. -The callback is invoked with an `fs.Stats` object containing methods and values describing the target path. +The callback is invoked with an [`fs.Stats`][node-fs-stats] object containing methods and values describing the target path. ```javascript stat( __filename, onStat ); @@ -72,7 +72,7 @@ function onStat( error, stats ) { Options: -- **bigint**: `boolean` flag indicating whether numeric values should be returned as `bigint`. Default: `false`. +- **bigint**: `boolean` flag indicating whether numeric values should be returned as [`bigint`][@stdlib/bigint/ctor]. Default: `false`. ```javascript stat( __filename, onStat ); @@ -125,7 +125,7 @@ The returned `fs.Stats` instance provides these type-check methods: - `isSocket()` - `isSymbolicLink()` -The returned `fs.Stats` instance also provides values such as: +The returned `fs.Stats` instance also provides values such as [`fs.Stats`][node-fs-stats]: - `dev`, `ino`, `mode`, `nlink`, `uid`, `gid`, `rdev`, `size`, `blksize`, `blocks` - `atime`, `mtime`, `ctime`, `birthtime` @@ -228,26 +228,13 @@ Options: - - - -