Created
June 19, 2018 14:13
-
-
Save EduardoSaverin/c874efd63979d383b3dea79fbbe889f4 to your computer and use it in GitHub Desktop.
File Clone creation using ES6 generator though it copies itself it can be modified to copy other files.
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
"use strict"; | |
const fs = require('fs'); | |
const path = require('path'); | |
function clone(generatorFunction) { | |
function callback(err) { | |
if (err) { | |
return generator.throw(err); | |
} | |
const results = [].slice.call(arguments, 1); | |
generator.next(results.length > 1 ? results : results[0]); // <-- calls generator with value to continue after yield. | |
} | |
const generator = generatorFunction(callback); | |
generator.next(); // <--- Line that triggers cloning. | |
} | |
clone(function* (callback) { | |
const fileName = path.basename(__filename); | |
const myself = yield fs.readFile(fileName, 'utf8', callback); | |
yield fs.writeFile(`clone_of_${fileName}`, myself, callback); | |
console.log('Clone created'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment