Giter Club home page Giter Club logo

copy's Introduction

copy NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Copy files or directories using globs.

Table of Contents

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install --save copy

Usage

var copy = require('copy');

See the API documentation for usage details and available methods.

Examples

The main export is a function that takes:

  • src - glob pattern or file path(s)
  • dest - the destination directory
  • cb - callback function
copy('*.js', 'foo', function(err, files) {
  if (err) throw err;
  // `files` is an array of the files that were copied
});

Usage with gulp

In your project's gulpfile.js:

var gulp = require('gulp');
var copy = require('copy');

gulp.task('default', function (cb) {
  copy('fixtures/*.txt', 'actual', cb);
});

API

Copy a filepath, vinyl file, array of files, or glob of files to the given destination directory, with options and callback function that exposes err and the array of vinyl files that are created by the copy operation.

Params

  • patterns {String|Object|Array}: Filepath(s), vinyl file(s) or glob of files.
  • dir {String}: Destination directory
  • options {Object|Function}: or callback function
  • cb {Function}: Callback function if no options are specified

Example

copy('*.js', 'dist', function(err, file) {
  // exposes the vinyl `file` created when the file is copied
});

Copy an array of files to the given destination directory, with options and callback function that exposes err and the array of vinyl files that are created by the copy operation.

Params

  • files {Array}: Filepaths or vinyl files.
  • dir {String}: Destination directory
  • options {Object|Function}: or callback function
  • cb {Function}: Callback function if no options are specified

Example

copy.each(['foo.txt', 'bar.txt', 'baz.txt'], 'dist', function(err, files) {
  // exposes the vinyl `files` created when the files are copied
});

Copy a single file to the given dest directory, using the specified options and callback function.

Params

  • file {String|Object}: Filepath or vinyl file
  • dir {String}: Destination directory
  • options {Object|Function}: or callback function
  • cb {Function}: Callback function if no options are specified

Example

copy.one('foo.txt', 'dist', function(err, file) {
  if (err) throw err;
  // exposes the vinyl `file` that is created when the file is copied
});

CLI

To use the CLI, install copy globally with the following command:

$ npm install --global copy

This adds copy to your system path, allowing the copy command to be executed anywhere.

CLI usage

$ copy <patterns> <dir>
  • patterns: glob pattern or array of file paths
  • dir: destination directory

Example

Copy* all files with the .js extension to the foo directory

$ copy *.js foo

*Note that glob patterns may need to be wrapped in quotes depending on the shell you're using.

History

v0.2.0 - breaking changes

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
61 jonschlinkert
2 joakimbeng
1 bleathem
1 doowb
1 gsantiago
1 SoulRIver2015

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on September 01, 2017.

copy's People

Contributors

bleathem avatar doowb avatar gsantiago avatar joakimbeng avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

copy's Issues

Cannot read file - illegal operation

Edit:
Windows 10
npm 5.2.0

I can't upgrade npm right now because they have a bug in the most recent version that breaks another npm package for me, but there is a fix committed and will soon be included in a future release.


I am getting an unexpected behavior with the callback. Everything seems to work perfectly (the way I have it) but I am getting an error.

copy(["./src/**/*.html", "./src/fonts/**/*"], './dist/', function (err, file) {
    if (err) console.error(err);
});

Console output:

{ Error: [copy base] cannot read file > "C:\path\to\site\src\fonts\material-icons": EISDIR: illegal operation on a directory, read at Error (native) errno: -4068, code: 'EISDIR', syscall: 'read' }

I have compared the two directories ./src/ and ./dist/ and it looks like everything worked great. I just can't move forward because I am handling the error as thought it failed.


Before I run copy I completely wipe ./dist/ with rimraf so I know the copy is happening. Here's the full code without the long winded error handling:

var globby = require('globby');
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var copy = require('copy');
var shell = require('shelljs');

globby(["./dist/*"]).then(function (files) {
    
    // Clean
    files.map(function (file) {
        rimraf.sync(file);
    });

    // Create any necessary directories
    mkdirp('./dist/css/', function (err) {
        if (err) console.error(err);
        // handle errors, kill process
    });

    copy(["./src/**/*.html", "./src/fonts/**/*"], './dist/', function (err, file) {
        if (err) console.error(err);
        // handle errors, kill process
    });

    // compile sass
    shell.exec("sass ./src/css/main.scss:./dist/css/main.css --style compressed");

    // compile typescript
    shell.exec("tsc");
});

Recurcively creates directories copying single file

copy.one("public/assets/tmp/font-awesome-4.7.0/css/font-awesome.min.css", "public/assets/css");

This ends up creating the folders

public/assets/tmp/font-awesome-4.7.0/css

inside of:

public/assets/css

I just want to put the file into this directory without all the other dirs.

cwd is not calculating correctly

I have absolute paths here, a single file and a target directory:

        copy.one(file, destinationDirectory, opts, function(err, file) {
            cb(err, file)
        })

file = /home/michael-heuberger/binarykitchen/code/videomail.io/var/local/tmp/clients/videomail.io/11e6-0555-3e17b940-b805-ef6a2fea069a/videomail_preview.webm

destinationDirectory = /home/michael-heuberger/binarykitchen/code/videomail.io/var/local/uploads/videomail.io/videomail/11e6/05/55/11e6-0555-3e17b940-b805-ef6a2fea069a

but it gets ugly, the video gets copied into

/var/local/uploads/videomail.io/videomail/11e6/05/55/11e6-0555-3e17b940-b805-ef6a2fea069a/var/local/tmp/clients/videomail.io/11e6-0555-3e17b940-b805-ef6a2fea069a/videomail_preview.webm

