-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgmd.js
More file actions
59 lines (50 loc) · 1.27 KB
/
cgmd.js
File metadata and controls
59 lines (50 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node
import CodeGridMarkdown from '../lib/index.js';
import fs from 'node:fs';
import { parseArgs } from 'node:util';
const CGMDRenderer = new CodeGridMarkdown();
const usage = `Usage: cgmd <path/to/your/text.md> [options]
Options:
-o, --out <path> Output path
-h, --help Show help`;
let args;
try {
args = parseArgs({
args: process.argv.slice(2),
options: {
out: { type: 'string', short: 'o' },
help: { type: 'boolean', short: 'h' }
},
allowPositionals: true
});
} catch (err) {
console.error(err.message);
console.error(usage);
process.exit(1);
}
if (args.values.help) {
console.log(usage);
process.exit(0);
}
if (args.positionals.length === 0) {
console.error('Provide a path to a markdown file or markdown text.');
console.error(usage);
process.exit(1);
}
const inputPath = args.positionals[0];
const outputPath = args.values.out || null;
let inputStr = '';
try {
inputStr = fs.readFileSync(inputPath, { encoding: 'utf8' });
} catch(e) {
inputStr = inputPath;
}
const htmlStr = CGMDRenderer.render(inputStr);
if (outputPath) {
fs.writeFile(outputPath, htmlStr, { encoding: 'utf8' }, (err) => {
if (err) { throw err; }
console.log('%s created.', outputPath);
});
} else {
console.log(htmlStr);
}