https://github.com/ghiscoding/native-copyfiles
a small JS and CLI util to copy files
https://github.com/ghiscoding/native-copyfiles
Last synced: 8 months ago
JSON representation
a small JS and CLI util to copy files
- Host: GitHub
- URL: https://github.com/ghiscoding/native-copyfiles
- Owner: ghiscoding
- License: other
- Created: 2025-02-01T21:53:45.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-04-15T21:24:59.000Z (8 months ago)
- Last Synced: 2025-04-23T08:09:50.734Z (8 months ago)
- Language: TypeScript
- Size: 130 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://opensource.org/licenses/MIT)
[](http://www.typescriptlang.org/)
[](https://vitest.dev/)
[](https://codecov.io/gh/ghiscoding/native-copyfiles)
[](https://www.npmjs.com/package/native-copyfiles)
[](https://www.npmjs.com/package/native-copyfiles)
[](https://bundlephobia.com/result?p=native-copyfiles)
## Copyfiles
#### native-copyfiles
Copy files easily via JavaScript or the CLI, it uses [tinyglobby](https://www.npmjs.com/package/tinyglobby) internally for glob patterns and [yargs](https://www.npmjs.com/package/yargs) for the CLI.
The library is similar to the [copyfiles](https://www.npmjs.com/package/copyfiles) package, it is however written with more native NodeJS APIs and less dependencies. The package options are exactly the same (except for `--soft` which is not implemented).
> There is 1 major difference though, any options must be provided after the command as a suffix (the original project had them as prefix)
### Install
```bash
npm install native-copyfiles -g
```
### Command Line
```bash
Usage: copyfiles inFile [more files ...] outDirectory [options]
Options:
-u, --up slice a path off the bottom of the paths [number]
-a, --all include files & directories begining with a dot (.) [boolean]
-f, --flat flatten the output [boolean]
-e, --exclude pattern or glob to exclude (may be passed multiple times) [string|string[]]
-E, --error throw error if nothing is copied [boolean]
-V, --verbose print more information to console [boolean]
-F, --follow follow symbolink links [boolean]
-v, --version Show version number [boolean]
-h, --help Show help [boolean]
```
> Note: as opposed to the original [copyfiles](https://www.npmjs.com/package/copyfiles) project, any options **must** be provided as a suffix.
copy some files, give it a bunch of arguments, (which can include globs), the last one
is the out directory (which it will create if necessary). Note: on windows globs must be **double quoted**, everybody else can quote however they please.
```bash
copyfiles foo foobar foo/bar/*.js out
```
you now have a directory called out, with the files foo and foobar in it, it also has a directory named foo with a directory named
bar in it that has all the files from foo/bar that match the glob.
If all the files are in a folder that you don't want in the path out path, ex:
```bash
copyfiles something/*.js out
```
which would put all the js files in `out/something`, you can use the `--up` (or `-u`) option
```bash
copyfiles something/*.js out -u 1
```
which would put all the js files in `out`
you can also just do `-f` which will flatten all the output into one directory, so with files "./foo/a.txt" and "./foo/bar/b.txt"
```bash
copyfiles ./foo/*.txt ./foo/bar/*.txt out -f
```
will put "a.txt" and "b.txt" into out
if your terminal doesn't support globstars then you can quote them
```bash
copyfiles ./foo/**/*.txt out -f
```
does not work by default on a mac
but
```bash
copyfiles "./foo/**/*.txt" out -f
```
does.
You could quote globstars as a part of input:
```bash
copyfiles some.json "./some_folder/*.json" ./dist/ && echo 'JSON files copied.'
```
You can use the `-e` option to exclude some files from the pattern, so to exclude all files ending in ".test.js" you could do
```bash
copyfiles "**/*.test.js" -f ./foo/**/*.js out -e
```
Other options include
- `-a` or `--all` which includes files that start with a dot.
- `-F` or `--follow` which follows symbolinks
### JavaScript API
```js
import { copyfiles } from 'native-copyfiles';
copyfiles([paths], opt, callback);
```
The first argument is an array of paths whose last element is assumed the destination path.
The second argument (`opt`) being the options argument
and finally the third and last argument is a callback function which is executed after after all files copied
```js
{
verbose: bool, // enable debug messages
up: number, // -u value
exclude: string, // exclude pattern
all: bool, // include dot files
follow: bool, // Follow symlinked directories when expanding ** patterns
error: bool // raise errors if no files copied
}
```