copyfiles

repository·master·Indexed 19 days ago

https://github.com/calvinmetcalf/copyfiles

A utility for copying files and directories supporting globs and directory structure manipulation. Available as a CLI tool with commands like `copyfiles` and `copyup`, or as a programmatic Node.js API. Features include flattening output, slicing paths off the bottom, excluding specific patterns, and following symbolic links.

Tokens
2.8K
Snippets
10
Records
14
Agent score
55%

What's inside copyfiles

  1. Use copyup to slice paths

    master

    The copyup command is a specialized version of copyfiles used to slice a specific number of path segments off the bottom of the source paths. When using copyup, the -u (or --up) flag defaults to 1 if not otherwise specified.

    Example: If you want to slice 1 level off the path, you can simply use copyup or copyfiles -u 1.

    # Using the specialized command (defaults to -u 1)
    copyup inFile outDirectory
    
    # Using the standard command with explicit slice
    copyfiles -u 1 inFile outDirectory
  2. Control path depth with the 'up' option

    master

    The up option determines how the directory structure of the source files is mapped to the destination directory.

    • up: 0 (Default): The full path from the source is recreated inside the destination directory.
    • up: true: Only the basename (the filename itself) is used. All files are flattened into the destination directory.
    • up: n (where n is a positive integer): The first n segments of the path are stripped. For example, if up: 1 and the file is a/b/c.txt, the destination will contain b/c.txt relative to the output directory.

    Note: If you attempt to set up to a value greater than the actual depth of the path, the function will throw an error: 'cant go up that far'.

  3. Prevent overwriting files with the 'soft' option

    master
    By default, copyFiles will overwrite files if they already exist in the destination. To prevent this, set the soft option to true in your configuration object. When soft is enabled, the function checks for the existence of the destination file using fs.stat and skips the copy operation if the file is already present.
  4. Tilde support for home directory on Windows

    master
    When using a tilde (~) to represent the home directory in source or destination paths on Windows, you must include either the -u (up) or -f (flat) option, or use the copyup command. Failure to do so will result in an Error: Illegal characters in path.
  5. Flatten output with the --flat option

    master

    Use the -f or --flat option to prevent the creation of subdirectories in the destination. Instead, all files from the source patterns will be placed directly into the output directory.

    Example: If you have ./foo/a.txt and ./foo/bar/b.txt, using -f will put both a.txt and b.txt directly into the out folder.

    copyfiles -f ./foo/*.txt ./foo/bar/*.txt out
  6. Slice paths off with the --up option

    master

    If your source files are inside a directory structure that you do not want replicated in the destination, use the -u or --up option followed by a number. This number represents how many levels of the path to strip from the bottom.

    Example: copyfiles -u 1 something/*.js out will take files from something/ and place them directly in out/ instead of out/something/.

    copyfiles -u 1 something/*.js out
  7. Use the copyfiles Programmatic API

    master

    You can use copyfiles as a Node.js module. The function takes an array of paths, an optional configuration argument, and a callback function.

    Arguments:

    • [paths]: An array of strings. The last element in the array is the destination path.
    • opt: An optional argument which can be:
      • A number: Sets the --up option.
      • true: Sets the --flat option.
      • An object: A hash of the various options (using the long names: up, all, flat, exclude, error, verbose, follow, and soft).
    • callback: A function called when the operation is complete.
    var copyfiles = require('copyfiles');
    
    copyfiles([paths], opt, callback);
  8. Reference: copyfiles CLI options

    master

    The following options are available for the copyfiles command:

    FlagLong FlagDescription
    -u, --upupSlice a path off the bottom of the paths (takes a [number])
    -a, --allallInclude files & directories beginning with a dot (.) [boolean]
    -f, --flatflatFlatten the output into a single directory [boolean]
    -e, --excludeexcludePattern or glob to exclude (can be passed multiple times)
    -E, --errorerrorThrow error if nothing is copied [boolean]
    -V, --verboseverbosePrint more information to console [boolean]
    -s, --softsoftDo not overwrite destination files if they exist [boolean]
    -F, --followfollowFollow symbolic links [boolean]
    -v, --versionversionShow version number [boolean]
    -h, --helphelpShow help [boolean]
    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)
      -E, --error    throw error if nothing is copied                      [boolean]
      -V, --verbose  print more information to console                     [boolean]
      -s, --soft     do not overwrite destination files if they exist      [boolean]
      -F, --follow   follow symbolink links                                [boolean]
      -v, --version  Show version number                                   [boolean]
      -h, --help     Show help                                            [boolean]
  9. Use the copyfiles CLI

    master

    The copyfiles command copies files and directories to a destination directory. The last argument provided is always treated as the output directory, which will be created if it does not exist. You can provide multiple input files or globs.

    Note for Windows users: Globs must be double quoted (e.g., "*.js"). On other platforms, quoting is optional but recommended for globstars.

    Basic Usage:

    copyfiles [options] inFile [more files ...] outDirectory
    copyfiles foo foobar foo/bar/*.js out