pwd-fs 3.2.4 → 3.3.2
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 +4 -0
- package/dist/bitmask.js +17 -0
- package/dist/bitmask.test.d.ts +1 -0
- package/dist/bitmask.test.js +29 -0
- package/dist/{src/index.d.ts → index.d.ts} +3 -0
- package/dist/{src/index.js → index.js} +3 -0
- package/dist/powered-file-system/append.d.ts +9 -0
- package/dist/powered-file-system/append.js +10 -0
- package/dist/powered-file-system/append.test.d.ts +1 -0
- package/dist/powered-file-system/append.test.js +47 -0
- package/dist/powered-file-system/chmod.d.ts +7 -0
- package/dist/powered-file-system/chmod.js +28 -0
- package/dist/powered-file-system/chmod.test.d.ts +1 -0
- package/dist/powered-file-system/chmod.test.js +71 -0
- package/dist/powered-file-system/chown.d.ts +9 -0
- package/dist/powered-file-system/chown.js +45 -0
- package/dist/powered-file-system/chown.test.d.ts +1 -0
- package/dist/powered-file-system/chown.test.js +82 -0
- package/dist/powered-file-system/copy.d.ts +8 -0
- package/dist/powered-file-system/copy.js +29 -0
- package/dist/powered-file-system/copy.test.d.ts +1 -0
- package/dist/powered-file-system/copy.test.js +82 -0
- package/dist/powered-file-system/mkdir.d.ts +8 -0
- package/dist/powered-file-system/mkdir.js +28 -0
- package/dist/powered-file-system/mkdir.test.d.ts +1 -0
- package/dist/powered-file-system/mkdir.test.js +116 -0
- package/dist/powered-file-system/read.d.ts +9 -0
- package/dist/powered-file-system/read.js +29 -0
- package/dist/powered-file-system/read.test.d.ts +1 -0
- package/dist/powered-file-system/read.test.js +75 -0
- package/dist/powered-file-system/readdir.d.ts +8 -0
- package/dist/powered-file-system/readdir.js +26 -0
- package/dist/powered-file-system/readdir.test.d.ts +1 -0
- package/dist/powered-file-system/readdir.test.js +72 -0
- package/dist/powered-file-system/remove.d.ts +7 -0
- package/dist/powered-file-system/remove.js +36 -0
- package/dist/powered-file-system/remove.test.d.ts +1 -0
- package/dist/powered-file-system/remove.test.js +78 -0
- package/dist/powered-file-system/rename.d.ts +7 -0
- package/dist/powered-file-system/rename.js +28 -0
- package/dist/powered-file-system/rename.test.d.ts +1 -0
- package/dist/powered-file-system/rename.test.js +70 -0
- package/dist/powered-file-system/stat.d.ts +7 -0
- package/dist/powered-file-system/stat.js +26 -0
- package/dist/powered-file-system/stat.test.d.ts +1 -0
- package/dist/powered-file-system/stat.test.js +79 -0
- package/dist/powered-file-system/symlink.d.ts +4 -0
- package/dist/powered-file-system/symlink.js +52 -0
- package/dist/powered-file-system/symlink.test.d.ts +1 -0
- package/dist/powered-file-system/symlink.test.js +77 -0
- package/dist/powered-file-system/test.d.ts +8 -0
- package/dist/powered-file-system/test.js +30 -0
- package/dist/powered-file-system/test.test.d.ts +1 -0
- package/dist/powered-file-system/test.test.js +76 -0
- package/dist/powered-file-system/write.d.ts +10 -0
- package/dist/powered-file-system/write.js +36 -0
- package/dist/powered-file-system/write.test.d.ts +1 -0
- package/dist/powered-file-system/write.test.js +97 -0
- package/dist/powered-file-system.d.ts +127 -0
- package/dist/powered-file-system.js +144 -0
- package/dist/powered-file-system.test.d.ts +1 -0
- package/dist/powered-file-system.test.js +21 -0
- package/dist/recurse-io-sync.d.ts +20 -0
- package/dist/{src/recurse-io-sync.js → recurse-io-sync.js} +37 -37
- package/dist/{src/recurse-io.d.ts → recurse-io.d.ts} +15 -1
- package/dist/{src/recurse-io.js → recurse-io.js} +52 -58
- package/dist/suite.test.d.ts +1 -0
- package/dist/suite.test.js +40 -0
- package/dist/test-utils.d.ts +18 -0
- package/dist/test-utils.js +72 -0
- package/package.json +31 -14
- package/readme.md +331 -246
- package/.travis.yml +0 -12
- package/appveyor.yml +0 -16
- package/dist/src/bitmask.d.ts +0 -1
- package/dist/src/bitmask.js +0 -18
- package/dist/src/powered-file-system.d.ts +0 -173
- package/dist/src/powered-file-system.js +0 -262
- package/dist/src/recurse-io-sync.d.ts +0 -5
- package/src/bitmask.ts +0 -20
- package/src/index.ts +0 -7
- package/src/powered-file-system.ts +0 -508
- package/src/recurse-io-sync.ts +0 -108
- package/src/recurse-io.ts +0 -238
- package/test/__fmock.ts +0 -45
- package/test/append.spec.ts +0 -71
- package/test/bitmask.spec.ts +0 -23
- package/test/chmod.spec.ts +0 -77
- package/test/chown.spec.ts +0 -92
- package/test/constructor.spec.ts +0 -15
- package/test/copy.spec.ts +0 -101
- package/test/mkdir.spec.ts +0 -121
- package/test/read.spec.ts +0 -91
- package/test/readdir.spec.ts +0 -86
- package/test/remove.spec.ts +0 -78
- package/test/rename.spec.ts +0 -84
- package/test/stat.spec.ts +0 -99
- package/test/symlink.spec.ts +0 -95
- package/test/test.spec.ts +0 -80
- package/test/write.spec.ts +0 -104
- package/tsconfig.json +0 -14
package/readme.md
CHANGED
|
@@ -1,385 +1,470 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pwd-fs
|
|
2
2
|
|
|
3
|
-
[](https://ci.appveyor.com/project/woodger/pwd-fs)
|
|
6
|
-
[](https://coveralls.io/github/woodger/pwd-fs)
|
|
7
|
-
[](https://snyk.io/test/github/woodger/type-enforcement?targetFile=package.json)
|
|
3
|
+
[](https://github.com/woodger/pwd-fs/blob/master/LICENSE)
|
|
4
|
+
[](https://github.com/woodger/pwd-fs/actions/workflows/ci.yml)
|
|
8
5
|
|
|
9
|
-
|
|
6
|
+
`pwd-fs` is a path-aware wrapper around Node.js file system APIs.
|
|
10
7
|
|
|
11
|
-
|
|
8
|
+
It provides:
|
|
12
9
|
|
|
13
|
-
|
|
10
|
+
- a dedicated working directory per instance
|
|
11
|
+
- Promise-based async methods
|
|
12
|
+
- matching synchronous variants via `{ sync: true }`
|
|
13
|
+
- recursive helpers for copy, remove, chmod, chown, and mkdir
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
All relative paths are resolved against `pfs.pwd`.
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
## Why Use It
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
Use `pwd-fs` when you want file system operations to be scoped to 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
|
|
20
29
|
|
|
21
30
|
```bash
|
|
22
31
|
npm install pwd-fs
|
|
23
32
|
```
|
|
24
33
|
|
|
25
|
-
|
|
34
|
+
## Quick Start
|
|
26
35
|
|
|
27
|
-
|
|
36
|
+
```ts
|
|
37
|
+
import { pfs } from 'pwd-fs';
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
* [pfs.stat(src[, options])](#pfsstatsrc-options)
|
|
32
|
-
* [pfs.chmod(src, mode[, options])](#pfschmodsrc-mode-options)
|
|
33
|
-
* [pfs.chown(src, uid, gid[, options])](#pfschownsrc-uid-gid-options)
|
|
34
|
-
* [pfs.symlink(src, use[, options])](#pfssymlinksrc-use-options)
|
|
35
|
-
* [pfs.copy(src, dir[, options])](#pfscopysrc-dir-options)
|
|
36
|
-
* [pfs.rename(src, use[, options])](#pfsrenamesrc-use-options)
|
|
37
|
-
* [pfs.remove(src[, options])](#pfsremovesrc-options)
|
|
38
|
-
* [pfs.read(src[, options])](#pfsreadsrc-options)
|
|
39
|
-
* [pfs.write(src, data[, options])](#pfswritesrc-data-options)
|
|
40
|
-
* [pfs.readdir(dir[, options])](#pfsreaddirdir-options)
|
|
41
|
-
* [pfs.mkdir(dir[, options])](#pfsmkdirdir-options)
|
|
42
|
-
* [pfs.pwd](#pfspwd)
|
|
43
|
-
* [static: bitmask(mode)](#static-bitmaskmode)
|
|
39
|
+
await pfs.mkdir('./own/project'); // recursively create the directory
|
|
40
|
+
```
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
## Common Recipes
|
|
46
43
|
|
|
47
|
-
|
|
48
|
-
|-----------------------------|------------------------------------------------------------------|
|
|
49
|
-
| Common (file and directory) | `chmod` `chown` `copy` `remove` `rename` `symlink` `stat` `test` |
|
|
50
|
-
| File only | `read` `write` |
|
|
51
|
-
| Directory only | `mkdir` `readdir` |
|
|
44
|
+
### Work inside a scoped directory
|
|
52
45
|
|
|
46
|
+
```ts
|
|
47
|
+
import { PoweredFileSystem } from 'pwd-fs';
|
|
53
48
|
|
|
54
|
-
|
|
49
|
+
const projectFs = new PoweredFileSystem('/workspace/my-project');
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
await projectFs.write('./.cache/build.txt', 'ok');
|
|
52
|
+
const exists = await projectFs.test('./.cache/build.txt');
|
|
53
|
+
```
|
|
58
54
|
|
|
59
|
-
|
|
55
|
+
### Copy assets into a build directory
|
|
60
56
|
|
|
61
|
-
|
|
57
|
+
```ts
|
|
58
|
+
await pfs.mkdir('./dist');
|
|
59
|
+
await pfs.copy('./assets', './dist');
|
|
60
|
+
```
|
|
62
61
|
|
|
63
|
-
|
|
62
|
+
Result:
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
- source: `./assets`
|
|
65
|
+
- destination directory: `./dist`
|
|
66
|
+
- created output: `./dist/assets`
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
### Append to a file
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
await pfs.write('./app.log', 'first line\n');
|
|
72
|
+
await pfs.write('./app.log', 'second line\n', { flag: 'a' });
|
|
69
73
|
```
|
|
70
74
|
|
|
71
|
-
|
|
75
|
+
### Read a file as a buffer
|
|
72
76
|
|
|
73
77
|
```ts
|
|
74
|
-
|
|
78
|
+
const raw = await pfs.read('./archive.bin', { encoding: null });
|
|
79
|
+
```
|
|
75
80
|
|
|
76
|
-
|
|
81
|
+
### Remove a temporary directory recursively
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
```ts
|
|
84
|
+
if (await pfs.test('./tmp')) {
|
|
85
|
+
await pfs.remove('./tmp');
|
|
86
|
+
}
|
|
79
87
|
```
|
|
80
88
|
|
|
81
|
-
|
|
89
|
+
## Compatibility
|
|
82
90
|
|
|
83
|
-
|
|
84
|
-
|
|
91
|
+
- package `engines`: Node.js `>=13.2.0`
|
|
92
|
+
- module format: CommonJS package output with TypeScript declarations
|
|
93
|
+
- platform notes:
|
|
94
|
+
- `chown()` is effectively a no-op on Windows apart from path validation
|
|
95
|
+
- `chmod()` behavior on Windows is limited by the platform
|
|
96
|
+
- `x` access checks in `test()` do not have the same meaning on Windows as on Unix-like systems
|
|
85
97
|
|
|
86
|
-
|
|
98
|
+
## Exports
|
|
87
99
|
|
|
88
|
-
|
|
100
|
+
```ts
|
|
101
|
+
import PoweredFileSystem, { pfs, bitmask } from 'pwd-fs';
|
|
89
102
|
```
|
|
90
103
|
|
|
91
|
-
|
|
104
|
+
- `default`: `PoweredFileSystem`
|
|
105
|
+
- `pfs`: default instance rooted at `process.cwd()`
|
|
106
|
+
- `bitmask(mode)`: helper that extracts standard permission bits from `fs.Stats.mode`
|
|
107
|
+
|
|
108
|
+
## API
|
|
92
109
|
|
|
93
|
-
|
|
94
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
95
|
-
- `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'`.
|
|
96
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
97
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful read, the `Promise` is resolved with an value with a `boolean`.
|
|
110
|
+
### `new PoweredFileSystem(pwd?)`
|
|
98
111
|
|
|
99
|
-
|
|
112
|
+
Creates a new instance rooted at `pwd`.
|
|
113
|
+
|
|
114
|
+
- `pwd?: string`
|
|
115
|
+
- default: `process.cwd()`
|
|
100
116
|
|
|
101
117
|
```ts
|
|
102
|
-
|
|
103
|
-
console.log(test); // true
|
|
104
|
-
```
|
|
105
|
-
> Function `pfs.test()` return only `Promise.resolve()`
|
|
118
|
+
import { PoweredFileSystem } from 'pwd-fs';
|
|
106
119
|
|
|
107
|
-
|
|
120
|
+
const pfs = new PoweredFileSystem('./workspace');
|
|
121
|
+
```
|
|
108
122
|
|
|
109
|
-
|
|
110
|
-
-----|-------------
|
|
111
|
-
`'e'` | Source is visible
|
|
112
|
-
`'r'` | Permitted can be read
|
|
113
|
-
`'w'` | Permitted can be written
|
|
114
|
-
`'x'` | Permitted can be executed. This has no effect on Windows system (will behave like `e`).
|
|
123
|
+
### `pfs.pwd`
|
|
115
124
|
|
|
116
|
-
|
|
125
|
+
Absolute base directory used to resolve all relative paths.
|
|
117
126
|
|
|
118
|
-
|
|
119
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
120
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
121
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful read, the `Promise` is resolved with an value with a `fs.Stats`.
|
|
127
|
+
### `pfs.constants`
|
|
122
128
|
|
|
123
|
-
|
|
129
|
+
Access mode aliases used by `pfs.test()`:
|
|
124
130
|
|
|
125
|
-
|
|
131
|
+
- `e`: existence
|
|
132
|
+
- `r`: readable
|
|
133
|
+
- `w`: writable
|
|
134
|
+
- `x`: executable
|
|
126
135
|
|
|
127
|
-
|
|
128
|
-
- `mode` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> Is a numeric `bitmask` created using a logical `OR`.
|
|
129
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
130
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
131
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful change, the `Promise` is resolved with an value with a `undefined`.
|
|
136
|
+
### `PoweredFileSystem.bitmask(mode)`
|
|
132
137
|
|
|
133
|
-
|
|
138
|
+
Static alias for `bitmask(mode)`.
|
|
134
139
|
|
|
135
140
|
```ts
|
|
136
|
-
|
|
141
|
+
const { mode } = await pfs.stat('./file.txt');
|
|
142
|
+
const permissions = PoweredFileSystem.bitmask(mode);
|
|
143
|
+
```
|
|
137
144
|
|
|
138
|
-
|
|
139
|
-
const { mode } = await pfs.stat('./path');
|
|
145
|
+
### `pfs.test(src, options?)`
|
|
140
146
|
|
|
141
|
-
|
|
147
|
+
Checks whether a path is accessible.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
test<T extends boolean = false>(
|
|
151
|
+
src: string,
|
|
152
|
+
options?: { sync?: T; flag?: 'e' | 'r' | 'w' | 'x' }
|
|
153
|
+
): T extends true ? boolean : Promise<boolean>
|
|
142
154
|
```
|
|
143
155
|
|
|
144
|
-
|
|
156
|
+
- `src`: absolute or instance-relative path
|
|
157
|
+
- `flag`: access check to perform
|
|
158
|
+
- default flag: `'e'`
|
|
145
159
|
|
|
146
|
-
|
|
160
|
+
```ts
|
|
161
|
+
const exists = await pfs.test('./notes.txt');
|
|
162
|
+
const writable = pfs.test('./notes.txt', { sync: true, flag: 'w' });
|
|
163
|
+
```
|
|
147
164
|
|
|
148
|
-
|
|
165
|
+
### `pfs.stat(src, options?)`
|
|
149
166
|
|
|
150
|
-
|
|
151
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
152
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
153
|
-
- `uid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new owner's user id.
|
|
154
|
-
- `gid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new group's group id.
|
|
155
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful change, the `Promise` is resolved with an value with a `undefined`.
|
|
167
|
+
Returns `fs.lstat()` information for a path.
|
|
156
168
|
|
|
157
|
-
|
|
158
|
-
|
|
169
|
+
```ts
|
|
170
|
+
stat<T extends boolean = false>(
|
|
171
|
+
src: string,
|
|
172
|
+
options?: { sync?: T }
|
|
173
|
+
): T extends true ? Stats : Promise<Stats>
|
|
174
|
+
```
|
|
159
175
|
|
|
160
|
-
|
|
176
|
+
This method uses `lstat`, so symbolic links are reported as links instead of followed targets.
|
|
161
177
|
|
|
162
|
-
|
|
163
|
-
- `use` <[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. If `use` 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)> Following successful created link, the `Promise` is resolved with an value with a `undefined`.
|
|
178
|
+
### `pfs.chmod(src, mode, options?)`
|
|
167
179
|
|
|
168
|
-
|
|
180
|
+
Recursively applies permissions to a file or directory tree.
|
|
169
181
|
|
|
170
182
|
```ts
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
183
|
+
chmod<T extends boolean = false>(
|
|
184
|
+
src: string,
|
|
185
|
+
mode: number,
|
|
186
|
+
options?: { sync?: T }
|
|
187
|
+
): T extends true ? void : Promise<void>
|
|
175
188
|
```
|
|
176
189
|
|
|
177
|
-
|
|
190
|
+
```ts
|
|
191
|
+
await pfs.chmod('./build', 0o755);
|
|
192
|
+
```
|
|
178
193
|
|
|
179
|
-
|
|
194
|
+
Note: on Windows, permission handling is limited by platform behavior.
|
|
180
195
|
|
|
181
|
-
|
|
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
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
186
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful copied, the `Promise` is resolved with an value with a `undefined`.
|
|
196
|
+
### `pfs.chown(src, options?)`
|
|
187
197
|
|
|
188
|
-
|
|
198
|
+
Recursively applies ownership to a file or directory tree.
|
|
189
199
|
|
|
190
200
|
```ts
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
201
|
+
chown<T extends boolean = false>(
|
|
202
|
+
src: string,
|
|
203
|
+
options?: { sync?: T; uid?: number; gid?: number }
|
|
204
|
+
): T extends true ? void : Promise<void>
|
|
194
205
|
```
|
|
195
206
|
|
|
196
|
-
|
|
207
|
+
- `uid` and `gid` default to `0`
|
|
208
|
+
- when `uid` or `gid` is `0`, the current value from the source entry is preserved
|
|
209
|
+
- on Windows, ownership changes are not performed, but path validation still happens
|
|
197
210
|
|
|
198
|
-
|
|
199
|
-
- `use` <[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.
|
|
200
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
201
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
202
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful renamed, the `Promise` is resolved with an value with a `undefined`.
|
|
211
|
+
### `pfs.symlink(src, dest, options?)`
|
|
203
212
|
|
|
204
|
-
|
|
213
|
+
Creates a symbolic link from `dest` to `src`.
|
|
205
214
|
|
|
206
215
|
```ts
|
|
207
|
-
|
|
216
|
+
symlink<T extends boolean = false>(
|
|
217
|
+
src: string,
|
|
218
|
+
dest: string,
|
|
219
|
+
options?: { sync?: T }
|
|
220
|
+
): T extends true ? void : Promise<void>
|
|
208
221
|
```
|
|
209
222
|
|
|
210
|
-
|
|
223
|
+
```ts
|
|
224
|
+
await pfs.symlink('./target.txt', './target-link.txt');
|
|
225
|
+
```
|
|
211
226
|
|
|
212
|
-
|
|
213
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
214
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
215
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful removed, the `Promise` is resolved with an value with a `undefined`.
|
|
227
|
+
### `pfs.copy(src, dest, options?)`
|
|
216
228
|
|
|
217
|
-
|
|
229
|
+
Copies `src` into the destination directory.
|
|
218
230
|
|
|
219
|
-
|
|
231
|
+
```ts
|
|
232
|
+
copy<T extends boolean = false>(
|
|
233
|
+
src: string,
|
|
234
|
+
dest: string,
|
|
235
|
+
options?: { sync?: T; umask?: number }
|
|
236
|
+
): T extends true ? void : Promise<void>
|
|
237
|
+
```
|
|
220
238
|
|
|
221
|
-
|
|
222
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
223
|
-
- `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'`.
|
|
224
|
-
- `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'`.
|
|
225
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
226
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful read, the `Promise` is resolved with an value with a `string`. If no encoding is specified <[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.
|
|
239
|
+
Behavior:
|
|
227
240
|
|
|
228
|
-
|
|
241
|
+
- copying a file creates `dest/<basename(src)>`
|
|
242
|
+
- copying a directory creates `dest/<basename(src)>` recursively
|
|
243
|
+
- the target must not already exist
|
|
229
244
|
|
|
230
245
|
```ts
|
|
231
|
-
|
|
232
|
-
console.log(content); // 'Lorem Ipsum...'
|
|
246
|
+
await pfs.copy('./assets', './dist');
|
|
233
247
|
```
|
|
234
248
|
|
|
235
|
-
|
|
249
|
+
This creates `./dist/assets`, not a direct rename to `./dist`.
|
|
236
250
|
|
|
237
|
-
|
|
238
|
-
- `data` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>
|
|
239
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
240
|
-
- `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`.
|
|
241
|
-
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
242
|
-
- `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'`.
|
|
243
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
244
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful write, the `Promise` is resolved with an value with a `undefined`.
|
|
251
|
+
### `pfs.rename(src, dest, options?)`
|
|
245
252
|
|
|
246
|
-
|
|
247
|
-
The encoding option is ignored if data is a buffer.
|
|
253
|
+
Renames or moves a file system entry.
|
|
248
254
|
|
|
249
255
|
```ts
|
|
250
|
-
|
|
256
|
+
rename<T extends boolean = false>(
|
|
257
|
+
src: string,
|
|
258
|
+
dest: string,
|
|
259
|
+
options?: { sync?: T }
|
|
260
|
+
): T extends true ? void : Promise<void>
|
|
251
261
|
```
|
|
252
262
|
|
|
253
|
-
|
|
263
|
+
### `pfs.remove(src, options?)`
|
|
254
264
|
|
|
255
|
-
|
|
265
|
+
Removes a file, directory, or symbolic link.
|
|
256
266
|
|
|
267
|
+
```ts
|
|
268
|
+
remove<T extends boolean = false>(
|
|
269
|
+
src: string,
|
|
270
|
+
options?: { sync?: T }
|
|
271
|
+
): T extends true ? void : Promise<void>
|
|
272
|
+
```
|
|
257
273
|
|
|
258
|
-
|
|
259
|
-
- `data` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>
|
|
260
|
-
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
|
|
261
|
-
- `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`.
|
|
262
|
-
- `encoding` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Is the expected [string encoding](#string-encoding). **Default:** `'utf8'`.
|
|
263
|
-
- `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:** `'a'`.
|
|
264
|
-
- `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
|
|
265
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful write, the `Promise` is resolved with an value with a `undefined`.
|
|
274
|
+
Behavior:
|
|
266
275
|
|
|
267
|
-
|
|
276
|
+
- directories are removed recursively
|
|
277
|
+
- symbolic links are unlinked without deleting the target
|
|
268
278
|
|
|
269
|
-
|
|
279
|
+
### `pfs.read(src, options?)`
|
|
270
280
|
|
|
271
|
-
|
|
281
|
+
Reads a file.
|
|
272
282
|
|
|
273
283
|
```ts
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
284
|
+
read<T extends boolean = false>(
|
|
285
|
+
src: string,
|
|
286
|
+
options?: {
|
|
287
|
+
sync?: T;
|
|
288
|
+
encoding?: BufferEncoding | null;
|
|
289
|
+
flag?: string;
|
|
290
|
+
}
|
|
291
|
+
): T extends true ? string | Buffer : Promise<string | Buffer>
|
|
277
292
|
```
|
|
278
293
|
|
|
279
|
-
|
|
294
|
+
- default `encoding`: `'utf8'`
|
|
295
|
+
- use `encoding: null` to get a `Buffer`
|
|
296
|
+
- default `flag`: `'r'`
|
|
280
297
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful write, the `Promise` is resolved with an value with a `Array` of the names of the files in the directory excluding `'.'` and `'..'`. If no encoding is specified <[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. Otherwise, the data will be a string.
|
|
298
|
+
```ts
|
|
299
|
+
const text = await pfs.read('./file.txt');
|
|
300
|
+
const buffer = pfs.read('./file.txt', { sync: true, encoding: null });
|
|
301
|
+
```
|
|
286
302
|
|
|
287
|
-
|
|
303
|
+
### `pfs.write(src, data, options?)`
|
|
304
|
+
|
|
305
|
+
Writes a file and explicitly reapplies the computed mode.
|
|
288
306
|
|
|
289
307
|
```ts
|
|
290
|
-
|
|
291
|
-
|
|
308
|
+
write<T extends boolean = false>(
|
|
309
|
+
src: string,
|
|
310
|
+
data: Buffer | string,
|
|
311
|
+
options?: {
|
|
312
|
+
sync?: T;
|
|
313
|
+
encoding?: BufferEncoding | null;
|
|
314
|
+
umask?: number;
|
|
315
|
+
flag?: string;
|
|
316
|
+
}
|
|
317
|
+
): T extends true ? void : Promise<void>
|
|
292
318
|
```
|
|
293
319
|
|
|
294
|
-
|
|
320
|
+
- default `encoding`: `'utf8'`
|
|
321
|
+
- default `umask`: `0o000`
|
|
322
|
+
- default `flag`: `'w'`
|
|
323
|
+
- use `flag: 'a'` to append
|
|
324
|
+
- any valid Node.js string file flag is accepted, such as `'r'`, `'w'`, `'a'`, `'wx'`, or `'a+'`
|
|
295
325
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
326
|
+
```ts
|
|
327
|
+
await pfs.write('./report.txt', 'generated output');
|
|
328
|
+
await pfs.write('./report.txt', '\nnext line', { flag: 'a' });
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### `pfs.append(src, data, options?)`
|
|
302
332
|
|
|
303
|
-
|
|
333
|
+
Deprecated wrapper around `write(..., { flag: 'a' })`.
|
|
304
334
|
|
|
305
335
|
```ts
|
|
306
|
-
|
|
336
|
+
append<T extends boolean = false>(
|
|
337
|
+
src: string,
|
|
338
|
+
data: Buffer | string,
|
|
339
|
+
options?: {
|
|
340
|
+
sync?: T;
|
|
341
|
+
encoding?: BufferEncoding | null;
|
|
342
|
+
umask?: number;
|
|
343
|
+
}
|
|
344
|
+
): T extends true ? void : Promise<void>
|
|
307
345
|
```
|
|
308
346
|
|
|
309
|
-
|
|
347
|
+
Prefer:
|
|
348
|
+
|
|
349
|
+
```ts
|
|
350
|
+
await pfs.write('./file.txt', 'content', { flag: 'a' });
|
|
351
|
+
```
|
|
310
352
|
|
|
311
|
-
|
|
353
|
+
### `pfs.readdir(dir, options?)`
|
|
354
|
+
|
|
355
|
+
Reads a directory and returns entry names.
|
|
356
|
+
|
|
357
|
+
```ts
|
|
358
|
+
readdir<T extends boolean = false>(
|
|
359
|
+
dir: string,
|
|
360
|
+
options?: { sync?: T; encoding?: BufferEncoding | null }
|
|
361
|
+
): T extends true ? string[] : Promise<string[]>
|
|
362
|
+
```
|
|
312
363
|
|
|
313
|
-
|
|
364
|
+
- default `encoding`: `'utf8'`
|
|
314
365
|
|
|
315
|
-
|
|
366
|
+
### `pfs.mkdir(dir, options?)`
|
|
316
367
|
|
|
317
|
-
|
|
368
|
+
Creates a directory tree recursively.
|
|
318
369
|
|
|
319
370
|
```ts
|
|
320
|
-
|
|
371
|
+
mkdir<T extends boolean = false>(
|
|
372
|
+
dir: string,
|
|
373
|
+
options?: { sync?: T; umask?: number }
|
|
374
|
+
): T extends true ? void : Promise<void>
|
|
375
|
+
```
|
|
321
376
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const access = bitmask(mode);
|
|
377
|
+
- existing directories are accepted
|
|
378
|
+
- default `umask`: `0o000`
|
|
325
379
|
|
|
326
|
-
|
|
380
|
+
```ts
|
|
381
|
+
await pfs.mkdir('./public/assets/icons');
|
|
327
382
|
```
|
|
328
383
|
|
|
329
|
-
|
|
384
|
+
## Sync Mode
|
|
385
|
+
|
|
386
|
+
Every API method supports a synchronous form through `{ sync: true }`.
|
|
330
387
|
|
|
388
|
+
```ts
|
|
389
|
+
pfs.mkdir('./cache', { sync: true });
|
|
390
|
+
pfs.write('./cache/data.json', '{}', { sync: true });
|
|
391
|
+
const content = pfs.read('./cache/data.json', { sync: true });
|
|
331
392
|
```
|
|
332
|
-
|
|
333
|
-
|
|
393
|
+
|
|
394
|
+
## Error Behavior
|
|
395
|
+
|
|
396
|
+
Most async methods reject with the underlying Node.js error. Their sync variants throw the same class of error synchronously.
|
|
397
|
+
|
|
398
|
+
Typical cases:
|
|
399
|
+
|
|
400
|
+
- `test()` is the exception:
|
|
401
|
+
it returns `false` for inaccessible or missing paths instead of rejecting or throwing
|
|
402
|
+
- `read()`, `stat()`, `readdir()`, `chmod()`, `chown()`, `rename()`, and `remove()` fail for missing paths
|
|
403
|
+
- `read()` fails when the target is a directory
|
|
404
|
+
- `readdir()` fails when the target is not a directory
|
|
405
|
+
- `write()` fails when the target path points to a directory
|
|
406
|
+
- `copy()` fails when the source does not exist
|
|
407
|
+
- `copy()` also fails when the destination already contains an entry with the same basename as the source
|
|
408
|
+
- `symlink()` fails when the destination already exists
|
|
409
|
+
- `mkdir()` accepts an existing directory, but fails when a path segment is a file
|
|
410
|
+
|
|
411
|
+
Practical pattern:
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
if (await pfs.test('./dist')) {
|
|
415
|
+
await pfs.remove('./dist');
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
await pfs.mkdir('./dist');
|
|
334
419
|
```
|
|
335
420
|
|
|
336
|
-
|
|
421
|
+
## Umask Behavior
|
|
422
|
+
|
|
423
|
+
`copy()`, `write()`, and `mkdir()` support `umask`.
|
|
424
|
+
|
|
425
|
+
Effective permissions:
|
|
426
|
+
|
|
427
|
+
| Umask | File mode | Directory mode |
|
|
428
|
+
| --- | --- | --- |
|
|
429
|
+
| `0o000` | `0o666` | `0o777` |
|
|
430
|
+
| `0o022` | `0o644` | `0o755` |
|
|
431
|
+
| `0o027` | `0o640` | `0o750` |
|
|
432
|
+
| `0o077` | `0o600` | `0o700` |
|
|
337
433
|
|
|
338
|
-
|
|
434
|
+
## Notes
|
|
339
435
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
| 0o007 | 0o660 (rw-rw----) | 0o770 (rwxrwx---) |
|
|
345
|
-
| 0o022 | 0o644 (rw-r--r--) | 0o755 (rwxr-xr-x) |
|
|
346
|
-
| 0o027 | 0o640 (rw-r-----) | 0o750 (rwxr-x---) |
|
|
347
|
-
| 0o077 | 0o600 (rw-------) | 0o700 (rwx------) |
|
|
348
|
-
| 0o277 | 0o400 (r--------) | 0o500 (r-x------) |
|
|
436
|
+
- Relative paths are always resolved against `pfs.pwd`
|
|
437
|
+
- `stat()` returns `lstat()` data
|
|
438
|
+
- `remove()` does not follow symbolic links
|
|
439
|
+
- `append()` is kept for backward compatibility and is deprecated
|
|
349
440
|
|
|
350
|
-
|
|
441
|
+
## Platform Caveats
|
|
351
442
|
|
|
352
|
-
|
|
443
|
+
| Area | Unix-like systems | Windows |
|
|
444
|
+
| --- | --- | --- |
|
|
445
|
+
| `chmod()` | Recursive permission changes work as expected | Permission handling is limited by platform behavior |
|
|
446
|
+
| `chown()` | Recursive ownership changes are applied | Ownership is not changed; only path validation is performed |
|
|
447
|
+
| `symlink()` | Link type is inferred by the platform | The implementation resolves the source first and chooses `file` or `junction` explicitly |
|
|
448
|
+
| `test(..., { flag: 'x' })` | Uses executable access checks | Does not have the same semantics as Unix execute checks |
|
|
449
|
+
| `remove()` on symlinks | Removes the link, not the target | Removes the link, not the target |
|
|
353
450
|
|
|
354
|
-
|
|
355
|
-
---------|------------
|
|
356
|
-
`'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.
|
|
357
|
-
`'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).
|
|
358
|
-
`'hex'` | Encode each byte as two hexadecimal characters.
|
|
359
|
-
`'ucs2` | 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.
|
|
360
|
-
`'utf16le'` | Like `'ucs2`.
|
|
361
|
-
`'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.
|
|
362
|
-
`'latin1'` | Defined by the IANA in [RFC1345](https://tools.ietf.org/html/rfc1345), only in node 6.4.0+.
|
|
363
|
-
`'binary'` | Like `'latin1`.
|
|
451
|
+
## When To Use Native `fs`
|
|
364
452
|
|
|
365
|
-
|
|
453
|
+
Prefer native `node:fs` APIs directly when you need:
|
|
366
454
|
|
|
367
|
-
|
|
455
|
+
- streams such as `createReadStream()` or `createWriteStream()`
|
|
456
|
+
- advanced flags and options not exposed by this wrapper
|
|
457
|
+
- very low-level control over file descriptors
|
|
458
|
+
- exact parity with Node's callback-based APIs
|
|
459
|
+
|
|
460
|
+
## Development
|
|
461
|
+
|
|
462
|
+
```bash
|
|
463
|
+
npm install
|
|
464
|
+
npm run build
|
|
465
|
+
npm test
|
|
466
|
+
```
|
|
368
467
|
|
|
369
|
-
|
|
370
|
-
-----|------------
|
|
371
|
-
`'a'` | Open file for appending. The file is created if it does not exist.
|
|
372
|
-
`'ax'` | Like `'a'` but fails if the path exists.
|
|
373
|
-
`'a+'` | Open file for reading and appending. The file is created if it does not exist.
|
|
374
|
-
`'ax+'` | Like `'a+'` but fails if the path exists.
|
|
375
|
-
`'as'` | Open file for appending in synchronous mode. The file is created if it does not exist.
|
|
376
|
-
`'as+'` | Open file for reading and appending in synchronous mode. The file is created if it does not exist.
|
|
377
|
-
`'r'` | Open file for reading. An exception occurs if the file does not exist.
|
|
378
|
-
`'r+'` | Open file for reading and writing. An exception occurs if the file does not exist.
|
|
379
|
-
`'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.
|
|
380
|
-
`'w'`| Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
|
|
381
|
-
`'wx'` | Like `'w'` but fails if the path exists.
|
|
382
|
-
`'w+'` | Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
|
|
383
|
-
`'wx+'` | Like `'w+'` but fails if the path exists.
|
|
468
|
+
## License
|
|
384
469
|
|
|
385
|
-
|
|
470
|
+
MIT
|