-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add fs/stat #11069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
feat: add fs/stat #11069
Changes from 1 commit
c5afb00
1cfc50b
e7d3980
e7199ec
d469d00
74b7f73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not modify this file. |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should not modify this file. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,256 @@ | ||||||||||||||||
| <!-- | ||||||||||||||||
|
|
||||||||||||||||
| @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. | ||||||||||||||||
|
|
||||||||||||||||
| --> | ||||||||||||||||
|
|
||||||||||||||||
| # 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` 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() ); | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| </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="related"> | ||||||||||||||||
|
|
||||||||||||||||
| * * * | ||||||||||||||||
|
|
||||||||||||||||
| ## See Also | ||||||||||||||||
|
|
||||||||||||||||
| - <span class="package-name">[`@stdlib/fs/read-file`][@stdlib/fs/read-file]</span><span class="delimiter">: </span><span class="description">read the entire contents of a file.</span> | ||||||||||||||||
| - <span class="package-name">[`node-fs-stats`][node-fs-stats]</span><span class="delimiter">: </span><span class="description">Official Node.js fs.Stats class.</span> | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You should not include content in this section. |
||||||||||||||||
|
|
||||||||||||||||
| </section> | ||||||||||||||||
|
|
||||||||||||||||
| <!-- /.related --> | ||||||||||||||||
|
|
||||||||||||||||
| <section class="links"> | ||||||||||||||||
|
|
||||||||||||||||
| [node-fs-stats]: https://nodejs.org/api/fs.html#class-fsstats | ||||||||||||||||
|
|
||||||||||||||||
| <!-- <related-links> --> | ||||||||||||||||
|
|
||||||||||||||||
| [@stdlib/fs/read-file]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/read-file | ||||||||||||||||
|
|
||||||||||||||||
| <!-- </related-links> --> | ||||||||||||||||
|
|
||||||||||||||||
| </section> | ||||||||||||||||
|
|
||||||||||||||||
| <!-- /.links --> | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not edit this file.