copyfiles icon indicating copy to clipboard operation
copyfiles copied to clipboard

Programic API

Open jnthnjns opened this issue 7 years ago • 10 comments

Desperate need for documentation here.

I have been an advocate for npm scripting of build processes for many departments at my work and I sold them a group of npm packages to get the job done, copyfiles being one of them. We have recently converted from npm scripts within package.json to writing out a node build file.

We are going to have to switch away from copyfiles do to the lack of documentation and below.

takes an array of paths, last one is the destination path...

On the command line that makes sense, programatically it is nonsensical. I can't explain to other developers why the "last item in the array is the destination" and any new developer coming on board would have to get taught that as well.

This npm package has been great to me, I would love to see more documentation on "programmatic" usage as well an additional parameter when calling copyfiles in that environment.

Something like:


copyfiles(path, dest [, opts] [, callback])

Example

var copyfiles = require('copyfiles');

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

Example w/opts

var copyfiles = require('copyfiles');

// I honestly don't understand how you are taking options here because it isn't documented.
copyfiles(["./src/**/*.html"], "./dist/", "-u", function (err) {
    if (err) console.error(err);
});

jnthnjns avatar Aug 30 '17 18:08 jnthnjns

yeah I can try to add some more details on the programmatic api, I'm not sure when I'm going to get to it, I'm kinda swamped with some stuff right now so pull requests would always help

calvinmetcalf avatar Aug 30 '17 21:08 calvinmetcalf

@calvinmetcalf Great job!

I would also need more information. I need to use the -u 3 options programmatically. A quick explanation in a comment would help.

cheers!

BioPhoton avatar Oct 01 '17 22:10 BioPhoton

the signature is

copyfiles(paths, opts, callback);

with paths being an array of the input paths and the output being the last path and config being an object where the names correspond to the flags so you'd want it to be {up: 3}

calvinmetcalf avatar Oct 02 '17 12:10 calvinmetcalf

THX @calvinmetcalf for your answer!

Here is some source code as documentation for the options:

 .option('-u, --up [levels]', 'slice a path off the bottom of the paths', parseInt)
 .option('-a --all', 'include files and directories whose names begin with a dot (.)')
 .option('-f, --flat', 'flatten the output')
 .option('-e, --exclude [pattern]', 'pattern or glob to exclude')
 .option('-s, --soft', 'do not overwrite destination files if they exist')

as the key in the options object corresponds to the flags i guess i would be able to use short and long version. So i could use {up: 3} and {u: 3}

BioPhoton avatar Oct 06 '17 00:10 BioPhoton

no just the long option, so you can have 'all', 'flat', 'exclude' and 'soft'

calvinmetcalf avatar Oct 06 '17 12:10 calvinmetcalf

Actually in my case where I wanted to flatten while copying Programmatically I had to do this:

copyfiles(["./src/**/*.html", "./dist/"], true, function (err) {
    if (err) console.error(err);
});

I found this out by checking your tests. This really needs to be documented

spenoir avatar Feb 14 '18 13:02 spenoir

I know this issue is pretty old but using true as the second parameter stops you from adding other parameters like error or verbose.

After looking through the code I saw that flat = true is actually transformed into up = true so for anyone who wants to use multiple parameters one of which is flat, the following will do the trick:

copyfiles(["./src/**/*.html", "./dist/"], {up: true, error: true, verbose: true}, function (err) {
    if (err) console.error(err);
});

@calvinmetcalf might be worth allowing people to use flat in the parameters object since the Read.me says:

if it is an object it is a hash of the various options (the long version e.g. up, all, flat, exclude, error, verbose and soft)

If you agree, I can create a PR.

topalavlad avatar Jul 03 '19 11:07 topalavlad

hello, How can I transfer this node script : copyfiles file.json ../ --flat I trayed several ways but always nothing var copyfiles= require('copyfiles'); copyfiles(['file.json', '../'], true, function (err) { if (err) console.error(err); }); copyfiles(['file', '../'], -flat, function (err) { if (err) console.error(err); });

BehnooshShiva avatar Jan 07 '20 13:01 BehnooshShiva

Actually in my case where I wanted to flatten while copying Programmatically I had to do this:

copyfiles(["./src/**/*.html", "./dist/"], true, function (err) {
    if (err) console.error(err);
});

I found this out by checking your tests. This really needs to be documented

Yes docs is very poor and lies that copyfiles([paths], opt, callback) opt is optional. Both opt callback and must be used.

nolimitdev avatar May 29 '20 09:05 nolimitdev

How can I transfer this node script : copyfiles file.json ../ --flat

Your copy operation is very simple, so I recommend NodeJS built in fs module.

require('fs').copyFileSync('file.json', '../file.json');

Or you can use async alternative copyFile().

nolimitdev avatar May 29 '20 09:05 nolimitdev