In fs-extra 5.0.0, you fixed an issue that said that directories could be copied to their own subdirectories. This is wonderful, but I couldn't help but notice that it would break some scripts. In some cases, one may want to copy a directory to its subdirectory, and use a filter function to prevent recursion. Perhaps you could give an option which is false by default that allows to copy to a subdirectory?
@mcboat What's your real world use-case for copying to a subdirectory?
In my case, I intended on creating a script which automates the Electron building and packaging process, and to do that without affecting the source I intended on creating a subdirectory in the existing directory.
For example:
path/to/electron/application
|- src/
|- some source files
|- a few other files and dirs
|- build.js
path/to/electron/application
|- app/
|- some source files
|- src/
|- some source files
|- a few other files and dirs
|- build.js
path/to/electron/application
|- app/
|- some minified files
|- src/
|- some source files
|- a few other files and dirs
|- build.js
@manidlou @JPeer264 @jprichardson thoughts here?
@mcboat I am not sure If I understand your case! Are you copying from src to app? Where are you copying to subdirectory? Will you please give us an example with the code?
I'm not understanding the use-case here; closing for the time being.
With v5 this fails and I don't quite follow the logic.
await fs.copy('db', 'build/db');
yields this error: Error: Cannot copy '/db/util' to a subdirectory of itself, '/build/db/util'
I'm copying db to build/db, not to a subdirectory of db (itself).
@duhseekoh That's a bug in 5.0.0; should be fixed in 6.0.0
I saw this again in v8.0. Trying to copy a symlink over another symlink.
@vjpr Please open a new issue with a reduced test case.
Can we reopen this issue?
Here is a real world use-case:
const fse = require("fs-extra");
const filter = async (src) => {
const stat = await fse.lstat(src);
const dirname = path.basename(src);
if(stat.isFile() || (stat.isDirectory() && dirname !== "bak")) return true;
};
fse.copy("path/to/directory", "path/to/directory/bak", { filter });
IMO we should not even need a filter to do this. fse.copy() should silently ignore directory/dest/ when trying to copy directory/ contents into directory/dest/, and copy other children files and directories in directory/dest/ as asked.
That is a legitimate use-case, albeit a somewhat rare one. Generally, I feel it is better to be explicit than implicit. I do not think we should "silently ignore" certain files.
I do not think we should "silently ignore" certain files.
fs-extra seemed to behave this way in some other situations: remove() ends silently if the file doesn't exist, emptyDir() creates a dir if it does not exist...
For the record, here is my workaround (with glob, because I'm already using it in my project):
const fse = require("fs-extra");
const util = require("util");
const glob = util.promisify(require("glob"));
const path = require("path");
async function copyToSubdir(src, subdirName) {
const dirs = await glob("*/", { cwd: src, absolute: true, ignore: subdirName });
const proms = dirs.map((file) => {
const basename = path.basename(file);
const dest = path.join(src, subdirName, basename);
return fse.copy(file, dest);
});
return Promise.all(proms);
}
Most helpful comment
With v5 this fails and I don't quite follow the logic.
yields this error:
Error: Cannot copy '/db/util' to a subdirectory of itself, '/build/db/util'I'm copying
dbtobuild/db, not to a subdirectory ofdb(itself).