Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
batchfile
Advanced tools
Readme
Batch operations, transformations, and conversions on files.
Writing logic to
sucks.
Let's assume that we have a collection of Markdown files with [Mustache][mustache] templates. We want to create HTML files from these Markdown files.
Let's assume that our Markdown looks like this:
My Biography
============
By {{author}}
-------------
... some text ...
var marked = require('marked')
, hogan = require('hogan.js')
, fs = require('fs')
, path = require('path')
, mkdirp = require('mkdirp');
var files = ['bio1.md', 'bio2.md', 'bio3.md'];
var authors = ['JP', 'Leslie', 'Chris'];
var pending = files.length;
files.forEach(function(file, i) {
fs.exists(file, function(itDoes) {
it (!itDoes) {
throw new Error(file + " does not exist.");
} else {
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
throw err;
} else {
var bn = path.basename(file, '.md');
var newMd = hogan.compile(data).render({author: authors[i]});
var html = marked(newMd);
var newFile = path.join('/tmp/output/', bn + '.html');
var dir = path.dirname(newFile);
fs.exists(dir, function(dirExists) {
function wf(err) {
if (err) {
throw err;
}
fs.writeFile(newFile, html, function(err) {
if (err) {
throw err;
} else {
pending -= 1;
if (pending === 0) {
console.log("Phew, we are done!");
}
}
})
}
if (!dirExists) {
mkdirp(dir, wf);
} else {
wf(null);
}
})
}
})
}
})
});
Yuck. Very long and hard to follow code. Yes, you can definitely simplify this by using libraries such as async, seq, or batchflow. batchtransform
itself uses batchflow.
Let's see the previous sample rewritten using batchfile
.
var marked = require('marked')
, hogan = require('hogan.js')
, btf = require('batchfile');
var files = ['bio1.md', 'bio2.md', 'bio3.md'];
var authors = ['JP', 'Leslie', 'Chris'];
btf(files).transform(function(i, file, data, write) {
var bn = path.basename(file, '.md');
var newMd = hogan.compile(data.toString()).render({author: authors[i]});
var html = marked(newMd);
var newFile = path.join('/tmp/output/', bn + '.html');
write(newFile, html); //<--- always call write() last and only once.
}).error(function(err) {
throw err;
}).end(function() {
console.log('We are done! Much clearer!');
});
npm install batchfile
Constructor function.
files: can be either an array or strings
Returns new BatchTransform object.
Example:
var batchfile = require('batchfile');
var myBatch = batchfile('f1', 'file2', 'readme.md');
Transformation callback.
i: index of current iteration
file: file path
data: data buffer (don't forget to convert to a string)
write: the write callback. Call this last, and call it only once.
End callback is passed an array of written files.
i: index of current iteration
file: file path
data: data buffer (don't forget to convert to a string)
done: the done callback. Call this last, and call it only once.
End callback is passed an array of what was passed to the parameter of the done function.
The error callback.
The end callback. The callback is passed different parameters depending upon which function was called above.
node-batchfile
was written by JP Richardson. You should follow him on Twitter @jprichardson. Also read his coding blog Procbits. If you write software with others, you should checkout Gitpilot to make collaboration with Git simple.
(MIT License)
Copyright 2012, JP Richardson jprichardson@gmail.com
FAQs
Batch operations, transformations, and conversions on files.
The npm package batchfile receives a total of 0 weekly downloads. As such, batchfile popularity was classified as not popular.
We found that batchfile demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.