Created
April 18, 2023 13:20
-
-
Save PerpetualWar/198261ef2cd58b58260d5dabda4fcc64 to your computer and use it in GitHub Desktop.
copy dirs without specific dir, namely .git or .node_modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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