-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new runnable xhb binary (#294)
* feat: new runnable xhb binary * fix: chunks not being concatenated * feat: update readme with cli example
- Loading branch information
Showing
9 changed files
with
122 additions
and
21 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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,2 @@ | ||
#!/usr/bin/env node | ||
require('../dist/bin/xhb') |
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 @@ | ||
import Yargs from 'yargs' | ||
import { hideBin } from 'yargs/helpers' | ||
import { parse, serialize } from '../' | ||
|
||
const _collect = async (stream: NodeJS.ReadStream): Promise<Buffer> => { | ||
const chunks: Buffer[] = [] | ||
for await (const chunk of stream) { | ||
chunks.push(chunk) | ||
} | ||
return Buffer.concat(chunks) | ||
} | ||
|
||
const parseAction = async () => { | ||
const xhb = (await _collect(process.stdin)).toString('utf8') | ||
const object = parse(xhb) | ||
process.stdout.end(JSON.stringify(object)) | ||
} | ||
|
||
const serializeAction = async () => { | ||
const json = (await _collect(process.stdin)).toString('utf8') | ||
const xhb = serialize(JSON.parse(json)) | ||
process.stdout.end(xhb) | ||
} | ||
|
||
const bootstrap = () => { | ||
return Yargs(hideBin(process.argv), process.cwd()) | ||
.scriptName('xhb') | ||
.version() | ||
.command( | ||
'parse', | ||
'read xhb from stdin and write json to stdout', | ||
parseAction, | ||
) | ||
.command( | ||
'serialize', | ||
'read json from stdin and write xhb to stdout', | ||
serializeAction, | ||
) | ||
.help() | ||
.wrap(Yargs.terminalWidth() !== null ? Yargs.terminalWidth() : 80) | ||
.demandCommand() | ||
.recommendCommands() | ||
.strict() | ||
} | ||
|
||
const main = async () => { | ||
const program = bootstrap() | ||
await program.parseAsync() | ||
} | ||
|
||
main() |
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 |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
"outDir": "dist", | ||
"rootDir": "src", | ||
"noImplicitAny": false | ||
} | ||
}, | ||
"exclude": ["./dist/**/*"] | ||
} |
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