Created
August 28, 2019 23:13
-
-
Save einaralex/34b9ccb64e59ad6808f6c4a4580cf834 to your computer and use it in GitHub Desktop.
copy files with structure
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
// This script takes fromFolder, toFolder and a fileType | |
// it finds all files of the fileType in fromFolder and copies it to toFolder | |
// there is something wrong with how the path is resolved if the fromFolder does not exist | |
const glob = require('glob'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const fromFolder = process.argv[2] || 'src'; | |
const toFolder = process.argv[3] || 'dist'; | |
const fileType = process.argv[4] || 'scss'; | |
var copyFile = (file, toFolder) => { | |
const fileName = path.basename(file); | |
const originRelative = `/${path.relative(process.cwd(), path.dirname(file))}`; | |
const destinationRelative = originRelative.replace(fromFolder, toFolder); | |
const destinationAbsolute = path.resolve(__dirname + destinationRelative); | |
const destinationRootAbsolute = path.resolve(__dirname, toFolder); | |
const source = fs.createReadStream(file); | |
console.log(fileName); | |
console.log('origin', originRelative); | |
if (!fs.existsSync(destinationRootAbsolute)) { | |
console.log('1', destinationRootAbsolute); | |
fs.mkdir(destinationRootAbsolute, { recursive: true }, err => { | |
if (err) throw err; | |
}); | |
} | |
if (!fs.existsSync(destinationAbsolute)) { | |
console.log('2', destinationAbsolute); | |
fs.mkdir(destinationAbsolute, { recursive: true }, err => { | |
if (err) throw err; | |
}); | |
} | |
var dest = fs.createWriteStream(path.resolve(destinationAbsolute, fileName)); | |
source.pipe(dest); | |
source.on('end', function() { | |
console.log('Succesfully copied'); | |
}); | |
source.on('error', function(err) { | |
console.log(err); | |
}); | |
}; | |
glob(__dirname + '/' + fromFolder + '/**/*.' + fileType, {}, (err, files) => { | |
files.map(file => { | |
copyFile(file, toFolder); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment