11.0.1 • Published 1 month ago

cpy v11.0.1

Weekly downloads
1,393,057
License
MIT
Repository
github
Last release
1 month ago

cpy

Copy files

IMPORTANT: This package has a lot of problems and I unfortunately don't have time to fix them. I would recommend against using this package until these problems are resolved. Help welcome (see the issue tracker) 🙏

Why

  • Fast by cloning the files whenever possible.
  • 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 cpy

Usage

import cpy from 'cpy';

await cpy([
	'source/*.png', // Copy all .png files
	'!source/goat.png', // Ignore goat.png
], 'destination');

// Copy node_modules to destination/node_modules
await cpy('node_modules', 'destination');

// Copy node_modules content to destination
await cpy('node_modules/**', 'destination');

// Copy node_modules structure but skip all files except package.json files
await cpy('node_modules/**/*.json', 'destination');

// Copy all png files into destination without keeping directory structure
await cpy('**/*.png', 'destination', {flat: true});

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.

flat

Type: boolean\ Default: false

Flatten directory structure. All copied files will be put in the same directory.

import cpy from 'cpy';

await cpy('src/**/*.js', 'destination', {
	flat: true
});
rename

Type: string | Function

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

import cpy from 'cpy';

await cpy('foo.js', 'destination', {
	// The `basename` is the filename with extension.
	rename: basename => `prefix-${basename}`
});

await cpy('foo.js', 'destination', {
	rename: 'new-name'
});
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.

import cpy from 'cpy';

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,
	sourcePath: string,
	destinationPath: string,
}
  • completedSize is in bytes
  • percent is a value between 0 and 1
  • sourcePath is the absolute source path of the current file being copied.
  • destinationPath is The absolute destination path of the current file being copied.

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:

import cpy from 'cpy';

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

Related

@jbabin91/configscomp-design@g7fe/g7-scriptsydd-buildreact-big-scheduler-sized-eventscreate-oval-next-app@xertz/coretest-lwd-cli@uzimaru0000/og-imagereact-big-scheduler-tdtreact-big-scheduler-tdt-v2bison-app-release-testcal-scripts@ngx-env/clibison-app-release-test2bison-app-release-test3npx-create-test-v2polemiccreate-zvs-app@infinitebrahmanuniverse/nolb-cp@saaspe/components@everything-registry/sub-chunk-1395react-typescript-schedulercreate-vsz-appflowmatic-cicdfn-lib-examplebirken-react-native-community-image-editor@cloudbase/static-builderstart-react-toolsbootstrap-mfe-appsplunk-slapcreate-tamagui-appcreate-templatecreate-templating-clicreate-test-vkui-appcreate-trpc-appxunreal-projectdesign-system-fitbank-450upstream-react-big-schedulerutquibusdamdiapobundlee@deboxsoft/release-itcasta-react-schedulercasta-scheduler@discuzq.icu/cli@discuzq/cli@discuzqfe/cli@discuzqsdk/clicepycfc-scriptscetus-clidonations-boxvue-kiwi@fht360/fht-git-sparse-checkoutdocarchive@eggjs/doctools@ehduardu/bat-frameworkvtexyumi-plugin-svgsumijs-plugin-pwacreate-amclin-nextjs-appcreate-algovisio-appcreate-ak-appcreate-ink-appcreate-react-email-testcreate-react-pencreate-pastel-appcreate-permaweb-appcreate-permaweb-app-test-1-1create-pilot-appcreate-pondercreate-turbo-ethcreate-vargas-appunichat-contract-sdkweb-ext-react@font-face/node@fleur/create-fleur-next-app@fleur/create-next-appcreate-an-app-testcreate-apollo-couchbase-servercreate-oacreate-orus-appcreate-sandbox-appcreate-sonnat-appcreate-steedos-appcreate-steedos-packagecpycnfcpy-clicpy-filescpy-sync-clicpyspeccreate-disnext-appcreate-carrot-templatecreate-capricreate-endline-appcreate-ghostcreate-hubs-appcreate-new-projectcreate-neru
11.0.1

1 month ago

11.0.0

6 months ago

10.0.0

12 months ago

10.1.0

12 months ago

9.0.1

2 years ago

9.0.0

2 years ago

8.1.2

3 years ago

8.1.1

4 years ago

8.1.0

4 years ago

8.0.1

4 years ago

8.0.0

4 years ago

7.3.0

5 years ago

7.2.0

5 years ago

7.1.0

5 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.0.0

7 years ago

5.1.0

7 years ago

5.0.0

7 years ago

4.0.1

8 years ago

4.0.0

8 years ago

3.4.1

9 years ago

3.4.0

9 years ago

3.3.1

9 years ago

3.3.0

9 years ago

3.2.0

9 years ago

3.1.0

9 years ago

3.0.0

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.0

9 years ago

0.2.0

10 years ago

0.1.0

10 years ago