Skip to content

Commit

Permalink
feat: cli script
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Aug 12, 2023
1 parent e18919e commit 55b81f2
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

import fs from 'fs';
import {convertDocsHtmlToMarkdown} from './lib/convert.js';

// to avoid top-level async/await. readFileSync(0) caused strange errors
// https://stackoverflow.com/questions/67037353/any-way-to-read-from-stdin-synchronously-without-extra-modules-libraries-node-j
function stdinReadSync() {
if (process.stdin.isTTY) {
return '';
}

const b = Buffer.alloc(1024)
let data = ''

while (true) {
let n = 0

// Read while EAGAIN
while (true) {
try {
n = fs.readSync(process.stdin.fd, b, 0, b.length)
break
} catch (e) {
if (e.code === 'EAGAIN') {
// Sleep 100ms
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100)
continue
}
throw e
}
}

if (!n) break
data += b.toString('utf8', 0, n)
}

return data
}

// in order to debug this tool over the command line, you can read a file with broken input locally
// ex: fs.readFileSync('./local-file.html', 'utf8');

const inputHTML = stdinReadSync()

if(!inputHTML) {
console.error('No input HTML provided. Please pipe HTML to this command.');
process.exit(1);
}

convertDocsHtmlToMarkdown(inputHTML).then(markdown => {
process.stdout.write(markdown);
});

0 comments on commit 55b81f2

Please sign in to comment.