pwd-fs 3.5.0 → 3.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitmask.d.ts +10 -0
- package/dist/bitmask.js +10 -0
- package/dist/powered-file-system/append.d.ts +10 -0
- package/dist/powered-file-system/append.js +10 -0
- package/dist/powered-file-system/chmod.d.ts +10 -0
- package/dist/powered-file-system/chmod.js +10 -0
- package/dist/powered-file-system/chown.d.ts +10 -0
- package/dist/powered-file-system/chown.js +10 -0
- package/dist/powered-file-system/copy.d.ts +10 -0
- package/dist/powered-file-system/copy.js +10 -0
- package/dist/powered-file-system/empty-dir.d.ts +10 -0
- package/dist/powered-file-system/empty-dir.js +10 -0
- package/dist/powered-file-system/mkdir.d.ts +10 -0
- package/dist/powered-file-system/mkdir.js +10 -0
- package/dist/powered-file-system/read.d.ts +10 -0
- package/dist/powered-file-system/read.js +10 -0
- package/dist/powered-file-system/readdir.d.ts +10 -0
- package/dist/powered-file-system/readdir.js +10 -0
- package/dist/powered-file-system/readlink.d.ts +10 -0
- package/dist/powered-file-system/readlink.js +10 -0
- package/dist/powered-file-system/realpath.d.ts +10 -0
- package/dist/powered-file-system/realpath.js +10 -0
- package/dist/powered-file-system/remove.d.ts +10 -0
- package/dist/powered-file-system/remove.js +10 -0
- package/dist/powered-file-system/rename.d.ts +10 -0
- package/dist/powered-file-system/rename.js +10 -0
- package/dist/powered-file-system/stat.d.ts +10 -0
- package/dist/powered-file-system/stat.js +10 -0
- package/dist/powered-file-system/symlink.d.ts +10 -0
- package/dist/powered-file-system/symlink.js +10 -0
- package/dist/powered-file-system/test.d.ts +10 -0
- package/dist/powered-file-system/test.js +10 -0
- package/dist/powered-file-system/write.d.ts +10 -0
- package/dist/powered-file-system/write.js +10 -0
- package/dist/powered-file-system.d.ts +10 -0
- package/dist/powered-file-system.js +10 -0
- package/dist/recurse-io-sync.d.ts +10 -0
- package/dist/recurse-io-sync.js +10 -0
- package/dist/recurse-io.d.ts +10 -0
- package/dist/recurse-io.js +10 -0
- package/package.json +10 -21
- package/readme.md +273 -426
- package/dist/bitmask.test.d.ts +0 -1
- package/dist/bitmask.test.js +0 -29
- package/dist/powered-file-system/append.test.d.ts +0 -1
- package/dist/powered-file-system/append.test.js +0 -47
- package/dist/powered-file-system/chmod.test.d.ts +0 -1
- package/dist/powered-file-system/chmod.test.js +0 -71
- package/dist/powered-file-system/chown.test.d.ts +0 -1
- package/dist/powered-file-system/chown.test.js +0 -82
- package/dist/powered-file-system/copy.test.d.ts +0 -1
- package/dist/powered-file-system/copy.test.js +0 -128
- package/dist/powered-file-system/empty-dir.test.d.ts +0 -1
- package/dist/powered-file-system/empty-dir.test.js +0 -61
- package/dist/powered-file-system/mkdir.test.d.ts +0 -1
- package/dist/powered-file-system/mkdir.test.js +0 -116
- package/dist/powered-file-system/read.test.d.ts +0 -1
- package/dist/powered-file-system/read.test.js +0 -75
- package/dist/powered-file-system/readdir.test.d.ts +0 -1
- package/dist/powered-file-system/readdir.test.js +0 -72
- package/dist/powered-file-system/readlink.test.d.ts +0 -1
- package/dist/powered-file-system/readlink.test.js +0 -44
- package/dist/powered-file-system/realpath.test.d.ts +0 -1
- package/dist/powered-file-system/realpath.test.js +0 -44
- package/dist/powered-file-system/remove.test.d.ts +0 -1
- package/dist/powered-file-system/remove.test.js +0 -78
- package/dist/powered-file-system/rename.test.d.ts +0 -1
- package/dist/powered-file-system/rename.test.js +0 -70
- package/dist/powered-file-system/stat.test.d.ts +0 -1
- package/dist/powered-file-system/stat.test.js +0 -79
- package/dist/powered-file-system/symlink.test.d.ts +0 -1
- package/dist/powered-file-system/symlink.test.js +0 -77
- package/dist/powered-file-system/test.test.d.ts +0 -1
- package/dist/powered-file-system/test.test.js +0 -76
- package/dist/powered-file-system/write.test.d.ts +0 -1
- package/dist/powered-file-system/write.test.js +0 -97
- package/dist/powered-file-system.test.d.ts +0 -1
- package/dist/powered-file-system.test.js +0 -46
- package/dist/suite.test.d.ts +0 -1
- package/dist/suite.test.js +0 -40
- package/dist/test-utils.d.ts +0 -18
- package/dist/test-utils.js +0 -72
package/readme.md
CHANGED
|
@@ -1,591 +1,438 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Powered File System
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/pwd-fs)
|
|
4
|
+
[](https://www.npmjs.com/package/pwd-fs)
|
|
5
|
+
[](https://www.npmjs.com/package/pwd-fs)
|
|
6
|
+
[](LICENSE)
|
|
5
7
|
|
|
6
|
-
`pwd
|
|
8
|
+
This module extends the [Node.js®](https://nodejs.org) file system API with a declared `pwd` (working directory) and recursive execution helpers. By default, all file system operations have asynchronous forms. The API provides an alternative set of asynchronous file system methods that return `Promise` objects.
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
## Getting Started
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
- Promise-based async methods
|
|
12
|
-
- matching synchronous variants via `{ sync: true }`
|
|
13
|
-
- recursive helpers for copy, remove, chmod, chown, and mkdir
|
|
12
|
+
### Installation
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
## Why Use It
|
|
18
|
-
|
|
19
|
-
Use `pwd-fs` when you want file system operations to be rooted at a specific working directory without manually calling `path.resolve()` before every operation.
|
|
20
|
-
|
|
21
|
-
It is especially useful for:
|
|
22
|
-
|
|
23
|
-
- CLI tools that operate inside a project root
|
|
24
|
-
- build or code generation scripts
|
|
25
|
-
- isolated test fixtures
|
|
26
|
-
- small automation tasks that need Promise-based file system helpers
|
|
27
|
-
|
|
28
|
-
## Installation
|
|
14
|
+
To use `Powered File System` in your project, run:
|
|
29
15
|
|
|
30
16
|
```bash
|
|
31
17
|
npm install pwd-fs
|
|
32
18
|
```
|
|
33
19
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
- [Quick Start](#quick-start)
|
|
37
|
-
- [Common Recipes](#common-recipes)
|
|
38
|
-
- [Compatibility](#compatibility)
|
|
39
|
-
- [Exports](#exports)
|
|
40
|
-
- [API](#api)
|
|
41
|
-
- [`new PoweredFileSystem(pwd?)`](#new-poweredfilesystempwd)
|
|
42
|
-
- [`pfs.pwd`](#pfspwd)
|
|
43
|
-
- [`pfs.resolve(src)`](#pfsresolvesrc)
|
|
44
|
-
- [`pfs.constants`](#pfsconstants)
|
|
45
|
-
- [`PoweredFileSystem.bitmask(mode)`](#poweredfilesystembitmaskmode)
|
|
46
|
-
- [`pfs.test(src, options?)`](#pfstestsrc-options)
|
|
47
|
-
- [`pfs.stat(src, options?)`](#pfsstatsrc-options)
|
|
48
|
-
- [`pfs.chmod(src, mode, options?)`](#pfschmodsrc-mode-options)
|
|
49
|
-
- [`pfs.chown(src, options?)`](#pfschownsrc-options)
|
|
50
|
-
- [`pfs.symlink(src, dest, options?)`](#pfssymlinksrc-dest-options)
|
|
51
|
-
- [`pfs.copy(src, dest, options?)`](#pfscopysrc-dest-options)
|
|
52
|
-
- [`pfs.rename(src, dest, options?)`](#pfsrenamesrc-dest-options)
|
|
53
|
-
- [`pfs.remove(src, options?)`](#pfsremovesrc-options)
|
|
54
|
-
- [`pfs.emptyDir(src, options?)`](#pfsemptydirsrc-options)
|
|
55
|
-
- [`pfs.read(src, options?)`](#pfsreadsrc-options)
|
|
56
|
-
- [`pfs.write(src, data, options?)`](#pfswritesrc-data-options)
|
|
57
|
-
- [`pfs.append(src, data, options?)`](#pfsappendsrc-data-options)
|
|
58
|
-
- [`pfs.readdir(dir, options?)`](#pfsreaddirdir-options)
|
|
59
|
-
- [`pfs.readlink(src, options?)`](#pfsreadlinksrc-options)
|
|
60
|
-
- [`pfs.realpath(src, options?)`](#pfsrealpathsrc-options)
|
|
61
|
-
- [`pfs.mkdir(dir, options?)`](#pfsmkdirdir-options)
|
|
62
|
-
- [Sync Mode](#sync-mode)
|
|
63
|
-
- [Error Behavior](#error-behavior)
|
|
64
|
-
- [Umask Behavior](#umask-behavior)
|
|
65
|
-
- [Notes](#notes)
|
|
66
|
-
- [Platform Caveats](#platform-caveats)
|
|
67
|
-
- [When To Use Native `fs`](#when-to-use-native-fs)
|
|
68
|
-
- [Development](#development)
|
|
69
|
-
- [License](#license)
|
|
70
|
-
|
|
71
|
-
## Quick Start
|
|
20
|
+
#### Table of Contents
|
|
72
21
|
|
|
73
|
-
|
|
74
|
-
import { pfs } from 'pwd-fs';
|
|
22
|
+
[class PoweredFileSystem](#class-poweredfilesystem)
|
|
75
23
|
|
|
76
|
-
|
|
77
|
-
|
|
24
|
+
* [constructor: new PoweredFileSystem([path])](#constructor-new-poweredfilesystempath)
|
|
25
|
+
* [pfs.test(src[, options])](#pfstestsrc-options)
|
|
26
|
+
* [pfs.stat(src[, options])](#pfsstatsrc-options)
|
|
27
|
+
* [pfs.chmod(src, mode[, options])](#pfschmodsrc-mode-options)
|
|
28
|
+
* [pfs.chown(src[, options])](#pfschownsrc-options)
|
|
29
|
+
* [pfs.symlink(src, dest[, options])](#pfssymlinksrc-dest-options)
|
|
30
|
+
* [pfs.copy(src, dir[, options])](#pfscopysrc-dir-options)
|
|
31
|
+
* [pfs.rename(src, dest[, options])](#pfsrenamesrc-dest-options)
|
|
32
|
+
* [pfs.remove(src[, options])](#pfsremovesrc-options)
|
|
33
|
+
* [pfs.emptyDir(src[, options])](#pfsemptydirsrc-options)
|
|
34
|
+
* [pfs.read(src[, options])](#pfsreadsrc-options)
|
|
35
|
+
* [pfs.write(src, data[, options])](#pfswritesrc-data-options)
|
|
36
|
+
* [pfs.append(src, data[, options])](#pfsappendsrc-data-options)
|
|
37
|
+
* [pfs.readdir(dir[, options])](#pfsreaddirdir-options)
|
|
38
|
+
* [pfs.readlink(src[, options])](#pfsreadlinksrc-options)
|
|
39
|
+
* [pfs.realpath(src[, options])](#pfsrealpathsrc-options)
|
|
40
|
+
* [pfs.mkdir(dir[, options])](#pfsmkdirdir-options)
|
|
41
|
+
* [pfs.pwd](#pfspwd)
|
|
42
|
+
* [static: bitmask(mode)](#static-bitmaskmode)
|
|
78
43
|
|
|
79
|
-
|
|
44
|
+
The class methods are divided into resource groups.
|
|
80
45
|
|
|
81
|
-
|
|
46
|
+
| Resource | Methods |
|
|
47
|
+
|-----------------------------|------------------------------------------------------------------|
|
|
48
|
+
| Common (file and directory) | `chmod` `chown` `copy` `realpath` `remove` `rename` `stat` `test` |
|
|
49
|
+
| File only | `append` `read` `write` |
|
|
50
|
+
| Directory only | `emptyDir` `mkdir` `readdir` |
|
|
51
|
+
| Link only | `readlink` `symlink` |
|
|
82
52
|
|
|
83
|
-
```ts
|
|
84
|
-
import { PoweredFileSystem } from 'pwd-fs';
|
|
85
53
|
|
|
86
|
-
|
|
54
|
+
#### class PoweredFileSystem
|
|
87
55
|
|
|
88
|
-
|
|
89
|
-
const exists = await projectFs.test('./.cache/build.txt');
|
|
90
|
-
```
|
|
56
|
+
This class is implemented in TypeScript and follows [Node.js](https://nodejs.org) file system semantics.
|
|
91
57
|
|
|
92
|
-
|
|
58
|
+
#### constructor: new PoweredFileSystem([path])
|
|
93
59
|
|
|
94
|
-
|
|
95
|
-
await pfs.mkdir('./dist');
|
|
96
|
-
await pfs.copy('./assets', './dist');
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Result:
|
|
100
|
-
|
|
101
|
-
- source: `./assets`
|
|
102
|
-
- destination directory: `./dist`
|
|
103
|
-
- created output: `./dist/assets`
|
|
60
|
+
- `path` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative directory name. `path` sets the `pfs.pwd` value. By default, it is [process.cwd()](https://nodejs.org/api/process.html#process_process_cwd).
|
|
104
61
|
|
|
105
|
-
|
|
62
|
+
String paths are interpreted as UTF-8 character sequences identifying absolute or relative file names.
|
|
106
63
|
|
|
107
64
|
```ts
|
|
108
|
-
|
|
65
|
+
import { pfs } from 'pwd-fs';
|
|
66
|
+
|
|
67
|
+
// pfs.pwd === process.cwd()
|
|
109
68
|
```
|
|
110
69
|
|
|
111
|
-
|
|
70
|
+
Relative paths will be resolved relative to the current working directory as specified by `process.cwd()`:
|
|
112
71
|
|
|
113
72
|
```ts
|
|
114
|
-
|
|
115
|
-
const resolved = await pfs.realpath('./current');
|
|
116
|
-
```
|
|
73
|
+
import { PoweredFileSystem } from 'pwd-fs';
|
|
117
74
|
|
|
118
|
-
|
|
75
|
+
// pfs.pwd === `${process.cwd()}/foo/bar`
|
|
119
76
|
|
|
120
|
-
|
|
121
|
-
await pfs.write('./app.log', 'first line\n');
|
|
122
|
-
await pfs.write('./app.log', 'second line\n', { flag: 'a' });
|
|
77
|
+
const pfs = new PoweredFileSystem('./foo/bar');
|
|
123
78
|
```
|
|
124
79
|
|
|
125
|
-
|
|
80
|
+
Absolute paths:
|
|
126
81
|
|
|
127
82
|
```ts
|
|
128
|
-
|
|
129
|
-
```
|
|
83
|
+
import { PoweredFileSystem } from 'pwd-fs';
|
|
130
84
|
|
|
131
|
-
|
|
85
|
+
// pfs.pwd === __dirname
|
|
132
86
|
|
|
133
|
-
|
|
134
|
-
if (await pfs.test('./tmp')) {
|
|
135
|
-
await pfs.remove('./tmp');
|
|
136
|
-
}
|
|
87
|
+
const pfs = new PoweredFileSystem(__dirname);
|
|
137
88
|
```
|
|
138
89
|
|
|
139
|
-
|
|
90
|
+
#### pfs.test(src[, options])
|
|
140
91
|
|
|
141
|
-
-
|
|
142
|
-
-
|
|
143
|
-
-
|
|
144
|
-
- `
|
|
145
|
-
|
|
146
|
-
- `x` access checks in `test()` do not have the same meaning on Windows as on Unix-like systems
|
|
92
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
93
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
94
|
+
- `flag` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is an optional string that specifies the accessibility checks to be performed. **Default:** `'e'`.
|
|
95
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
96
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with a `boolean`.
|
|
147
97
|
|
|
148
|
-
|
|
98
|
+
Tests a user's permissions for the file or directory specified by path.
|
|
149
99
|
|
|
150
100
|
```ts
|
|
151
|
-
|
|
101
|
+
const access = await pfs.test('./path');
|
|
102
|
+
console.log(access); // true
|
|
152
103
|
```
|
|
104
|
+
> Function `pfs.test()` resolves with `false` for access errors instead of rejecting.
|
|
153
105
|
|
|
154
|
-
|
|
155
|
-
- `pfs`: default instance rooted at `process.cwd()`
|
|
156
|
-
- `bitmask(mode)`: helper that extracts standard permission bits from `fs.Stats.mode`
|
|
157
|
-
|
|
158
|
-
## API
|
|
159
|
-
|
|
160
|
-
### `new PoweredFileSystem(pwd?)`
|
|
106
|
+
The following `flag` values are meant for use with `pfs.test()`.
|
|
161
107
|
|
|
162
|
-
|
|
108
|
+
Flag | Description
|
|
109
|
+
-----|-------------
|
|
110
|
+
`'e'` | Source exists
|
|
111
|
+
`'r'` | Source can be read
|
|
112
|
+
`'w'` | Source can be written
|
|
113
|
+
`'x'` | Source can be executed. This has no effect on Windows system (will behave like `e`).
|
|
163
114
|
|
|
164
|
-
|
|
165
|
-
- default: `process.cwd()`
|
|
166
|
-
|
|
167
|
-
```ts
|
|
168
|
-
import { PoweredFileSystem } from 'pwd-fs';
|
|
115
|
+
#### pfs.stat(src[, options])
|
|
169
116
|
|
|
170
|
-
|
|
171
|
-
|
|
117
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
118
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
119
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
120
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `fs.Stats`.
|
|
172
121
|
|
|
173
|
-
|
|
122
|
+
These functions return `lstat` information about a resource in the file system. Symbolic links are reported as links instead of followed targets.
|
|
174
123
|
|
|
175
|
-
|
|
124
|
+
#### pfs.chmod(src, mode[, options])
|
|
176
125
|
|
|
177
|
-
|
|
126
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
127
|
+
- `mode` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> Is a numeric `bitmask` created using a logical `OR`.
|
|
128
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
129
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
130
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
178
131
|
|
|
179
|
-
|
|
132
|
+
Asynchronously changes the permissions of a file or directory tree.
|
|
180
133
|
|
|
181
134
|
```ts
|
|
182
|
-
|
|
183
|
-
const file = pfs.resolve('./src/index.ts');
|
|
184
|
-
```
|
|
135
|
+
import { bitmask } from 'pwd-fs';
|
|
185
136
|
|
|
186
|
-
|
|
137
|
+
await pfs.chmod('./path', 0o750);
|
|
138
|
+
const { mode } = await pfs.stat('./path');
|
|
187
139
|
|
|
188
|
-
|
|
189
|
-
pfs.resolve('/tmp/outside.txt'); // '/tmp/outside.txt'
|
|
140
|
+
console.log(bitmask(mode) === 0o750); // true
|
|
190
141
|
```
|
|
191
142
|
|
|
192
|
-
|
|
143
|
+
> **Caveats:** on Windows only the write permission can be changed, and the distinction among the permissions of group, owner or others is not implemented.
|
|
193
144
|
|
|
194
|
-
|
|
145
|
+
See manuals [chmod(2)](http://man7.org/linux/man-pages/man2/chmod.2.html)
|
|
195
146
|
|
|
196
|
-
|
|
197
|
-
- `r`: readable
|
|
198
|
-
- `w`: writable
|
|
199
|
-
- `x`: executable
|
|
147
|
+
#### pfs.chown(src[, options])
|
|
200
148
|
|
|
201
|
-
|
|
149
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
150
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
151
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
152
|
+
- `uid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new owner's user id.
|
|
153
|
+
- `gid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new group's group id.
|
|
154
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
202
155
|
|
|
203
|
-
|
|
156
|
+
Asynchronously changes owner and group of a file or directory tree.
|
|
157
|
+
If `uid` or `gid` is omitted, the current value is preserved. On Windows, `chown()` validates the path but does not change POSIX ownership.
|
|
158
|
+
See manuals [chown(2)](http://man7.org/linux/man-pages/man2/chown.2.html).
|
|
204
159
|
|
|
205
|
-
|
|
206
|
-
const { mode } = await pfs.stat('./file.txt');
|
|
207
|
-
const permissions = PoweredFileSystem.bitmask(mode);
|
|
208
|
-
```
|
|
160
|
+
#### pfs.symlink(src, dest[, options])
|
|
209
161
|
|
|
210
|
-
|
|
162
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
163
|
+
- `dest` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the symbolic link to create. If `dest` exists, it will not be overwritten.
|
|
164
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
165
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
166
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
211
167
|
|
|
212
|
-
|
|
168
|
+
Asynchronously creates a new symbolic link (also known as a soft link).
|
|
213
169
|
|
|
214
170
|
```ts
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
options?: { sync?: T; flag?: 'e' | 'r' | 'w' | 'x' }
|
|
218
|
-
): T extends true ? boolean : Promise<boolean>
|
|
219
|
-
```
|
|
171
|
+
await pfs.symlink('./path', './link');
|
|
172
|
+
const stats = await pfs.stat('./link');
|
|
220
173
|
|
|
221
|
-
|
|
222
|
-
- `flag`: access check to perform
|
|
223
|
-
- default flag: `'e'`
|
|
224
|
-
|
|
225
|
-
```ts
|
|
226
|
-
const exists = await pfs.test('./notes.txt');
|
|
227
|
-
const writable = pfs.test('./notes.txt', { sync: true, flag: 'w' });
|
|
174
|
+
console.log(stats.isSymbolicLink()); // true
|
|
228
175
|
```
|
|
229
176
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
Returns `fs.lstat()` information for a path.
|
|
233
|
-
|
|
234
|
-
```ts
|
|
235
|
-
stat<T extends boolean = false>(
|
|
236
|
-
src: string,
|
|
237
|
-
options?: { sync?: T }
|
|
238
|
-
): T extends true ? Stats : Promise<Stats>
|
|
239
|
-
```
|
|
177
|
+
See manuals [symlink(2)](http://man7.org/linux/man-pages/man2/symlink.2.html).
|
|
240
178
|
|
|
241
|
-
|
|
179
|
+
#### pfs.copy(src, dir[, options])
|
|
242
180
|
|
|
243
|
-
|
|
181
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
182
|
+
- `dir` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the directory to which the resource is to be copied.
|
|
183
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
184
|
+
- `umask` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> Umask or file [mode creation mask](#mode-creation-mask) is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files or directories. See manuals [umask(2)](http://man7.org/linux/man-pages/man2/umask.2.html). Not supported on Windows system. **Default:** `0o000`.
|
|
185
|
+
- `overwrite` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Replace an existing target before copying. **Default:** `false`.
|
|
186
|
+
- `filter` <[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)> Optional `(src, dest) => boolean` predicate. Return `false` to skip a resource.
|
|
187
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
188
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
244
189
|
|
|
245
|
-
|
|
190
|
+
Asynchronously copies a file or directory recursively into `dir`. The destination directory must exist. The created target is `path.join(dir, path.basename(src))`.
|
|
246
191
|
|
|
247
192
|
```ts
|
|
248
|
-
|
|
249
|
-
src: string,
|
|
250
|
-
mode: number,
|
|
251
|
-
options?: { sync?: T }
|
|
252
|
-
): T extends true ? void : Promise<void>
|
|
253
|
-
```
|
|
193
|
+
import { pfs } from 'pwd-fs';
|
|
254
194
|
|
|
255
|
-
|
|
256
|
-
await pfs.
|
|
195
|
+
await pfs.mkdir('./dist');
|
|
196
|
+
await pfs.copy('./path/file.txt', './dist');
|
|
257
197
|
```
|
|
258
198
|
|
|
259
|
-
|
|
199
|
+
#### pfs.rename(src, dest[, options])
|
|
260
200
|
|
|
261
|
-
|
|
201
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
202
|
+
- `dest` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative destination path in the file system.
|
|
203
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
204
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
205
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
262
206
|
|
|
263
|
-
|
|
207
|
+
Rename file or directory. See manuals [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html).
|
|
264
208
|
|
|
265
209
|
```ts
|
|
266
|
-
|
|
267
|
-
src: string,
|
|
268
|
-
options?: { sync?: T; uid?: number; gid?: number }
|
|
269
|
-
): T extends true ? void : Promise<void>
|
|
210
|
+
await pfs.rename('./path/old_name.txt', './path/new_name.txt');
|
|
270
211
|
```
|
|
271
212
|
|
|
272
|
-
|
|
273
|
-
- when `uid` or `gid` is `0`, the current value from the source entry is preserved
|
|
274
|
-
- on Windows, ownership changes are not performed, but path validation still happens
|
|
275
|
-
|
|
276
|
-
### `pfs.symlink(src, dest, options?)`
|
|
213
|
+
#### pfs.remove(src[, options])
|
|
277
214
|
|
|
278
|
-
|
|
215
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
216
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
217
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
218
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
279
219
|
|
|
280
|
-
|
|
281
|
-
symlink<T extends boolean = false>(
|
|
282
|
-
src: string,
|
|
283
|
-
dest: string,
|
|
284
|
-
options?: { sync?: T }
|
|
285
|
-
): T extends true ? void : Promise<void>
|
|
286
|
-
```
|
|
220
|
+
Asynchronously removes a file or directory recursively. Missing paths reject or throw, matching `fs.rm(..., { force: false })`.
|
|
287
221
|
|
|
288
|
-
|
|
289
|
-
await pfs.symlink('./target.txt', './target-link.txt');
|
|
290
|
-
```
|
|
222
|
+
#### pfs.emptyDir(src[, options])
|
|
291
223
|
|
|
292
|
-
|
|
224
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the directory in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
225
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
226
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
227
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
293
228
|
|
|
294
|
-
|
|
229
|
+
Asynchronously removes all entries inside a directory while preserving the directory itself.
|
|
295
230
|
|
|
296
231
|
```ts
|
|
297
|
-
|
|
298
|
-
src: string,
|
|
299
|
-
dest: string,
|
|
300
|
-
options?: {
|
|
301
|
-
sync?: T;
|
|
302
|
-
umask?: number;
|
|
303
|
-
overwrite?: boolean;
|
|
304
|
-
filter?: (src: string, dest: string) => boolean;
|
|
305
|
-
}
|
|
306
|
-
): T extends true ? void : Promise<void>
|
|
232
|
+
await pfs.emptyDir('./cache');
|
|
307
233
|
```
|
|
308
234
|
|
|
309
|
-
|
|
235
|
+
#### pfs.read(src[, options])
|
|
310
236
|
|
|
311
|
-
-
|
|
312
|
-
-
|
|
313
|
-
- the
|
|
314
|
-
- `
|
|
315
|
-
- `
|
|
316
|
-
|
|
317
|
-
```ts
|
|
318
|
-
await pfs.copy('./assets', './dist');
|
|
319
|
-
```
|
|
237
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
238
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
239
|
+
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> | <[null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
240
|
+
- `flag` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> See support of [file system flags](#file-system-flags). **Default:** `'r'`.
|
|
241
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
242
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with a `string`. If `encoding` is <[null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)>, the data is returned as a <[Buffer](https://nodejs.org/api/buffer.html)> object.
|
|
320
243
|
|
|
321
|
-
|
|
244
|
+
Asynchronously reads the entire contents of a file.
|
|
322
245
|
|
|
323
246
|
```ts
|
|
324
|
-
await pfs.
|
|
325
|
-
|
|
326
|
-
filter: (src) => !src.endsWith('.map')
|
|
327
|
-
});
|
|
247
|
+
const content = await pfs.read('./file.txt');
|
|
248
|
+
console.log(content); // 'Lorem Ipsum...'
|
|
328
249
|
```
|
|
329
250
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
Renames or moves a file system entry.
|
|
333
|
-
|
|
334
|
-
```ts
|
|
335
|
-
rename<T extends boolean = false>(
|
|
336
|
-
src: string,
|
|
337
|
-
dest: string,
|
|
338
|
-
options?: { sync?: T }
|
|
339
|
-
): T extends true ? void : Promise<void>
|
|
340
|
-
```
|
|
251
|
+
#### pfs.write(src, data[, options])
|
|
341
252
|
|
|
342
|
-
|
|
253
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
254
|
+
- `data` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> | <[Buffer](https://nodejs.org/api/buffer.html)>
|
|
255
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
256
|
+
- `umask` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> Umask or file [mode creation mask](#mode-creation-mask) is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files or directories. See manuals [umask(2)](http://man7.org/linux/man-pages/man2/umask.2.html). Not supported on Windows system. **Default:** `0o000`.
|
|
257
|
+
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
258
|
+
- `flag` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> See support of [file system flags](#file-system-flags). **Default:** `'w'`.
|
|
259
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
260
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
343
261
|
|
|
344
|
-
|
|
262
|
+
Asynchronously writes `data` to a file, replacing the file if it already exists. If the file does not exist, a new file is created.
|
|
263
|
+
The encoding option is ignored if data is a buffer.
|
|
345
264
|
|
|
346
265
|
```ts
|
|
347
|
-
|
|
348
|
-
src: string,
|
|
349
|
-
options?: { sync?: T }
|
|
350
|
-
): T extends true ? void : Promise<void>
|
|
266
|
+
await pfs.write('./file.txt', '... some text');
|
|
351
267
|
```
|
|
352
268
|
|
|
353
|
-
|
|
269
|
+
> For `stream`, `fs.createWriteStream()` is recommended.
|
|
354
270
|
|
|
355
|
-
|
|
356
|
-
- symbolic links are unlinked without deleting the target
|
|
271
|
+
#### pfs.append(src, data[, options])
|
|
357
272
|
|
|
358
|
-
### `pfs.emptyDir(src, options?)`
|
|
359
273
|
|
|
360
|
-
|
|
274
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
275
|
+
- `data` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> | <[Buffer](https://nodejs.org/api/buffer.html)>
|
|
276
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
277
|
+
- `umask` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> Umask or file [mode creation mask](#mode-creation-mask) is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files or directories. See manuals [umask(2)](http://man7.org/linux/man-pages/man2/umask.2.html). Not supported on Windows system. **Default:** `0o000`.
|
|
278
|
+
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
279
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
280
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
361
281
|
|
|
362
|
-
|
|
363
|
-
emptyDir<T extends boolean = false>(
|
|
364
|
-
src: string,
|
|
365
|
-
options?: { sync?: T }
|
|
366
|
-
): T extends true ? void : Promise<void>
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
```ts
|
|
370
|
-
await pfs.emptyDir('./tmp');
|
|
371
|
-
```
|
|
282
|
+
Asynchronously appends data to a file, creating the file if it does not yet exist.
|
|
372
283
|
|
|
373
|
-
|
|
284
|
+
> NOTE Method is deprecated. May be removed in the next major version
|
|
374
285
|
|
|
375
|
-
|
|
286
|
+
**Use instead [pfs.write(src, data[, options])](#pfswritesrc-data-options) with { flag: 'a' } option**
|
|
376
287
|
|
|
377
288
|
```ts
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
sync?: T;
|
|
382
|
-
encoding?: BufferEncoding | null;
|
|
383
|
-
flag?: string;
|
|
384
|
-
}
|
|
385
|
-
): T extends true ? string | Buffer : Promise<string | Buffer>
|
|
289
|
+
await pfs.write('./file', 'some content', {
|
|
290
|
+
flag: 'a'
|
|
291
|
+
});
|
|
386
292
|
```
|
|
387
293
|
|
|
388
|
-
|
|
389
|
-
- use `encoding: null` to get a `Buffer`
|
|
390
|
-
- default `flag`: `'r'`
|
|
391
|
-
|
|
392
|
-
```ts
|
|
393
|
-
const text = await pfs.read('./file.txt');
|
|
394
|
-
const buffer = pfs.read('./file.txt', { sync: true, encoding: null });
|
|
395
|
-
```
|
|
294
|
+
#### pfs.readdir(dir[, options])
|
|
396
295
|
|
|
397
|
-
|
|
296
|
+
- `dir` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the directory you want to read.
|
|
297
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
298
|
+
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> | <[null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
299
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
300
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with an `Array` of file names in the directory excluding `'.'` and `'..'`. If `encoding` is <[null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)>, entries are returned as <[Buffer](https://nodejs.org/api/buffer.html)> objects. Otherwise, entries are strings.
|
|
398
301
|
|
|
399
|
-
|
|
302
|
+
Asynchronously reads the contents of a directory. Returns an empty `Array` if the directory is empty. See manuals [readdir(3)](http://man7.org/linux/man-pages/man3/readdir.3.html).
|
|
400
303
|
|
|
401
304
|
```ts
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
data: Buffer | string,
|
|
405
|
-
options?: {
|
|
406
|
-
sync?: T;
|
|
407
|
-
encoding?: BufferEncoding | null;
|
|
408
|
-
umask?: number;
|
|
409
|
-
flag?: string;
|
|
410
|
-
}
|
|
411
|
-
): T extends true ? void : Promise<void>
|
|
305
|
+
const list = await pfs.readdir('./files');
|
|
306
|
+
console.log(list); // ["icons", "logo.svg"]
|
|
412
307
|
```
|
|
413
308
|
|
|
414
|
-
|
|
415
|
-
- default `umask`: `0o000`
|
|
416
|
-
- default `flag`: `'w'`
|
|
417
|
-
- use `flag: 'a'` to append
|
|
418
|
-
- any valid Node.js string file flag is accepted, such as `'r'`, `'w'`, `'a'`, `'wx'`, or `'a+'`
|
|
419
|
-
|
|
420
|
-
```ts
|
|
421
|
-
await pfs.write('./report.txt', 'generated output');
|
|
422
|
-
await pfs.write('./report.txt', '\nnext line', { flag: 'a' });
|
|
423
|
-
```
|
|
309
|
+
#### pfs.readlink(src[, options])
|
|
424
310
|
|
|
425
|
-
|
|
311
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the symbolic link in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
312
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
313
|
+
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
314
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
315
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with a `string`.
|
|
426
316
|
|
|
427
|
-
|
|
317
|
+
Asynchronously reads the path stored in a symbolic link.
|
|
428
318
|
|
|
429
319
|
```ts
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
data: Buffer | string,
|
|
433
|
-
options?: {
|
|
434
|
-
sync?: T;
|
|
435
|
-
encoding?: BufferEncoding | null;
|
|
436
|
-
umask?: number;
|
|
437
|
-
}
|
|
438
|
-
): T extends true ? void : Promise<void>
|
|
320
|
+
const target = await pfs.readlink('./current');
|
|
321
|
+
console.log(target); // "./releases/current"
|
|
439
322
|
```
|
|
440
323
|
|
|
441
|
-
|
|
324
|
+
#### pfs.realpath(src[, options])
|
|
442
325
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
326
|
+
- `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths are resolved against `pfs.pwd`.
|
|
327
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
328
|
+
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
329
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
330
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with a `string`.
|
|
448
331
|
|
|
449
|
-
|
|
332
|
+
Asynchronously resolves a path to its canonical absolute location.
|
|
450
333
|
|
|
451
334
|
```ts
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
options?: { sync?: T; encoding?: BufferEncoding | null }
|
|
455
|
-
): T extends true ? string[] : Promise<string[]>
|
|
335
|
+
const realpath = await pfs.realpath('./current');
|
|
336
|
+
console.log(realpath); // "/absolute/path/to/releases/current"
|
|
456
337
|
```
|
|
457
338
|
|
|
458
|
-
|
|
339
|
+
#### pfs.mkdir(dir[, options])
|
|
459
340
|
|
|
460
|
-
|
|
341
|
+
- `dir` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the directory you want to create.
|
|
342
|
+
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
343
|
+
- `umask` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> Umask or file [mode creation mask](#mode-creation-mask) is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files or directories. See manuals [umask(2)](http://man7.org/linux/man-pages/man2/umask.2.html). Not supported on Windows system. **Default:** `0o000`.
|
|
344
|
+
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
345
|
+
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Resolves with `undefined`.
|
|
461
346
|
|
|
462
|
-
|
|
347
|
+
Recursively creates directories. Resolves successfully if the directory already exists.
|
|
463
348
|
|
|
464
349
|
```ts
|
|
465
|
-
|
|
466
|
-
src: string,
|
|
467
|
-
options?: { sync?: T; encoding?: BufferEncoding }
|
|
468
|
-
): T extends true ? string : Promise<string>
|
|
350
|
+
await pfs.mkdir('./static/images');
|
|
469
351
|
```
|
|
470
352
|
|
|
471
|
-
|
|
353
|
+
#### pfs.pwd
|
|
472
354
|
|
|
473
|
-
|
|
355
|
+
- <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>
|
|
474
356
|
|
|
475
|
-
|
|
476
|
-
realpath<T extends boolean = false>(
|
|
477
|
-
src: string,
|
|
478
|
-
options?: { sync?: T; encoding?: BufferEncoding }
|
|
479
|
-
): T extends true ? string : Promise<string>
|
|
480
|
-
```
|
|
357
|
+
The full path from the root directory to the working directory: in the context of which relative paths will be resolved.
|
|
481
358
|
|
|
482
|
-
|
|
359
|
+
#### static: bitmask(mode)
|
|
483
360
|
|
|
484
|
-
|
|
361
|
+
Masking is the act of applying a mask to a value. Bitwise ANDing in order to extract a subset of the bits in the value.
|
|
485
362
|
|
|
486
363
|
```ts
|
|
487
|
-
|
|
488
|
-
dir: string,
|
|
489
|
-
options?: { sync?: T; umask?: number }
|
|
490
|
-
): T extends true ? void : Promise<void>
|
|
491
|
-
```
|
|
364
|
+
import { bitmask } from 'pwd-fs';
|
|
492
365
|
|
|
493
|
-
|
|
494
|
-
|
|
366
|
+
// Access: (0644/-rw-r--r--)
|
|
367
|
+
const { mode } = await pfs.stat('./things.txt');
|
|
368
|
+
const access = bitmask(mode);
|
|
495
369
|
|
|
496
|
-
|
|
497
|
-
await pfs.mkdir('./public/assets/icons');
|
|
370
|
+
console.log(access === 0o644); // true
|
|
498
371
|
```
|
|
499
372
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
Every API method supports a synchronous form through `{ sync: true }`.
|
|
373
|
+
Applying the mask to the value means that we want to clear the first (higher) 4 bits, and keep the last (lower) 4 bits. Thus we have extracted the lower 4 bits. The result is:
|
|
503
374
|
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
const content = pfs.read('./cache/data.json', { sync: true });
|
|
375
|
+
```
|
|
376
|
+
mode: 33188
|
|
377
|
+
mask: 0o644 (rw-rw-r--)
|
|
508
378
|
```
|
|
509
379
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
Most async methods reject with the underlying Node.js error. Their sync variants throw the same class of error synchronously.
|
|
513
|
-
|
|
514
|
-
Typical cases:
|
|
515
|
-
|
|
516
|
-
- `test()` is the exception:
|
|
517
|
-
it returns `false` for inaccessible or missing paths instead of rejecting or throwing
|
|
518
|
-
- `read()`, `stat()`, `readdir()`, `chmod()`, `chown()`, `rename()`, and `remove()` fail for missing paths
|
|
519
|
-
- `readlink()` and `realpath()` fail for missing paths
|
|
520
|
-
- `read()` fails when the target is a directory
|
|
521
|
-
- `readdir()` fails when the target is not a directory
|
|
522
|
-
- `emptyDir()` fails when the target is not a directory
|
|
523
|
-
- `write()` fails when the target path points to a directory
|
|
524
|
-
- `copy()` fails when the source does not exist
|
|
525
|
-
- `copy()` fails when the destination already contains an entry with the same basename as the source, unless `overwrite: true` is used
|
|
526
|
-
- `copy()` fails when a directory is copied into itself
|
|
527
|
-
- `symlink()` fails when the destination already exists
|
|
528
|
-
- `mkdir()` accepts an existing directory, but fails when a path segment is a file
|
|
529
|
-
|
|
530
|
-
Practical pattern:
|
|
531
|
-
|
|
532
|
-
```ts
|
|
533
|
-
if (await pfs.test('./dist')) {
|
|
534
|
-
await pfs.remove('./dist');
|
|
535
|
-
}
|
|
380
|
+
#### Mode creation mask
|
|
536
381
|
|
|
537
|
-
|
|
538
|
-
```
|
|
382
|
+
The following table shows some examples of how to set the extension `mode` or `umask` for files and directories.
|
|
539
383
|
|
|
540
|
-
|
|
384
|
+
| Umask | Mode files | Mode directories |
|
|
385
|
+
|-------|-------------------|-------------------|
|
|
386
|
+
| 0o000 | 0o666 (rw-rw-rw-) | 0o777 (rwxrwxrwx) |
|
|
387
|
+
| 0o002 | 0o664 (rw-rw-r--) | 0o775 (rwxrwxr-x) |
|
|
388
|
+
| 0o007 | 0o660 (rw-rw----) | 0o770 (rwxrwx---) |
|
|
389
|
+
| 0o022 | 0o644 (rw-r--r--) | 0o755 (rwxr-xr-x) |
|
|
390
|
+
| 0o027 | 0o640 (rw-r-----) | 0o750 (rwxr-x---) |
|
|
391
|
+
| 0o077 | 0o600 (rw-------) | 0o700 (rwx------) |
|
|
392
|
+
| 0o277 | 0o400 (r--------) | 0o500 (r-x------) |
|
|
541
393
|
|
|
542
|
-
|
|
394
|
+
#### String encoding
|
|
543
395
|
|
|
544
|
-
|
|
396
|
+
The following encodings are available wherever the encoding option takes a <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>.
|
|
545
397
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
398
|
+
Encoding | Description
|
|
399
|
+
---------|------------
|
|
400
|
+
`'ascii'` | Each alphabetic, numeric or special character is represented by a 7-bit binary number (a string of seven 0 or 1), which is assigned a number from 0 to 127.
|
|
401
|
+
`'base64'` | Three 8-bit bytes (i.e., a total of 24 bits) can be represented by four 6-bit digits. The full specification of this form is contained in IANA [RFC 1421](https://tools.ietf.org/html/rfc1421) and [RFC 2045](https://tools.ietf.org/html/rfc2045).
|
|
402
|
+
`'hex'` | Encode each byte as two hexadecimal characters.
|
|
403
|
+
`'ucs2'` | 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.
|
|
404
|
+
`'utf16le'` | Like `'ucs2`.
|
|
405
|
+
`'utf8'` | Multibyte encoded Unicode characters. The first 128 characters of Unicode, which correspond one-to-one with `ascii`, are encoded using a single octet with the same binary value as `ascii`, so that valid `ascii` text is valid `utf8`-encoded Unicode as well.
|
|
406
|
+
`'latin1'` | Defined by the IANA in [RFC1345](https://tools.ietf.org/html/rfc1345), only in node 6.4.0+.
|
|
407
|
+
`'binary'` | Like `'latin1`.
|
|
552
408
|
|
|
553
|
-
|
|
409
|
+
#### File system flags
|
|
554
410
|
|
|
555
|
-
|
|
556
|
-
- Absolute paths are not constrained by `pfs.pwd`
|
|
557
|
-
- `stat()` returns `lstat()` data
|
|
558
|
-
- `remove()` does not follow symbolic links
|
|
559
|
-
- `append()` is kept for backward compatibility and is deprecated
|
|
411
|
+
The following flags are available for `pfs.read` and `pfs.write` the flag option takes a <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>.
|
|
560
412
|
|
|
561
|
-
|
|
413
|
+
Flag | Description
|
|
414
|
+
-----|------------
|
|
415
|
+
`'a'` | Open file for appending. The file is created if it does not exist.
|
|
416
|
+
`'ax'` | Like `'a'` but fails if the path exists.
|
|
417
|
+
`'a+'` | Open file for reading and appending. The file is created if it does not exist.
|
|
418
|
+
`'ax+'` | Like `'a+'` but fails if the path exists.
|
|
419
|
+
`'as'` | Open file for appending in synchronous mode. The file is created if it does not exist.
|
|
420
|
+
`'as+'` | Open file for reading and appending in synchronous mode. The file is created if it does not exist.
|
|
421
|
+
`'r'` | Open file for reading. Fails if the file does not exist.
|
|
422
|
+
`'r+'` | Open file for reading and writing. Fails if the file does not exist.
|
|
423
|
+
`'rs+'` | Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache. Using this flag is not recommended unless it is needed.
|
|
424
|
+
`'w'`| Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
|
|
425
|
+
`'wx'` | Like `'w'` but fails if the path exists.
|
|
426
|
+
`'w+'` | Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
|
|
427
|
+
`'wx+'` | Like `'w+'` but fails if the path exists.
|
|
562
428
|
|
|
563
|
-
|
|
564
|
-
| --- | --- | --- |
|
|
565
|
-
| `chmod()` | Recursive permission changes work as expected | Permission handling is limited by platform behavior |
|
|
566
|
-
| `chown()` | Recursive ownership changes are applied | Ownership is not changed; only path validation is performed |
|
|
567
|
-
| `symlink()` | Link type is inferred by the platform | The implementation resolves the source first and chooses `file` or `junction` explicitly |
|
|
568
|
-
| `test(..., { flag: 'x' })` | Uses executable access checks | Does not have the same semantics as Unix execute checks |
|
|
569
|
-
| `remove()` on symlinks | Removes the link, not the target | Removes the link, not the target |
|
|
429
|
+
The behavior of some flags is platform-specific.
|
|
570
430
|
|
|
571
|
-
|
|
431
|
+
#### When To Use Native `fs`
|
|
572
432
|
|
|
573
433
|
Prefer native `node:fs` APIs directly when you need:
|
|
574
434
|
|
|
575
435
|
- streams such as `createReadStream()` or `createWriteStream()`
|
|
576
436
|
- advanced flags and options not exposed by this wrapper
|
|
577
437
|
- very low-level control over file descriptors
|
|
578
|
-
- exact parity with Node's callback-based APIs
|
|
579
|
-
|
|
580
|
-
## Development
|
|
581
|
-
|
|
582
|
-
```bash
|
|
583
|
-
yarn install --frozen-lockfile
|
|
584
|
-
yarn lint
|
|
585
|
-
yarn build
|
|
586
|
-
yarn test
|
|
587
|
-
```
|
|
588
|
-
|
|
589
|
-
## License
|
|
590
|
-
|
|
591
|
-
MIT
|
|
438
|
+
- exact parity with Node's callback-based APIs
|