Dependency free module to copy all files (sync) from directory in nodejs



const fs = require( 'fs' );
const path = require( 'path' );

module.exports = ( source, destination ) => {
if ( !fs.statSync( source ).isDirectory() ) throw 'Source path must be directory';
if ( !fs.statSync( destination ).isDirectory() )
throw 'Destination path must be directory';

const files = fs.readdirSync( source );
for ( var index in files ) {
fs.copyFileSync(
path.join( source, files[index] ),
path.join( destination, files[index] )
);
}
};

You can always promisify fs.stat and copyFile if you want to be pro ;)

Recent Posts

Git repository as dependency in package.json.

Iterating through nested JavaScript object and extracting data.

Modules Cheatsheet

Ultimate slugify function

XPath cheatsheet

Vanilla JS XPath query function

Online code utilities

Toggle window snippet (show / hide / bring to front) with Autohotkey

Remove new lines in quotations from csv with Regex

← Back to all posts