Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Commit

Permalink
Add j2m cli
Browse files Browse the repository at this point in the history
There is a new cli in src/bin/j2m.js that can convert a file or stdin
  • Loading branch information
Johannes Hertenstein committed May 12, 2016
1 parent e7b5596 commit ad769fb
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"j2m": "src/bin/j2m.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/FokkeZB/J2M.git"
Expand All @@ -21,5 +24,9 @@
"bugs": {
"url": "https://github.com/FokkeZB/J2M/issues"
},
"homepage": "https://github.com/FokkeZB/J2M#readme"
"homepage": "https://github.com/FokkeZB/J2M#readme",
"dependencies": {
"colors": "^1.1.2",
"minimist": "^1.2.0"
}
}
51 changes: 51 additions & 0 deletions src/bin/j2m.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node

var J2M = require('../J2M');
var settings = require('./settings');

var readline = require('readline');
var fs = require('fs');

var colors = require('colors');

/**
* All content will be written into this string. This string will then be converted.
* This cannot be done line-by-line because there are muliline contexts in both formats, that
* would be discarded
*
* @type {string}
*/
var input = '';

/**
* Does the final conversion of all data in the "input" variable and outputs it correctly
*/
function convert() {
if (settings.toJ) {
console.log(J2M.toJ(input));
} else if (settings.toM) {
console.log(J2M.toM(input));
} else {
console.error("Something went horribly wrong. This should never happen".red);
process.exit(500);
}
}


if (settings.stdin) {
var rl = readline.createInterface({
input: process.stdin,
terminal: false
});
rl.on('line', function(line){
input += line;
});

rl.on('close', function() {
convert();
process.exit(0);
});
} else {
input = fs.readFileSync(settings.filename, 'utf8');
convert();
}
45 changes: 45 additions & 0 deletions src/bin/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var argv = require('minimist')(process.argv.slice(2), {
boolean: ['toM', 'toJ', 'm', 'j', 'stdin']
});
var colors = require('colors');

var settings = {
toM: !!(argv.m || argv.toM),
toJ: !!(argv.j || argv.toJ),
stdin: !!argv.stdin,
filename: argv._[argv._.length - 1]
};


var USAGE = "J2M: Convert from JIRA text formatting to GitHub Flavored MarkDown and back again \n" +
"\n" +
"$ j2m [--toM|--toJ] [--stdin] $filename \n" +
"\n" +
"Options: \n" +
"--toM, -m: Treat input as jira text and convert it to Markdown \n" +
"--toJ, -j: Treat input as markdown text and convert it to Jira \n" +
"--stdin: Read input from stdin. In this case the give filename is ignored \n";

function exit(message, code) {
console.error('\n' + message.red + '\n');
console.log(USAGE);
process.exit(code);
}


if (!settings.stdin && !settings.filename) {
// TODO: neither stdin nor file?
exit('No file was specified. Did you mean to use --stdin ?');
}


if (!settings.toM && !settings.toJ) {
// TODO: Try to detect it automatically
if (settings.filename && settings.filename.indexOf('.md') > -1) {
settings.toJ = true;
} else {
exit('Neither --toM nor --toJ was specified', 2);
}
}

module.exports = settings;

0 comments on commit ad769fb

Please sign in to comment.