|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2018 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# Stat |
| 22 | + |
| 23 | +> Get full details on a file or directory. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var stat = require( '@stdlib/fs/stat' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### stat( path, clbk ) |
| 34 | + |
| 35 | +Asynchronously returns file system statistics for a file or directory. |
| 36 | + |
| 37 | +The callback is invoked with an `fs.Stats` object containing methods and values describing the target path. |
| 38 | + |
| 39 | +```javascript |
| 40 | +stat( __filename, onStat ); |
| 41 | + |
| 42 | +function onStat( error, stats ) { |
| 43 | + if ( error ) { |
| 44 | + throw error; |
| 45 | + } |
| 46 | + console.log( stats.isFile() ); |
| 47 | + console.log( stats.isDirectory() ); |
| 48 | + console.log( stats.isSymbolicLink() ); |
| 49 | + console.log( stats.size ); |
| 50 | + console.log( stats.mode ); |
| 51 | + console.log( stats.atime ); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +#### stat( path, options, clbk ) |
| 56 | + |
| 57 | +Asynchronously returns file system statistics for a file or directory with options. |
| 58 | + |
| 59 | +```javascript |
| 60 | +stat( __filename, { |
| 61 | + 'bigint': true |
| 62 | +}, onStat ); |
| 63 | + |
| 64 | +function onStat( error, stats ) { |
| 65 | + if ( error ) { |
| 66 | + throw error; |
| 67 | + } |
| 68 | + console.log( stats.size ); |
| 69 | + console.log( stats.atimeNs ); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Options: |
| 74 | + |
| 75 | +- **bigint**: `boolean` flag indicating whether numeric values should be returned as `bigint`. Default: `false`. |
| 76 | + |
| 77 | +```javascript |
| 78 | +stat( __filename, onStat ); |
| 79 | + |
| 80 | +function onStat( error, stats ) { |
| 81 | + if ( error ) { |
| 82 | + throw error; |
| 83 | + } |
| 84 | + console.log( stats.isFile() ); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +#### stat.sync( path ) |
| 89 | + |
| 90 | +Synchronously returns file system statistics for a file or directory. |
| 91 | + |
| 92 | +```javascript |
| 93 | +var out = stat.sync( __filename ); |
| 94 | +if ( out instanceof Error ) { |
| 95 | + throw out; |
| 96 | +} |
| 97 | +console.log( out.isFile() ); |
| 98 | +console.log( out.isSocket() ); |
| 99 | +console.log( out.size ); |
| 100 | +console.log( out.mtimeMs ); |
| 101 | +``` |
| 102 | + |
| 103 | +#### stat.sync( path, options ) |
| 104 | + |
| 105 | +Synchronously returns file system statistics for a file or directory with options. |
| 106 | + |
| 107 | +```javascript |
| 108 | +var out = stat.sync( __filename, { |
| 109 | + 'bigint': true |
| 110 | +}); |
| 111 | +if ( out instanceof Error ) { |
| 112 | + throw out; |
| 113 | +} |
| 114 | +console.log( out.size ); |
| 115 | +console.log( out.birthtimeNs ); |
| 116 | +``` |
| 117 | + |
| 118 | +The returned `fs.Stats` instance provides these type-check methods: |
| 119 | + |
| 120 | +- `isFile()` |
| 121 | +- `isDirectory()` |
| 122 | +- `isBlockDevice()` |
| 123 | +- `isCharacterDevice()` |
| 124 | +- `isFIFO()` |
| 125 | +- `isSocket()` |
| 126 | +- `isSymbolicLink()` |
| 127 | + |
| 128 | +The returned `fs.Stats` instance also provides values such as: |
| 129 | + |
| 130 | +- `dev`, `ino`, `mode`, `nlink`, `uid`, `gid`, `rdev`, `size`, `blksize`, `blocks` |
| 131 | +- `atime`, `mtime`, `ctime`, `birthtime` |
| 132 | +- `atimeMs`, `mtimeMs`, `ctimeMs`, `birthtimeMs` |
| 133 | +- `atimeNs`, `mtimeNs`, `ctimeNs`, `birthtimeNs` (available when `bigint: true`) |
| 134 | + |
| 135 | +```javascript |
| 136 | +var out = stat.sync( __filename ); |
| 137 | +if ( out instanceof Error ) { |
| 138 | + throw out; |
| 139 | +} |
| 140 | +console.log( out.isFile() ); |
| 141 | +``` |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.usage --> |
| 146 | + |
| 147 | +<section class="examples"> |
| 148 | + |
| 149 | +## Examples |
| 150 | + |
| 151 | +<!-- eslint no-undef: "error" --> |
| 152 | + |
| 153 | +```javascript |
| 154 | +var stat = require( '@stdlib/fs/stat' ); |
| 155 | + |
| 156 | +var out = stat.sync( __filename ); |
| 157 | +if ( out instanceof Error ) { |
| 158 | + throw out; |
| 159 | +} |
| 160 | +console.log( out.isFile() ); |
| 161 | + |
| 162 | +stat( __filename, onStat ); |
| 163 | + |
| 164 | +function onStat( error, stats ) { |
| 165 | + if ( error ) { |
| 166 | + throw error; |
| 167 | + } |
| 168 | + console.log( stats.isFile() ); |
| 169 | + console.log( stats.isDirectory() ); |
| 170 | + console.log( stats.isBlockDevice() ); |
| 171 | + console.log( stats.isCharacterDevice() ); |
| 172 | + console.log( stats.isFIFO() ); |
| 173 | + console.log( stats.isSocket() ); |
| 174 | + console.log( stats.isSymbolicLink() ); |
| 175 | + console.log( stats.size ); |
| 176 | + console.log( stats.mode ); |
| 177 | + console.log( stats.uid ); |
| 178 | + console.log( stats.gid ); |
| 179 | + console.log( stats.atimeMs ); |
| 180 | + console.log( stats.mtimeMs ); |
| 181 | + console.log( stats.ctimeMs ); |
| 182 | + console.log( stats.birthtimeMs ); |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +</section> |
| 187 | + |
| 188 | +<!-- /.examples --> |
| 189 | + |
| 190 | +* * * |
| 191 | + |
| 192 | +<section class="cli"> |
| 193 | + |
| 194 | +## CLI |
| 195 | + |
| 196 | +<section class="usage"> |
| 197 | + |
| 198 | +### Usage |
| 199 | + |
| 200 | +```text |
| 201 | +Usage: stat [options] <path> |
| 202 | +
|
| 203 | +Options: |
| 204 | +
|
| 205 | + -h, --help Print this message. |
| 206 | + -V, --version Print the package version. |
| 207 | +``` |
| 208 | + |
| 209 | +</section> |
| 210 | + |
| 211 | +<!-- /.usage --> |
| 212 | + |
| 213 | +<section class="notes"> |
| 214 | + |
| 215 | +### Notes |
| 216 | + |
| 217 | +- Relative paths are resolved relative to the current working directory. |
| 218 | +- Errors are written to `stderr`. |
| 219 | +- Results are written to `stdout` as JSON. |
| 220 | + |
| 221 | +</section> |
| 222 | + |
| 223 | +<!-- /.notes --> |
| 224 | + |
| 225 | +</section> |
| 226 | + |
| 227 | +<!-- /.cli --> |
| 228 | + |
| 229 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 230 | + |
| 231 | +<section class="related"> |
| 232 | + |
| 233 | +* * * |
| 234 | + |
| 235 | +## See Also |
| 236 | + |
| 237 | +- <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> |
| 238 | +- <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> |
| 239 | + |
| 240 | +</section> |
| 241 | + |
| 242 | +<!-- /.related --> |
| 243 | + |
| 244 | +<section class="links"> |
| 245 | + |
| 246 | +[node-fs-stats]: https://nodejs.org/api/fs.html#class-fsstats |
| 247 | + |
| 248 | +<!-- <related-links> --> |
| 249 | + |
| 250 | +[@stdlib/fs/read-file]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/read-file |
| 251 | + |
| 252 | +<!-- </related-links> --> |
| 253 | + |
| 254 | +</section> |
| 255 | + |
| 256 | +<!-- /.links --> |
0 commit comments