Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions lib/node_modules/@stdlib/fs/stat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<!--

@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.

-->

# Stat

> Get full details on a file or directory.

<section class="usage">

## 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`][node-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`][@stdlib/bigint/ctor]. 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 [`fs.Stats`][node-fs-stats]:

- `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() );
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 );
}
```

</section>

<!-- /.examples -->

* * *

<section class="cli">

## CLI

<section class="usage">

### Usage

```text
Usage: stat [options] <path>

Options:

-h, --help Print this message.
-V, --version Print the package version.
```

</section>

<!-- /.usage -->

<section class="notes">

### Notes

- Relative paths are resolved relative to the current working directory.
- Errors are written to `stderr`.
- Results are written to `stdout` as JSON.

</section>

<!-- /.notes -->

</section>

<!-- /.cli -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="links">

[node-fs-stats]: https://nodejs.org/api/fs.html#class-fsstats

[@stdlib/bigint/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/bigint/ctor

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
44 changes: 44 additions & 0 deletions lib/node_modules/@stdlib/fs/stat/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @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.
*/

'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();
});
84 changes: 84 additions & 0 deletions lib/node_modules/@stdlib/fs/stat/bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env node

/**
* @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.
*/

'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();
Loading
Loading