Skip to content

Commit

Permalink
add option to set thrift exec + nicer logs
Browse files Browse the repository at this point in the history
  • Loading branch information
amirbilu committed Aug 9, 2018
1 parent 27afa11 commit 71aed7b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ An easy way to compile thrift idl files using simple configuration.
"log": "debug",
"generators": [
{
"languages": [
"language": [
"js:node",
"java"
],
Expand Down
1 change: 0 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node
const Generator = require('../src');
const logger = require('../src/logger');
const argv = require('optimist').argv;
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion example/example1.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"log": "debug",
"generators": [
{
"languages": [
"language": [
"js:node",
"java"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thrift-generator",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"main": "src/index.js",
"scripts": {
Expand Down
55 changes: 32 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,66 @@ handlebars.registerHelper('gen', function(languages) {
return languages.map((lang) => `--gen ${lang}`).join(' ');
});


const defaults = {
idl: "**/*.thrift",
output: "./"
}

function mkdirIfNotExists(dir) {
if (!fs.existsSync(dir))
mkdirp.sync(dir);
output: "./",
executable: "thrift",
}

const thriftCli1 = handlebars.compile(`thrift {{gen languages}} -o {{output}} {{idl}}`);
const thriftCli2 = handlebars.compile(`thrift --gen {{language}} -out {{output}} {{idl}}`);

const thriftCli1 = handlebars.compile(`{{executable}} -v {{gen language}} -o {{output}} {{idl}}`);
const thriftCli2 = handlebars.compile(`{{executable}} -v --gen {{language}} -out {{output}} {{idl}}`);

module.exports = class {
constructor(config) {
this.config = config;
this.logger = Logger(config.log || 'info');
const {executable, idl, output, generators, ignore, log} = {...defaults, ...config};
this.defaults = {executable, idl, output};

this.generators = generators;
this.ignore = ignore;
this.logger = Logger(log || 'info');
}
generate() {
this.config.generators.forEach(async generator => {
generator = {...defaults,
this.generators.forEach(async generator => {

generator = {
...this.defaults,
...generator
};
mkdirIfNotExists(generator.output);

mkdirp.sync(generator.output);

const idls = await glob(generator.idl, {
ignore: this.config.ignore
ignore: this.ignore
});

let command = "";
idls.forEach(async idl => {
if (generator.languages) {
let command = "";
if (Array.isArray(generator.language)) {
command = thriftCli1({...generator,
idl
});
}

if (generator.language) {
else if (typeof generator.language == "string") {
command = thriftCli2({...generator,
idl
});
}

this.logger.debug(command);

const {
stdout,
stderror
} = await exec(command);
try{
const {
stdout,
stderror
} = await exec(command);

this.logger.debug(stdout);
}
catch(e){
this.logger.error("exception while trying to generate thrift files");
this.logger.debug(e);
}
});

});
Expand Down
17 changes: 13 additions & 4 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const winston = require('winston');
const colors = require('colors');

const format = winston.format.combine(
winston.format.label(
{
label: colors.green('[thrift-generator]')
}
),
winston.format.colorize(),
winston.format.printf(info => `${info.level} ${info.label} ${info.message}`)
);

module.exports = (level) => winston.createLogger({
level: level,
format: winston.format.json(),
format: format,
transports: [
new winston.transports.Console({
format: winston.format.simple()
})
new winston.transports.Console()
]
});

0 comments on commit 71aed7b

Please sign in to comment.