Last active
April 2, 2020 03:49
-
-
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
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 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