1.0.0 • Published 5 years ago

@teplovs/node-copy v1.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 years ago

node-copy

Library for copying files recursively with renaming support.


Table of contents


Why

  • Supports globs (e.g. FolderName/** or Sources/*.js)
  • Correctly renames folders and adds children to the right place

Installation

To install the library, run:

npm install @teplovs/node-copy --save
# or if you prefer yarn:
yarn add @teplovs/node-copy

Using library

import { copy } from "@teplovs/node-copy"

;(async () => {
    await copy([ "Sources/*.png", "Sources/*.jpg" ], "Destination")
    console.log("Files copied successfully")
})()

API

copy(source, destination, options?)

Returns a Promise<string[]> with the destination file paths.

source

Type: string or string[]

Files to copy.

If any file doesn't exist, the error will be thrown.

destination

Type: string

Destination directory.

If it doesn't exist, it'll be created.

options

Type: object

Options available: | Option | Value | Description | |--------|-------|-------------| | rename | function (basename: string, path: string): string | Function takes basename and path as arguments, and returns the new basename. | | cwd | string, by default: process.cwd() | Working directory to find source files. | | overwrite | boolean, by default: true | Overwrite existing files. | | filter | function (basename: string, path: string): boolean | Function takes basename and path as arguments, and returns true or false (if true then file/folder will be copied) |

copy(...).onProgress(handler)

Adds listener for progress change.

Event info, passed to handler: | Option | Value | Description | |--------|-------|-------------| | percent | number from 0 to 1 | Percents of files copied (calculated using files count, not total file size) | | filesCopied | number | Count of files that are already copied | | copiedFilesSize | number | Total file size of copied files (in bytes) | | totalFiles | number | Total count of files |

copy(...).pause()

Pauses opened read streams.

copy(...).resume()

Resumes copying files.

copy(...).stop()

Stops copying files.