Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmaster2000 committed Aug 7, 2022
1 parent 716043a commit 717087d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 30 deletions.
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
# bun-repl [![GitHub version][github-image]][github-url] [![GitHub code size in bytes][size-image]][github-url] [![license][license-image]][license-url]



Experimental unofficial REPL for Bun
Experimental unofficial REPL for [Bun](https://github.com/oven-sh/bun)

Powered by Bun itself with the help of `swc` and JSC's `ShadowRealm` API

## Install
```
bun add -g bun-repl
```
> ⚠️ Due to workarounds for Bun-specific issues, installation on other package managers like `npm` or `yarn` will not work.
## Usage
```sh
bun run repl -- [options]
# or
bun-repl [options]
```
bun run repl
```
Pass the `-h` or `--help` CLI option for a list of all options.

Type `.help` within the REPL for a list of commands.

Press ``+`Enter` to travel up the execution history.

Press ``+`Enter` to travel back down.

## Features

* Seamless JavaScript & TypeScript execution
* Top level import syntax supported (`import fs from 'fs'`)
* Import either CommonJS or ESM into the REPL
* Import either CommonJS or ESM local files and packages into the REPL
* Node.js REPL special underscore variables provided (`_` and `_error`)
* Execution history (Arrow up/down)
* Execution history (`` ``)
* REPL Commands (`.command`)

## Known issues/limitations
## Known issues & limitations
Please keep in mind this is unofficial and experimental software built on top of experimental software, there **ARE** bugs. Additionally, Bun will obviously be getting an official native REPL in the future which certainly will be much better, this module is merely serving as a temporary alternative until then.

PRs are welcome to help fix any of the items below or anything else.

* Top level await is not supported.
* Reason: Usage of `eval()`
* The execution history (Arrow up/down) is buggy to navigate and doesn't support backspacing past the history entry.
* Multi-line inputs are not supported.
* Reason: Same as below.
* Pressing `` and `` to traverse the input with the cursor is not supported.
* Reason: Same as below.
* The execution history is buggy to navigate and doesn't support backspacing past the history entry.
* Reason: Bun's current lack of support for event-based streams limits us to `prompt()`, which provides little to no control over the input as its being written. This also makes it impossible to intercept keypresses/combinations.
* There is a space between the execution history entry and your appended code.
* Reason: `prompt()` automatically inserts a space at the end, with no way to turn it off. Yes, quite annoying.
Expand Down
2 changes: 1 addition & 1 deletion bun-repl-cli.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
bun "$BUN_INSTALL/install/global/node_modules/bun-repl/src/repl.ts"
bun "$BUN_INSTALL/install/global/node_modules/bun-repl/src/repl.ts" -- $*
2 changes: 2 additions & 0 deletions bun-repl-cli2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
bun "$BUN_INSTALL/install/global/node_modules/bun-repl/src/repl.ts" -- $*
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "bun-repl",
"version": "1.0.1",
"version": "1.0.2",
"description": "Experimental unofficial REPL for Bun",
"main": "src/repl.ts",
"scripts": {
Expand Down Expand Up @@ -33,6 +33,7 @@
"typescript": "^4.7.4"
},
"bin": {
"repl": "./bun-repl-cli.sh"
"repl": "./bun-repl-cli.sh",
"bun-repl": "./bun-repl-cli2.sh"
}
}
2 changes: 1 addition & 1 deletion src/realm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import util from 'util';
import path from 'path';
import swcrc from './swcrc';

/**
/**
* @typedef BunError
* @property {string} name
* @property {number} line
Expand Down
55 changes: 39 additions & 16 deletions src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import REPLState from './replstate';
import REPLHistory from './replhistory';
import './extendglobals';

const pkgjson = await Bun.file(path.join(import.meta.dir, '..', 'package.json')).json() as PackageJson;

const helpFlag = process.argv.includes('-h') || process.argv.includes('--help');
if (helpFlag) {
printCLIHelp();
process.exit(0);
}

const versionFlag = process.argv.includes('-v') || process.argv.includes('--version');
if (versionFlag) {
console.log(`bun-repl ${pkgjson.version}`);
process.exit(0);
}

const IS_DEBUG = process.argv.includes('--debug');
const debuglog = IS_DEBUG ? console.debug : () => void 0;

Expand All @@ -20,8 +34,6 @@ const replState = new REPLState();
const replHistory = new REPLHistory();
const transpiler = new Transpiler();

const pkgjson = await Bun.file(path.join(import.meta.dir, '..', 'package.json')).json() as PackageJson;

console.log(`Welcome to Bun.js ${process.version} (${process.revision ? 'canary-'+process.revision : 'release'})
Type ".help" for more information.`);

Expand Down Expand Up @@ -66,24 +78,36 @@ MainLoop: for (;;) {
debuglog('postAdjust:', transpiled.trim());
const evalIn = util.inspect(transpiled);
realm.evaluate(`
let $__SHADOWREALM_EVAL_RETURN_VALUE__;
try {
$__SHADOWREALM_EVAL_RETURN_VALUE__ = eval(${evalIn});
} catch (error) {
$__SHADOWREALM_EVAL_RETURN_VALUE__ = _error = error;
}
if ($__SHADOWREALM_EVAL_RETURN_VALUE__ !== _error) _ = $__SHADOWREALM_EVAL_RETURN_VALUE__;
globalThis['@@replFmt']($__SHADOWREALM_EVAL_RETURN_VALUE__);`);
let $__SHADOWREALM_EVAL_RETURN_VALUE__;
try {
$__SHADOWREALM_EVAL_RETURN_VALUE__ = eval(${evalIn});
} catch (error) {
$__SHADOWREALM_EVAL_RETURN_VALUE__ = _error = error;
}
if ($__SHADOWREALM_EVAL_RETURN_VALUE__ !== _error) _ = $__SHADOWREALM_EVAL_RETURN_VALUE__;
globalThis['@@replFmt']($__SHADOWREALM_EVAL_RETURN_VALUE__);`);
}

function printHelp(): void {
console.log(`Commands:
.help = Print this message.
.info = Print REPL information.
.clear = Clear the screen.
.exit = Exit the REPL.
.help = Print this message.
.info = Print REPL information.
.clear = Clear the screen.
.exit = Exit the REPL.
Press Ctrl+C or Ctrl+Z to forcefully terminate the REPL.`);
}

function printCLIHelp(): void {
console.log(`Usage: bun-repl [options]
Press Ctrl+C to forcefully terminate the REPL.`);
Options:
-h, --help = Display this message.
-v, --version = Show installed bun-repl version.
--debug = Print debug information while running.
Environment variables:
None`);
}

function printInfo(): void {
Expand All @@ -93,5 +117,4 @@ function printInfo(): void {
Color mode: ${Bun.enableANSIColors}
Debug mode: ${IS_DEBUG}
Current session uptime: ${prettyms(performance.now())}`);

}
2 changes: 1 addition & 1 deletion src/swcrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const swcrc: swc.Options = {
transform: {
useDefineForClassFields: false,
treatConstEnumAsEnum: false,
optimizer: {
optimizer: {
simplify: false,
globals: { vars: {
$__SHADOWREALM_EVAL_RETURN_VALUE__: '$__SHADOWREALM_EVAL_RETURN_VALUE___'
Expand Down

0 comments on commit 717087d

Please sign in to comment.