forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserde.mjs
More file actions
executable file
·74 lines (62 loc) · 1.9 KB
/
Copy pathserde.mjs
File metadata and controls
executable file
·74 lines (62 loc) · 1.9 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env node
import fs from 'fs/promises';
import { program } from 'commander';
import serializeMDX from './actions/serialize-mdx.mjs';
import deserializeHTML from './actions/deserialize-html.mjs';
const cantOpenPath = (path) => () => {
console.error(`😵 unable to open path ${path}`);
process.exit(1);
};
const assertPathIsNotEmpty = (path) => {
if (path == null || path === '') {
program.help({ error: true });
}
};
const serializeMdxToHtml = async (path, outputPath) => {
const mdx = await fs.readFile(path).catch(cantOpenPath(path));
const html = await serializeMDX(mdx).catch((err) => {
console.error('❌ error serializing MDX');
console.error(err);
process.exit(1);
});
if (outputPath) {
fs.writeFile(outputPath, html, 'utf-8');
} else {
console.log(html);
}
};
const deserializeHtmlToMdx = async (path, outputPath) => {
const html = await fs.readFile(path, 'utf-8').catch(cantOpenPath(path));
const mdx = await deserializeHTML(html).catch((err) => {
console.error('❌ error deserializing HTML');
console.error(err);
process.exit(1);
});
if (outputPath) {
fs.writeFile(outputPath, mdx, 'utf-8');
} else {
console.log(mdx);
}
};
const serde = program
.name('serde')
.description('manually serialize or deserialize an MDX or HTML file');
serde
.command('serialize')
.description('serialize an MDX file to HTML')
.option('-o, --output [path]', 'write to given path instead of stdout')
.argument('[path]')
.action((path, options) => {
assertPathIsNotEmpty(path);
serializeMdxToHtml(path, options.output);
});
serde
.command('deserialize')
.description('deserialize an HTML file to MDX')
.option('-o, --output [path]', 'write to given path instead of stdout')
.argument('[path]')
.action((path, options) => {
assertPathIsNotEmpty(path);
deserializeHtmlToMdx(path, options.output);
});
program.parse();