Skip to content

Commit 402c1d0

Browse files
robertjacobsonjprichardson
authored andcommitted
Show support for mode (#587)
Node fs's mkdir supports mode specification. After reviewing the source fs-extra does as well, but is not documented. Update the documentation to include the `options` parameter and provide a few examples of using `mode`.
1 parent 4da17fe commit 402c1d0

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

docs/ensureDir-sync.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
# ensureDirSync(dir)
1+
# ensureDirSync(dir[,options])
22

3-
Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`.
3+
Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`. If provided, options may specify the desired mode for the directory.
44

55
**Aliases:** `mkdirsSync()`, `mkdirpSync()`
66

77
- `dir` `<String>`
8-
8+
- `options` `<Integer>|<Object>`
99
## Example:
1010

1111
```js
1212
const fs = require('fs-extra')
1313

1414
const dir = '/tmp/this/path/does/not/exist'
15+
16+
const desiredMode = 0o2775
17+
const options = {
18+
mode: 0o2775
19+
}
20+
1521
fs.ensureDirSync(dir)
1622
// dir has now been created, including the directory it is to be placed in
23+
24+
fs.ensureDirSync(dir, desiredMod)
25+
// dir has now been created, including the directory it is to be placed in with permission 0o2775
26+
27+
fs.ensureDirSync(dir, options)
28+
// dir has now been created, including the directory it is to be placed in with permission 0o2775
1729
```

docs/ensureDir.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# ensureDir(dir, [callback])
1+
# ensureDir(dir[,options][,callback])
22

33
Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`.
44

55
**Aliases:** `mkdirs()`, `mkdirp()`
66

77
- `dir` `<String>`
8+
- `options` `<Integer>|<Object>`
89
- `callback` `<Function>`
910

1011
## Example:
@@ -13,13 +14,23 @@ Ensures that the directory exists. If the directory structure does not exist, it
1314
const fs = require('fs-extra')
1415

1516
const dir = '/tmp/this/path/does/not/exist'
17+
const desiredMode = 0o2775
18+
const options = {
19+
mode: 0o2775
20+
}
1621

1722
// With a callback:
1823
fs.ensureDir(dir, err => {
1924
console.log(err) // => null
2025
// dir has now been created, including the directory it is to be placed in
2126
})
2227

28+
// With a callback and a mode integer
29+
fs.ensureDir(dir, desiredMode, err => {
30+
console.log(err) // => null
31+
// dir has now been created with mode 0o2775, including the directory it is to be placed in
32+
})
33+
2334
// With Promises:
2435
fs.ensureDir(dir)
2536
.then(() => {
@@ -29,6 +40,15 @@ fs.ensureDir(dir)
2940
console.error(err)
3041
})
3142

43+
// With Promises and a mode integer:
44+
fs.ensureDir(dir, desiredMode)
45+
.then(() => {
46+
console.log('success!')
47+
})
48+
.catch(err => {
49+
console.error(err)
50+
})
51+
3252
// With async/await:
3353
async function example (directory) {
3454
try {
@@ -38,6 +58,16 @@ async function example (directory) {
3858
console.error(err)
3959
}
4060
}
41-
4261
example(dir)
62+
63+
// With async/await and an options object, containing mode:
64+
async function exampleMode (directory) {
65+
try {
66+
await fs.ensureDir(directory, options)
67+
console.log('success!')
68+
} catch (err) {
69+
console.error(err)
70+
}
71+
}
72+
exampleMode(dir)
4373
```

0 commit comments

Comments
 (0)