Skip to content

Commit

Permalink
feat: add vfm cli
Browse files Browse the repository at this point in the history
  • Loading branch information
uetchy committed Jun 15, 2020
1 parent beea760 commit 5733e64
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/vfm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"version": "1.0.0-alpha.2",
"author": "Yasuaki Uechi <y@uechi.io>",
"scripts": {
"build": "shx rm -rf lib && tsc",
"build": "shx rm -rf lib && tsc && shx chmod +x lib/cli.js",
"test": "jest"
},
"dependencies": {
"debug": "^4.1.1",
"hast-util-is-element": "^1.0.4",
"hastscript": "^5.1.2",
"meow": "^7.0.1",
"rehype-document": "^5.0.0",
"rehype-mathjax": "^2.0.0",
"rehype-raw": "^4.0.2",
Expand Down Expand Up @@ -49,6 +50,7 @@
"directory": "packages/vfm"
},
"license": "Apache-2.0",
"bin": "lib/cli.js",
"homepage": "https://github.com/vivliostyle/vfm",
"keywords": [
"markdown",
Expand Down
72 changes: 72 additions & 0 deletions packages/vfm/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env node

import fs from 'fs';
import readline from 'readline';
import meow from 'meow';
import {stringify} from '.';

const cli = meow(
`
Usage
$ vfm <filename>
$ echo <string> | vfm
Options
--partial, -r Output markdown fragments
--stylesheet Custom stylesheet path/URL
Examples
$ vfm input.md
`,
{
flags: {
partial: {
type: 'boolean',
alias: 'p',
},
stylesheet: {
type: 'string',
},
},
},
);

function convert(
input: string,
flags: meow.TypedFlags<{
partial: {type: 'boolean'; alias: string};
stylesheet: {type: 'string'; alias: string};
}> & {
[name: string]: unknown;
},
) {
return stringify(input, {
partial: flags.partial,
stylesheet: flags.stylesheet,
});
}

function main() {
try {
const filepath = cli.input[0];

if (filepath) {
return console.log(
convert(fs.readFileSync(filepath).toString(), cli.flags),
);
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
rl.on('line', function (line) {
console.log(convert(line, cli.flags));
});
} catch (err) {
console.log(err.message);
}
}

main();
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5047,7 +5047,7 @@ meow@^4.0.0:
redent "^2.0.0"
trim-newlines "^2.0.0"

meow@^7.0.0:
meow@^7.0.0, meow@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/meow/-/meow-7.0.1.tgz#1ed4a0a50b3844b451369c48362eb0515f04c1dc"
integrity sha512-tBKIQqVrAHqwit0vfuFPY3LlzJYkEOFyKa3bPgxzNl6q/RtN8KQ+ALYEASYuFayzSAsjlhXj/JZ10rH85Q6TUw==
Expand Down

0 comments on commit 5733e64

Please sign in to comment.