Skip to content

Instantly share code, notes, and snippets.

@PerpetualWar
Created April 18, 2023 13:20
Show Gist options
  • Save PerpetualWar/198261ef2cd58b58260d5dabda4fcc64 to your computer and use it in GitHub Desktop.
Save PerpetualWar/198261ef2cd58b58260d5dabda4fcc64 to your computer and use it in GitHub Desktop.
copy dirs without specific dir, namely .git or .node_modules
const { promises: fsPromises } = require('fs');
const path = require('path');
async function copyDirsWithout(source, destination, regex) {
const folderName = new RegExp(regex);
try {
console.log(`Copying from "${source}" to "${destination}"...`);
await fsPromises.cp(source, destination, {
recursive: true,
filter: (src, dest) => {
console.log(`Copying ${src}...`);
return !folderName.test(path.basename(src));
},
});
console.log('Directory was copied successfully!');
} catch (err) {
console.error(err);
}
}
const [sourcePath, destPath, regexPattern] = process.argv.slice(2);
if (!sourcePath || !destPath || !regexPattern) {
console.error(
'Usage: node copyDirsWithout.js <source> <destination> <regex>',
);
process.exit(1);
}
copyDirsWithout(sourcePath, destPath, regexPattern);
//Usage example:
// node copyDirsWithout.js ./something/bleh/ ./somelse/ 'node_modules'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment