Skip to content

Instantly share code, notes, and snippets.

@getchenge
Created March 26, 2015 13:15
Show Gist options
  • Save getchenge/1889113ffb789bb83e7b to your computer and use it in GitHub Desktop.
Save getchenge/1889113ffb789bb83e7b to your computer and use it in GitHub Desktop.
a thrift files generator
"use strict";
var fs = require('fs');
var path = require('path');
var async = require('async');
var childProcess = require('child_process');
var walk = function (dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function (file) {
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
walk('./thrift-api', function (err, files) {
async.map(files, function (item, cb) {
var out_path;
if (item.indexOf('.thrift') != -1) {
console.info('item_', item);
out_path = path.dirname(item);
childProcess.exec('thrift' + ' -o ' + out_path + ' -r --gen js:node ' + item, cb);
} else {
cb();
}
}, function (err, results) {
if (err) {
console.error('thrift_generate_err', err);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment