Skip to content

Instantly share code, notes, and snippets.

@brianblakely
Last active March 11, 2019 16:26
Show Gist options
  • Save brianblakely/412e22d11bce73b0f38f to your computer and use it in GitHub Desktop.
Save brianblakely/412e22d11bce73b0f38f to your computer and use it in GitHub Desktop.
Broccoli Babel Plugin with and without Promises
// Unfinished - before throwing up hands and moving on to refactor
Babel.prototype.write = function(readTree, destDir) {
var self = this;
return readTree(self.inputTree).then(function(srcDir) {
var promise = new rsvp.Promise(function(resolve, reject) {
recreaddir(srcDir, function(err, files) {
files = files.forEach(function(file, index) {
// Normalize recursive-readdir path output
file = file.replace(/\\/g, '/');
file = file.replace(srcDir+'/', '');
var relativePath = srcDir + '/' + file;
var destinatPath = destDir + '/' + file;
if(path.extname(file) !== '.'+self.targetExtension) {
fs.readFile(relativePath, 'utf8', function(data) {
fs.writeFile(destinatPath, data);
});
if(index === files.length-1) {
resolve(srcDir);
}
return;
}
var options = self.copyOptions();
options.filename = options.sourceMapName = options.sourceFileName = relativePath;
fs.readFile(relativePath, 'utf8', function(string) {
var result = self.transform(string, options);
var subpath = path.dirname(destinatPath);
fs.stat(subpath, function(err, stats) {
if(!stats.isDirectory()) {
mkpath(subpath, function() {
fs.writeFile(destinatPath, result.code, function() {
if(index === files.length-1 && !result.map) {
resolve(srcDir);
}
});
if(result.map) {
fs.writeFile(destinatPath + '.map', JSON.stringify(result.map));
}
})
} else {
fs.writeFile(destinatPath, result.code);
if(result.map) {
fs.writeFile(destinatPath + '.map', JSON.stringify(result.map));
}
}
});
});
});
});
});
return promise;
});
};
Babel.prototype.write = function(readTree, destDir) {
var self = this;
return readTree(self.inputTree).then(function(srcDir) {
return deepList(srcDir).then(function(files) {
// File list is converted into promise collection as output is written.
return rsvp.all(files.map(function(file) {
var src = file,
dest = file.replace(srcDir, destDir);
var options = self.copyOptions();
options.filename = options.sourceMapName = options.sourceFileName = src;
return readAndTranspile(src, options, self.extensions).then(function(data) {
return writeOutput(dest, data, self.targetExtension);
});
}));
});
});
};
// Just-for-fun addition a few years later
Babel.prototype.write = async function(readTree, destDir) {
const srcDir = await readTree(this.inputTree);
const files = await deepList(srcDir)
const output = await Promise.all(files.map(async file => {
const src = file,
dest = file.replace(srcDir, destDir);
const options = this.copyOptions();
options.filename = options.sourceMapName = options.sourceFileName = src;
const data = await readAndTranspile(src, options, this.extensions);
return writeOutput(dest, data, this.targetExtension);
}));
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment