Im playing around with a javascript project, it uses a node build script.
It syncing some folders into a built folder via
try {
fs.statSync('built/imgs');
} catch(err) {
if (err.code=='ENOENT') fs.symlinkSync('../imgs', 'built/imgs');
else throw err;
}
Whats the corresponding fs command to get a real copy of the files to the built folder?
copyFile() method is used to asynchronously copy a file from the source path to destination path. By default, Node. js will overwrite the file if it already exists at the given destination. The optional mode parameter can be used to modify the behavior of the copy operation.
With Node. js 8.5, a new File System feature is shipped by which you can copy the files using the core fs module.
Copy() : File « MS JScript « JavaScript Tutorial. The Copy() method is used to copy a file. overwrite is a Boolean value indicating whether to overwrite an existing file of the same name. 32.9.
There is no function in the fs
object that will copy a whole directory. There's not even one that will copy a whole file.
However, this is a quick and easy way to copy one file.
var fs = require('fs');
fs.createReadStream('input_filename').pipe(fs.createWriteStream('output_filename'));
Now you just need to get a directory list. You would use the fs.readdir
or fs.readdirSync
for that.
So to copy a directory to another you might do something like this:
var dir = fs.readdirSync('.');
for (var i=0; i < dir.length; i++) {
fs.createReadStream(dir[i]).pipe(fs.createWriteStream("newpath/"+dir[i]));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With