diff --git a/package.json b/package.json index 5e4549c..8dc675e 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" + } } diff --git a/src/bin/j2m.js b/src/bin/j2m.js new file mode 100755 index 0000000..df7c029 --- /dev/null +++ b/src/bin/j2m.js @@ -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(); +} diff --git a/src/bin/settings.js b/src/bin/settings.js new file mode 100644 index 0000000..e728465 --- /dev/null +++ b/src/bin/settings.js @@ -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; \ No newline at end of file