@brickdoc/cpy

中文文档
8.1.2-patch.1发布于 2022/02/20MITTypeScript近 30 天下载 5807

Copy files

原始使用文档
文档目录8 节
  1. Why
  2. Install
  3. Usage
  4. API
  5. cpy(source, destination, options?)
  6. Progress reporting
  7. cpy.on('progress', handler)
  8. Related

cpy

Forked from sindresorhus/cpy and add security patch for CVE-2020-28469.

Copy files

Why

  • Fast by using streams.
  • Resilient by using graceful-fs.
  • User-friendly by accepting globs and creating non-existent destination directories.
  • User-friendly error messages.
  • Progress reporting.

Install

$ npm install @brickdoc/cpy

Usage

const cpy = require("cpy");

(async () => {
    await cpy(["source/*.png", "!source/goat.png"], "destination");
    console.log("Files copied!");
})();

API

cpy(source, destination, options?)

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

source

Type: string | string[]

Files to copy.

If any of the files do not exist, an error will be thrown (does not apply to globs).

destination

Type: string

Destination directory.

options

Type: object

Options are passed to globby.

In addition, you can specify the below options.

cwd

Type: string
Default: process.cwd()

Working directory to find source files.

overwrite

Type: boolean
Default: true

Overwrite existing files.

parents

Type: boolean
Default: false

Preserve path structure.

rename

Type: string | Function

Filename or function returning a filename used to rename every file in source.

const cpy = require("cpy");

(async () => {
    await cpy("foo.js", "destination", {
        rename: (basename) => `prefix-${basename}`,
    });
})();
concurrency

Type: number
Default: (os.cpus().length || 1) * 2

Number of files being copied concurrently.

ignoreJunk

Type: boolean
Default: true

Ignores junk files.

filter

Type: Function

Function to filter files to copy.

Receives a source file object as the first argument.

Return true to include, false to exclude. You can also return a Promise that resolves to true or false.

const cpy = require("cpy");

(async () => {
    await cpy("foo", "destination", {
        filter: (file) => file.extension !== "nocopy",
    });
})();
Source file object
path

Type: string
Example: '/tmp/dir/foo.js'

Resolved path to the file.

relativePath

Type: string
Example: 'dir/foo.js' if cwd was '/tmp'

Relative path to the file from cwd.

name

Type: string
Example: 'foo.js'

Filename with extension.

nameWithoutExtension

Type: string
Example: 'foo'

Filename without extension.

extension

Type: string
Example: 'js'

File extension.

Progress reporting

cpy.on('progress', handler)

handler(progress)

Type: Function

progress
{
    completedFiles: number,
    totalFiles: number,
    completedSize: number,
    percent: number
}
  • completedSize is in bytes
  • percent is a value between 0 and 1

Note that the .on() method is available only right after the initial cpy call, so make sure you add a handler before awaiting the promise:

(async () => {
    await cpy(source, destination).on("progress", (progress) => {
        // …
    });
})();

版本与发布记录

@brickdoc/cpy 共发布 1 个版本,首次发布于 2022/02/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
8.1.2-patch.1latest2022/02/204 年前

关于 @brickdoc/cpy 的常见问题

@brickdoc/cpy 是什么?

Copy files

如何安装 @brickdoc/cpy?

在项目目录运行 npm install @brickdoc/cpy,或使用 pnpm add / yarn add / bun add。

@brickdoc/cpy 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@brickdoc/cpy 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@brickdoc/cpy 可以商用吗?

@brickdoc/cpy 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brickdoc/cpy 的下载量有多少?

根据 npm 官方下载统计,@brickdoc/cpy 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,807 次,日均约 194 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brickdoc/cpy 还在维护吗?

最新版本 8.1.2-patch.1 发布于 2022/02/20(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/02/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。