Skip to content

Instantly share code, notes, and snippets.

@EduardoSaverin
Created June 19, 2018 14:13
Show Gist options
  • Save EduardoSaverin/c874efd63979d383b3dea79fbbe889f4 to your computer and use it in GitHub Desktop.
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.
"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