Skip to content

Commit

Permalink
Merge 73c2b38 into d1a01e7
Browse files Browse the repository at this point in the history
  • Loading branch information
EsTharian committed Sep 21, 2019
2 parents d1a01e7 + 73c2b38 commit d0d5e2f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -102,6 +102,30 @@ async function copyFiles () {
copyFiles()
```

Working with Multiple Files
----------

Examples:

```js
const fs = require('fs-extra')

let fileOptions = {
'/tmp/myfile': {
target: '/tmp/mynewfile',
callback: err => {
if (err) console.error(err)
else console.log('myfile success!')
}
},

'/tmp/mysecondfile': {
target: '/tmp/mysecondnewfile'
}
}

fs.multi('copy', fileOptions, () => console.log('multiple processes completed'))
```

Methods
-------
Expand All @@ -117,6 +141,7 @@ Methods
- [mkdirp](docs/ensureDir.md)
- [mkdirs](docs/ensureDir.md)
- [move](docs/move.md)
- [multi](docs/multi.md)
- [outputFile](docs/outputFile.md)
- [outputJson](docs/outputJson.md)
- [pathExists](docs/pathExists.md)
Expand All @@ -135,6 +160,7 @@ Methods
- [mkdirpSync](docs/ensureDir-sync.md)
- [mkdirsSync](docs/ensureDir-sync.md)
- [moveSync](docs/move-sync.md)
- [multiSync](docs/multiSync)
- [outputFileSync](docs/outputFile-sync.md)
- [outputJsonSync](docs/outputJson-sync.md)
- [pathExistsSync](docs/pathExists-sync.md)
Expand Down
31 changes: 31 additions & 0 deletions docs/multi-sync.md
@@ -0,0 +1,31 @@
# multiSync(method, files)

[`copySync`](copy-sync.md) or [`moveSync`](move-sync.md) multiple files or directories.

- `method` `<String>` Methods may be `copySync` or `moveSync`.
- `files` `<Object>` Destinations, options and callback functions for each file. Each key of `<Object>` is referred to `src` (see: [`copySync`](copy-sync.md) or [`moveSync`](move-sync.md)).
- `dest` `<String>`
- `opts` `<Object>`
- `overwrite` `<boolean>`: default is `true` for [`copySync`](copy-sync.md) or default is `false` for [`moveSync`](move-sync.md)
- `errorOnExist` `<boolean>`: for [`copySync`](copy-sync.md)
- `dereference` `<boolean>`: for [`copySync`](copy-sync.md)
- `preserveTimestamps` `<boolean>`: for [`copySync`](copy-sync.md)
- `filter` `<Function>`: for [`copySync`](copy-sync.md)

## Example:

```js
const fs = require('fs-extra')

let files = {
'/tmp/myfile': {
target: '/tmp/mynewfile'
},

'/tmp/mysecondfile': {
target: '/tmp/mysecondnewfile'
}
}

fs.multiSync('copy', files)
```
37 changes: 37 additions & 0 deletions docs/multi.md
@@ -0,0 +1,37 @@
# multi(method, files[, callback])

[`copy`](copy.md) or [`move`](move.md) multiple files or directories.

- `method` `<String>` Methods may be `copy` or `move`.
- `files` `<Object>` Destinations, options and callback functions for each file. Each key of `<Object>` is referred to `src` (see: [`copy`](copy.md) or [`move`](move.md)).
- `dest` `<String>`
- `opts` `<Object>`
- `overwrite` `<boolean>`: default is `true` for [`copy`](copy.md) or default is `false` for [`move`](move.md)
- `errorOnExist` `<boolean>`: fot [`copy`](copy.md)
- `dereference` `<boolean>`: [`copy`](copy.md)
- `preserveTimestamps` `<boolean>`: [`copy`](copy.md)
- `filter` `<Function>`: [`copy`](copy.md)
- `callback` `<Function>`
- `callback` `<Function>`

## Example:

```js
const fs = require('fs-extra')

let files = {
'/tmp/myfile': {
target: '/tmp/mynewfile',
callback: err => {
if (err) console.error(err)
else console.log('myfile success!')
}
},

'/tmp/mysecondfile': {
target: '/tmp/mysecondnewfile'
}
}

fs.multi('copy', files, () => console.log('multiple processes completed'))
```
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -15,7 +15,8 @@ module.exports = Object.assign(
require('./move'),
require('./output'),
require('./path-exists'),
require('./remove')
require('./remove'),
require('./multi')
)

// Export fs.promises as a getter property so that we don't trigger
Expand Down
5 changes: 5 additions & 0 deletions lib/multi-sync/index.js
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
multiSync: require('./multi-sync')
}
12 changes: 12 additions & 0 deletions lib/multi-sync/multi-sync.js
@@ -0,0 +1,12 @@
const fs = {
copy: require('../copy-sync'),
move: require('../move-sync')
}

const multiSync = (method, arr) => {
for (const [src, prop] of Object.entries(arr)) {
fs[method](src, prop.target, prop.opts)
}
}

module.exports = multiSync
5 changes: 5 additions & 0 deletions lib/multi/index.js
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
multi: require('./multi')
}
14 changes: 14 additions & 0 deletions lib/multi/multi.js
@@ -0,0 +1,14 @@
const fs = {
copy: require('../copy'),
move: require('../move')
}

const multi = async function (method, files, callback) {
for (const [src, prop] of Object.entries(files)) {
if (typeof prop.callback !== 'function') prop.callback = err => { if (err) console.error(err) }
await fs[method](src, prop.dest, prop.opts, prop.callback)
}
callback()
}

module.exports = multi

0 comments on commit d0d5e2f

Please sign in to comment.