This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |