1 | # @nodelib/fs.scandir
|
2 |
|
3 | > List files and directories inside the specified directory.
|
4 |
|
5 | ## :bulb: Highlights
|
6 |
|
7 | The package is aimed at obtaining information about entries in the directory.
|
8 |
|
9 | * :moneybag: Returns useful information: `name`, `path`, `dirent` and `stats` (optional).
|
10 | * :link: Can safely work with broken symbolic links.
|
11 |
|
12 | ## Install
|
13 |
|
14 | ```console
|
15 | npm install @nodelib/fs.scandir
|
16 | ```
|
17 |
|
18 | ## Usage
|
19 |
|
20 | ```ts
|
21 | import * as fsScandir from '@nodelib/fs.scandir';
|
22 |
|
23 | fsScandir.scandir('path', (error, stats) => { /* … */ });
|
24 | ```
|
25 |
|
26 | Also available with the promise interface:
|
27 |
|
28 | ```ts
|
29 | import * as fsScandir from '@nodelib/fs.scandir/promises';
|
30 |
|
31 | await fsScandir.scandir('path');
|
32 | ```
|
33 |
|
34 | ## API
|
35 |
|
36 | ### .scandir(path, [optionsOrSettings], callback)
|
37 |
|
38 | Returns an array of plain objects ([`Entry`](#entry)) with information about entry for provided path with standard callback-style.
|
39 |
|
40 | > :book: If you want to use the Promise API, import `@nodelib/fs.scandir/promises` or use `util.promisify`.
|
41 |
|
42 | ```ts
|
43 | fsScandir.scandir('path', (error, entries) => { /* … */ });
|
44 | fsScandir.scandir('path', {}, (error, entries) => { /* … */ });
|
45 | fsScandir.scandir('path', new fsScandir.Settings(), (error, entries) => { /* … */ });
|
46 | ```
|
47 |
|
48 | ### .scandirSync(path, [optionsOrSettings])
|
49 |
|
50 | Returns an array of plain objects ([`Entry`](#entry)) with information about entry for provided path.
|
51 |
|
52 | ```ts
|
53 | const entries = fsScandir.scandirSync('path');
|
54 | const entries = fsScandir.scandirSync('path', {});
|
55 | const entries = fsScandir.scandirSync('path', new fsScandir.Settings());
|
56 | ```
|
57 |
|
58 | #### path
|
59 |
|
60 | * Required: `true`
|
61 | * Type: `string | Buffer | URL`
|
62 |
|
63 | A path to a file. If a URL is provided, it must use the `file:` protocol.
|
64 |
|
65 | #### optionsOrSettings
|
66 |
|
67 | * Required: `false`
|
68 | * Type: `Options | Settings`
|
69 | * Default: An instance of `Settings` class
|
70 |
|
71 | An [`Options`](#options) object or an instance of [`Settings`](#settingsoptions) class.
|
72 |
|
73 | > :book: When you pass a plain object, an instance of the `Settings` class will be created automatically. If you plan to call the method frequently, use a pre-created instance of the `Settings` class.
|
74 |
|
75 | ### Settings([options])
|
76 |
|
77 | A class of full settings of the package.
|
78 |
|
79 | ```ts
|
80 | const settings = new fsScandir.Settings({ followSymbolicLinks: false });
|
81 |
|
82 | const entries = fsScandir.scandirSync('path', settings);
|
83 | ```
|
84 |
|
85 | ## Entry
|
86 |
|
87 | * `name` — The name of the entry (`unknown.txt`).
|
88 | * `path` — The path of the entry relative to call directory (`root/unknown.txt`).
|
89 | * `dirent` — An instance of [`fs.Dirent`](./src/types/index.ts) class. When the `stats` option is enabled, it will be emulated by [`DirentFromStats`](./src/utils/fs.ts) class.
|
90 | * `stats` (optional) — An instance of `fs.Stats` class.
|
91 |
|
92 | For example, the `scandir` call for `tools` directory with one directory inside:
|
93 |
|
94 | ```ts
|
95 | {
|
96 | dirent: Dirent { name: 'typedoc', /* … */ },
|
97 | name: 'typedoc',
|
98 | path: 'tools/typedoc'
|
99 | }
|
100 | ```
|
101 |
|
102 | ## Options
|
103 |
|
104 | ### stats
|
105 |
|
106 | * Type: `boolean`
|
107 | * Default: `false`
|
108 |
|
109 | Adds an instance of `fs.Stats` class to the [`Entry`](#entry).
|
110 |
|
111 | > :book: Always use `fs.readdir` without the `withFileTypes` option. ??TODO??
|
112 |
|
113 | ### followSymbolicLinks
|
114 |
|
115 | * Type: `boolean`
|
116 | * Default: `false`
|
117 |
|
118 | Follow symbolic links or not. Call `fs.stat` on symbolic link if `true`.
|
119 |
|
120 | ### `throwErrorOnBrokenSymbolicLink`
|
121 |
|
122 | * Type: `boolean`
|
123 | * Default: `true`
|
124 |
|
125 | Throw an error when symbolic link is broken if `true` or safely use `lstat` call if `false`.
|
126 |
|
127 | ### `pathSegmentSeparator`
|
128 |
|
129 | * Type: `string`
|
130 | * Default: `path.sep`
|
131 |
|
132 | By default, this package uses the correct path separator for your OS (`\` on Windows, `/` on Unix-like systems). But you can set this option to any separator character(s) that you want to use instead.
|
133 |
|
134 | ### `fs`
|
135 |
|
136 | * Type: [`FileSystemAdapter`](./src/adapters/fs.ts)
|
137 | * Default: A default FS methods
|
138 |
|
139 | By default, the built-in Node.js module (`fs`) is used to work with the file system. You can replace any method with your own.
|
140 |
|
141 | ```ts
|
142 | interface FileSystemAdapter {
|
143 | lstat?: typeof fs.lstat;
|
144 | stat?: typeof fs.stat;
|
145 | lstatSync?: typeof fs.lstatSync;
|
146 | statSync?: typeof fs.statSync;
|
147 | readdir?: typeof fs.readdir;
|
148 | readdirSync?: typeof fs.readdirSync;
|
149 | }
|
150 |
|
151 | const settings = new fsScandir.Settings({
|
152 | fs: { lstat: fakeLstat }
|
153 | });
|
154 | ```
|
155 |
|
156 | ## Changelog
|
157 |
|
158 | See the [Releases section of our GitHub project](https://github.com/nodelib/nodelib/releases) for changelog for each release version.
|
159 |
|
160 | ## License
|
161 |
|
162 | This software is released under the terms of the MIT license.
|