which is not what i want. probably the cwd is messing here. how can the cwd be ignored, and just copy from an absolute file to an absolute destination directory?

Blocks event loop for too long

copy.one() can take about 500ms for small files which doesn't seem right.

I wonder if some internal code could be split between process.nextTick?

Pass options through CLI

I would like to be able to pass options to copy through the CLI. I see it is implemented in the branch cli-cwd, but not in master. Are you planning to merge?

Thanks!

What are the available options?

Hello.

Where might I find what options are available for the options parameter? I could be missing the obvious, but I didn't see them documented anywhere. Thanks.

Breaks in Node 10.x

When using Node 10, the plugin creates new files in the right place, but doesn't copy over the file contents

Weird behaviour if first glob in array is using '**'.

if I do a thing like

const copy = require('copy');

const files = ['dist/**/*','README.md','LICENSE.md'];
copy(files, '/tmp/folder', (err, files) => {
 // handle result here 
})

Result :

  • README.md and LICENSE.md are located in /tmp
  • The content of the dist folder is in '/tmp/folder'

Should be :

  • README.md and LICENSE.md are located in /tmp/folder
  • The content of the dist folder is in /tmp/folder/dist

Note that the order of the globs is important as if i declare it like :

const files = ['README.md','LICENSE.md','dist/**/*'];

It behaves as intended.

Cannot read property 'cwd' of undefined

My code fairly simple but maybe I'm missing something

var copy = require('copy');
var OUTPUT_DIR = '_output/';
copy.sync('src/*.js', OUTPUT_DIR );

I get this stacktrace

Error: Error: TypeError: Cannot read property 'cwd' of undefined
    at /home/graham/dev/<my_app>/node_modules/copy/index.js:55:13
    at Array.forEach (native)
    at Function.copySync [as sync] (/home/graham/dev/<my_app>/node_modules/copy/index.js:51:31)
    at Object.<anonymous> (/home/graham/dev/<my_app>/build.js:17:6)
    ...

Unexpected callback behavior when using globs

The callback isn't behaving as I expect when using glob file patterns.

This works as you might expect:

copy('src/api/public/index.php', 'dist', function (err, file) {})
// -> index.php is copied to dist/src/api/public/index.php

So, naturally, if I want to perform some action on index.php after its copied, I do something like:

copy('src/api/public/index.php', 'dist', function (err, file) {
  console.log(fs.statSync('dist/src/api/public/index.php'))
})
// -> File is found, all is well

Now consider:

copy('src/api/public/*', 'dist', function (err, file) {})
// -> index.php is copied to dist/index.php

But then this doesn't work:

copy('src/api/public/*', 'dist', function (err, file) {
  console.log(fs.statSync('dist/index.php'))
})

Output:

Error: ENOENT: no such file or directory, stat 'dist/index.php'

Why can't fs.statSync find the file when using the *?

wrong behavior on windows when using glob "*"

I've just noticed a wrong behavior on windows when using glob "*".

C:\test
└───source
    └───path
        └───to
            └───a
                └───file
└───dest1
└───dest2

The main objective is to copy the file file and its tree to the destination directory (dest1 or dest2).

When using a full path to specify which files to copy, everything goes well.

λ node_modules\.bin\copy source/path/to/a/file dest1/
√ dest1\source\path\to\a\file

The result is that the file file is copied with is whole tree.

C:\test
└───source
    └───path
        └───to
            └───a
                └───file
└───dest1
    └───source
        └───path
            └───to
                └───a
                    └───file
└───dest2

When using a glob pattern "*" to specify multiple files to copy, things get worse.

node_modules\.bin\copy source/path/to/a/* dest2/
√ ..\..\..\..\dest2\file

The result is that the file file is copied but without its tree.

C:\test
└───source
    └───path
        └───to
            └───a
                └───file
└───dest1
    └───source
        └───path
            └───to
                └───a
                    └───file
└───dest2
    └───file

This bug does not happen on linux.

copy.dir

hi,I use copy.dir,find can't copy with directory,all files copy into the same directory.what params I should
configuration,I don't want use callback to traversal directory.

File permissions are not preserved

Making a file executable then using copy on it does not copy the executable bit:

$ touch foo.sh
$ chmod +x foo.sh 
$ ls -l foo.sh
-rwxr-xr-x  1 me  1002642239  0 30 Oct 17:25 foo.sh
$ ./node_modules/.bin/copy-cli foo.sh out
✔ out/foo.sh
$ ls -l out
total 0
-rw-r--r--  1 me  1002642239  0 30 Oct 17:48 foo.sh

cwd not working as expected

Maybe I'm just confused, but with the following command I expect build/filename.ext to be copied to dist/plugins/filename.ext:

copy(`filename.ext`, `dist/plugins`, { cwd: 'build' }, err => {
    if (err) throw err;
});

Currently nothing is copied.

glob pattern does not allow for ignored patterns

I have a case where I want to copy all files out of my src directory except *.ts files. I'd expect the following code to prevent *.ts files from being copied to the destination folder.

copy(['src/**/*', '!src/**/*.ts'], 'dest', function (err, file) {
  // done
});

Cli doesn't allow `**` globbing patterns

When using copy via the cli, you can only use simple patterns like the one in the example. Using the API directly works fine with the same globs. I am running Windows 10 (pre-anniversary update)

Fails

Running from the command line.

$ copy src/**/*.html foo
# The syntax of the command is incorrect.
$ copy "src/**/*.html" foo

# src/**/*.html
# The filename, directory name, or volume label syntax is incorrect.
#        0 file(s) copied.

Works

Running from script

copy('src/**/*.html', 'bar', function (err, file) {
  if (err ) console.log(err);
  console.log('files copied');
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.