Skip to content

Commit

Permalink
update cli to be able to use stdin and stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBuhler committed Jul 13, 2023
1 parent c440b82 commit c0de600
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
5 changes: 5 additions & 0 deletions ts/bin/razuberi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

script="$(dirname "$0")"/../dist/cli/razuberi.js

node $script "$@"
3 changes: 0 additions & 3 deletions ts/bin/razuberi.js

This file was deleted.

4 changes: 2 additions & 2 deletions ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@razuberi/transpiler",
"version": "0.1.0",
"version": "0.1.1",
"description": "A JavaScript/TypeScript -> C++ transpiler",
"keywords": [
"razuberi",
Expand Down Expand Up @@ -28,7 +28,7 @@
],
"main": "dist/index.js",
"bin": {
"razuberi": "./bin/razuberi.js"
"razuberi": "./bin/razuberi"
},
"repository": {
"type": "git",
Expand Down
53 changes: 35 additions & 18 deletions ts/src/cli/razuberi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,55 @@ import * as path from 'node:path'

import { ArgumentParser } from 'argparse'

const usage = `razuberi [-h | --help]
razuberi --stdin [[-o | --out] <outputFile>]
razuberi <inputFile> [[-o | --out] <outputFile>]`

const argParser = new ArgumentParser({
description: 'Razuberi: a JavaScript -> TypeScript transpiler.',
prog: 'razuberi',
usage,
})

argParser.add_argument('inputFilename', {
help: 'a JavaScript/TypeScript file to transpile. only one <input> file is supported at this time.',
metavar: '<input>',
const argGroup = argParser.add_mutually_exclusive_group({ required: true })

argGroup.add_argument('inputFile', {
help: 'the input file to transpile. pass "--stdin" to read from `stdin`.',
metavar: '<inputFile>',
nargs: '?',
type: (arg: string) => path.join(process.cwd(), arg),
})

argGroup.add_argument('--stdin', {
action: 'store_true',
help: 'read from stdin',
})

argParser.add_argument('-o', '--out', {
default: 'a.cpp',
dest: 'outputFilename',
help: 'write output C++ to <file>. defaults to `a.cpp`',
metavar: '<file>',
dest: 'outputFile',
help: 'write C++ output to this file. defaults to `stdout`.',
metavar: '<outputFile>',
nargs: '?',
type: (arg: string) => path.join(process.cwd(), arg),
})

const {
inputFilename,
outputFilename,
} = argParser.parse_args()

const { readFile } = await import('node:fs/promises')
const args = argParser.parse_args()

const inputFileContents = await readFile(inputFilename, 'utf-8')
let inputFileContents: string
if (args.stdin) {
const { readFileSync } = await import('node:fs')
inputFileContents = readFileSync(process.stdin.fd, 'utf-8')
} else {
const { readFile } = await import('node:fs/promises')
inputFileContents = await readFile(args.inputFile, 'utf-8')
}

const { transpile } = await import('../lib/transpile.js')

const outputFileContents = transpile(inputFileContents)

const { writeFile } = await import('node:fs/promises')

await writeFile(outputFilename, outputFileContents)
if (args.outputFile) {
const { writeFile } = await import('node:fs/promises')
await writeFile(args.outputFile, outputFileContents)
} else {
process.stdout.write(outputFileContents)
}
2 changes: 1 addition & 1 deletion ts/test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
cd "$(dirname "$0")"

set -x
../bin/razuberi.js $1.ts -o $1.cpp
../bin/razuberi $1.ts -o $1.cpp
indent $1.cpp
g++ -c -I ../../cpp/out/include $1.cpp -o $1.o
g++ -L ../../cpp/out/lib -l razuberi $1.o -o $1
Expand Down

0 comments on commit c0de600

Please sign in to comment.