Copy file in node.js build script

I, playing with a javascript project, use node build script.

Sync some folders to the built-in folder via

try { fs.statSync('built/imgs'); } catch(err) { if (err.code=='ENOENT') fs.symlinkSync('../imgs', 'built/imgs'); else throw err; } 

What corresponds to the corresponding fs command to get a real copy of files in the built-in folder?

+4
source share
1 answer

There is no function in the fs object that will copy the entire directory. There is not even one that will copy the entire file.

However, this is a quick and easy way to copy a single file.

 var fs = require('fs'); fs.createReadStream('input_filename').pipe(fs.createWriteStream('output_filename')); 

Now you just need to get a list of directories. You would use fs.readdir or fs.readdirSync .

So, to copy the directory to another, you can 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])); } 
+4
source

All Articles