Skip to content

Instantly share code, notes, and snippets.

@akshayjai1
Last active April 2, 2020 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshayjai1/83c277a016786b70e66c7ff92deb7e82 to your computer and use it in GitHub Desktop.
Save akshayjai1/83c277a016786b70e66c7ff92deb7e82 to your computer and use it in GitHub Desktop.
node snippet to copy files matching a pattern from a folder to a new location with specific file name
const fs = require('fs-extra');
const folders = [
{
path: './build/static/css',
regex: /^(main).[\w]*.css$/,
outputFile: '../public-pages/style/sme.css'
},
{
path: './build/static/js',
regex: /^(main).[\w]*.js$/,
outputFile: '../public-pages/js/sme.js'
},
{
path: './build/assets/img/calendar-solid',
regex: /^[\w]*.png$/,
outputFile: '../public-pages/images/smb/calendar-solid/',
sameOutputName: true
},
{
path: './build/assets/img/',
regex: /^[\w]*.gif$/,
outputFile: '../public-pages/images/smb/',
sameOutputName: true
},
{
path: './build/policy/',
regex: /^[\w\W]*.docx$/,
outputFile: '../public-pages/policy/',
sameOutputName: true
}
];
folders.forEach(folder => {
fs.readdirSync(folder.path).forEach(file => {
if (folder.regex.test(file)) {
let fileDest = folder.outputFile;
if (folder.sameOutputName) {
fileDest = fileDest + file;
}
fs.copy(`${folder.path}/${file}`, fileDest)
.then(() => console.log(`success! copying file ${file}`))
.catch(err => {
console.error(err);
console.log(`success! copying file ${file}`);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment