Comparing version 1.0.2 to 1.0.3
@@ -66,3 +66,5 @@ { | ||
"@typescript-eslint/unbound-method": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/no-non-null-assertion": "off", | ||
"@typescript-eslint/prefer-namespace-keyword": "off", | ||
"@typescript-eslint/adjacent-overload-signatures": "off", | ||
@@ -69,0 +71,0 @@ "@typescript-eslint/no-inferrable-types": ["warn", { "ignoreParameters": true, "ignoreProperties": true }], |
{ | ||
"type": "module", | ||
"name": "bun-repl", | ||
"version": "1.0.2", | ||
"description": "Experimental unofficial REPL for Bun", | ||
"main": "src/repl.ts", | ||
"scripts": { | ||
"start": "bun run src/repl.ts --", | ||
"test": "bun run --silent check && bun run --silent start", | ||
"deeptest": "bun run --silent lint && bun run --silent test", | ||
"check": "bun tsc", | ||
"lint": "bun eslint src/**/*.ts", | ||
"lint-fix": "bun run --silent lint -- --fix" | ||
}, | ||
"author": "jhmaster", | ||
"license": "MIT", | ||
"homepage": "https://www.npmjs.com/package/bun-repl", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jhmaster2000/bun-repl.git" | ||
}, | ||
"keywords": ["bun", "repl", "cli", "ts", "js"], | ||
"dependencies": { | ||
"@swc/core": "^1.2.224", | ||
"pretty-ms": "^8.0.0" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^5.32.0", | ||
"@typescript-eslint/parser": "^5.32.0", | ||
"bun-types": "^0.1.5", | ||
"eslint": "^8.21.0", | ||
"eslint-plugin-unicorn": "^43.0.2", | ||
"typescript": "^4.7.4" | ||
}, | ||
"bin": { | ||
"repl": "./bun-repl-cli.sh", | ||
"bun-repl": "./bun-repl-cli2.sh" | ||
} | ||
} | ||
"type": "module", | ||
"name": "bun-repl", | ||
"version": "1.0.3", | ||
"description": "Experimental unofficial REPL for Bun", | ||
"main": "src/module/repl.ts", | ||
"scripts": { | ||
"start": "bun run src/repl.ts -- --", | ||
"test": "bun run --silent check && bun run --silent start", | ||
"deeptest": "bun run --silent lint && bun run --silent test", | ||
"check": "bun tsc", | ||
"lint": "bun eslint src/**/*.ts", | ||
"lint-fix": "bun run --silent lint -- --fix" | ||
}, | ||
"author": "jhmaster", | ||
"license": "MIT", | ||
"homepage": "https://github.com/jhmaster2000/bun-repl#readme", | ||
"bugs": { | ||
"url": "https://github.com/jhmaster2000/bun-repl/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jhmaster2000/bun-repl.git" | ||
}, | ||
"keywords": ["bun", "repl", "cli", "ts", "js"], | ||
"dependencies": { | ||
"@swc/core": "^1.2.224", | ||
"pretty-ms": "^8.0.0" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^5.32.0", | ||
"@typescript-eslint/parser": "^5.32.0", | ||
"bun-types": "^0.1.5", | ||
"eslint": "^8.21.0", | ||
"eslint-plugin-unicorn": "^43.0.2", | ||
"typescript": "^4.7.4" | ||
}, | ||
"bin": { | ||
"repl": "./bun-repl-cli.sh", | ||
"bun-repl": "./bun-repl-cli2.sh" | ||
} | ||
} |
@@ -33,2 +33,3 @@ # bun-repl [![GitHub version][github-image]][github-url] [![GitHub code size in bytes][size-image]][github-url] [![license][license-image]][license-url] | ||
* Node.js REPL special underscore variables provided (`_` and `_error`) | ||
* Node.js `repl` module polyfill | ||
* Execution history (`↑` `↓`) | ||
@@ -54,4 +55,2 @@ * REPL Commands (`.command`) | ||
* Reason: Usage of `eval()` which has its own lexical scope. | ||
* Some modules fail to resolve their imports within the REPL, such as `fs/promises`. | ||
* Reason: Unknown... | ||
@@ -58,0 +57,0 @@ [github-url]:https://github.com/jhmaster2000/bun-repl |
@@ -15,2 +15,9 @@ declare global { | ||
interface BunError { | ||
name: string; | ||
message: string; | ||
line: number; | ||
column: number; | ||
} | ||
interface ResolveError { | ||
@@ -21,3 +28,3 @@ referrer?: string; | ||
specifier?: string; | ||
importKind?: 'stmt'; | ||
importKind?: string; | ||
position?: number; | ||
@@ -30,20 +37,37 @@ convertToType?: string; | ||
interface PackageJson { | ||
type?: "module" | "commonjs", | ||
name: string, | ||
version: string, | ||
description?: string, | ||
main?: "src/repl.ts", | ||
scripts?: Record<string, string>, | ||
author?: string, | ||
license?: string, | ||
dependencies?: Record<string, string>, | ||
devDependencies?: Record<string, string>, | ||
type?: "module" | "commonjs"; | ||
name: string; | ||
version: string; | ||
description?: string; | ||
main?: string; | ||
author?: string; | ||
license?: string; | ||
homepage?: string; | ||
scripts?: Record<string, string>; | ||
dependencies?: Record<string, string>; | ||
devDependencies?: Record<string, string>; | ||
bin?: Record<string, string>; | ||
keywords?: string[]; | ||
bugs?: { | ||
url: string; | ||
} | ||
repository?: { | ||
type: string; | ||
url: string; | ||
} | ||
} | ||
interface ImportMeta { | ||
require: (moduleIdentifier: string) => unknown; | ||
} | ||
function require(moduleIdentifier: string): unknown; | ||
} | ||
ShadowRealm.prototype.execFile = async function (filepath) { | ||
try { await this.importValue(filepath, ''); } catch { void 0; } | ||
try { await this.importValue(filepath, ''); } catch (err) { | ||
if ((<Error>err).message.trim() !== '%ShadowRealm%.importValue requires |exportName| to exist in the |specifier|') | ||
throw err; // Unexpected error should not be suppressed. | ||
} | ||
}; | ||
export { }; |
#!/usr/bin/env bun | ||
import './extendglobals'; | ||
import $ from './colors'; | ||
import swc from '@swc/core'; | ||
@@ -10,6 +12,5 @@ import util from 'util'; | ||
import REPLHistory from './replhistory'; | ||
import './extendglobals'; | ||
import pkgjson from './pkgjson'; | ||
import { debuglog, IS_DEBUG } from './debug'; | ||
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'); | ||
@@ -27,5 +28,24 @@ if (helpFlag) { | ||
const IS_DEBUG = process.argv.includes('--debug'); | ||
const debuglog = IS_DEBUG ? console.debug : () => void 0; | ||
interface PartialNPMResponse { | ||
'dist-tags': { | ||
latest: string | ||
} | ||
} | ||
if (process.argv.includes('--update')) { | ||
console.log(`${$.dim}Checking for updates...${$.reset}`); | ||
const res: PartialNPMResponse = await (await fetch('https://registry.npmjs.org/bun-repl', { | ||
headers: { Accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*' } | ||
})).json(); | ||
const { latest } = res['dist-tags']; | ||
if (pkgjson.version === latest) { | ||
console.log(`${$.gray}No updates found. You are already on the latest version of bun-repl (${$.greenBright+latest+$.gray})${$.reset}`); | ||
process.exit(0); | ||
} else { | ||
console.log(`${$.whiteBright}Updates found: ${$.yellow+$.bold+pkgjson.version+$.whiteBright} -> ${$.greenBright+latest+$.reset} | ||
${$.whiteBright}Run "${$.cyanBright}bun add -g bun-repl${$.whiteBright}" to update.${$.reset}`); | ||
process.exit(0); | ||
} | ||
} | ||
const realm = new ShadowRealm(); | ||
@@ -41,2 +61,3 @@ // Inject realm globals | ||
Type ".help" for more information.`); | ||
debuglog(`${$.dim}INFO: Debug mode enabled.${$.reset}`); | ||
@@ -71,3 +92,3 @@ MainLoop: for (;;) { | ||
let transpiled: string = transpiler.preAdjust(userInput); | ||
debuglog('preAdjust:', transpiled.trim()); | ||
debuglog($.dim+'preprocess:', transpiled.trim()+$.reset); | ||
try { | ||
@@ -80,15 +101,30 @@ transpiled = transpiler.transpile(transpiled); | ||
} | ||
debuglog('transpile:', transpiled.trim()); | ||
debuglog($.dim+'transpile:', transpiled.trim()+$.reset); | ||
transpiled = transpiler.postAdjust(transpiled); | ||
debuglog('postAdjust:', transpiled.trim()); | ||
const evalIn = util.inspect(transpiled); | ||
realm.evaluate(` | ||
let $__SHADOWREALM_EVAL_RETURN_VALUE__; | ||
debuglog($.dim+'postprocess:', transpiled.trim()+$.reset); | ||
try { | ||
$__SHADOWREALM_EVAL_RETURN_VALUE__ = eval(${evalIn}); | ||
const evalIn = util.inspect(transpiled); | ||
realm.evaluate(`//'use strict'; (Strict mode currently causes issues that need fixing) | ||
let $__SHADOWREALM_EVAL_RETURN_VALUE__ = []; | ||
try { | ||
$__SHADOWREALM_EVAL_RETURN_VALUE__[0] = (async function*() {}).constructor['@@REPLGlobal'].eval(${evalIn}); | ||
} catch (error) { | ||
$__SHADOWREALM_EVAL_RETURN_VALUE__[0] = _error = error; | ||
$__SHADOWREALM_EVAL_RETURN_VALUE__[1] = true; // isError | ||
} | ||
if (!$__SHADOWREALM_EVAL_RETURN_VALUE__[1]) _ = $__SHADOWREALM_EVAL_RETURN_VALUE__[0]; | ||
const REPLGlobal = (async function*(){}).constructor['@@REPLGlobal']; | ||
const [val, err] = REPLGlobal.format($__SHADOWREALM_EVAL_RETURN_VALUE__[0], $__SHADOWREALM_EVAL_RETURN_VALUE__[1]); | ||
if (err) { | ||
REPLGlobal.console.error('Fatal REPL Formatting Error:'); | ||
REPLGlobal.console.error(err); | ||
REPLGlobal.console.error('This is most likely a bug, please report it at ${pkgjson.bugs?.url ?? 'the project\'s GitHub repository'}'); | ||
} else REPLGlobal.console.log(val); | ||
`); | ||
} catch (error) { | ||
$__SHADOWREALM_EVAL_RETURN_VALUE__ = _error = error; | ||
console.error('Fatal REPL Evaluation Error:'); | ||
console.error(error); | ||
console.error(`This is most likely a bug, please report it at ${pkgjson.bugs?.url ?? 'the project\'s GitHub repository'}`); | ||
} | ||
if ($__SHADOWREALM_EVAL_RETURN_VALUE__ !== _error) _ = $__SHADOWREALM_EVAL_RETURN_VALUE__; | ||
globalThis['@@replFmt']($__SHADOWREALM_EVAL_RETURN_VALUE__);`); | ||
} | ||
@@ -112,2 +148,3 @@ | ||
-v, --version = Show installed bun-repl version. | ||
--update = Check for bun-repl updates. | ||
--debug = Print debug information while running. | ||
@@ -122,2 +159,3 @@ | ||
Installed at: ${path.join(import.meta.dir, '..')} | ||
Bun version: ${process.version} | ||
SWC version: ${swc.version as string} | ||
@@ -124,0 +162,0 @@ Color mode: ${Bun.enableANSIColors} |
@@ -31,3 +31,3 @@ import swc from '@swc/core'; | ||
globals: { vars: { | ||
$__SHADOWREALM_EVAL_RETURN_VALUE__: '$__SHADOWREALM_EVAL_RETURN_VALUE___' | ||
repl: '(async function*() {}).constructor["@@REPLGlobal"].REPL' | ||
} } | ||
@@ -34,0 +34,0 @@ } |
@@ -36,3 +36,3 @@ import swc from '@swc/core'; | ||
const match = variable.match(/[^ \n\t]+[ \n\t]+as[ \n\t]+([^ \n\t]+)/); | ||
str += `void ${match ? match[1] : variable}`; | ||
str += `void ${match ? match[1] : variable};`; | ||
}); | ||
@@ -53,7 +53,21 @@ return str; | ||
if (info.varname) str += `var ${info.varname} = ${requireVar};`; | ||
if (info.destructuredVars) info.destructuredVars.forEach(variable => { | ||
const match = variable.match(/([^ \n\t]+)[ \n\t]+as[ \n\t]+([^ \n\t]+)/); | ||
if (match) str += `var ${match[2]} = ${requireVar}.${match[1]};`; | ||
else str += `var ${variable} = ${requireVar}.${variable};`; | ||
}); | ||
if (info.destructuredVars) { | ||
let ifstr = 'if(!('; | ||
let delstr = ')){'; | ||
info.destructuredVars.forEach(variable => { | ||
let exportStr = variable; | ||
const match = variable.match(/([^ \n\t]+)[ \n\t]+as[ \n\t]+([^ \n\t]+)/); | ||
if (match) { | ||
variable = match[2]; | ||
exportStr = match[1]; | ||
str += `var { ${exportStr}: ${variable} } = ${requireVar};`; | ||
} else str += `var { ${variable} } = ${requireVar};`; | ||
ifstr += `((async function*(){}).constructor['@@REPLGlobal'].temp.$v="${exportStr}") in ${requireVar}&&`; | ||
delstr += `delete (async function*(){}).constructor['@@REPLGlobal'].global["${variable}"];`; | ||
}); | ||
const errmsg = `The requested module '${requireStr}' does not provide an export named '\${(async function*(){}).constructor['@@REPLGlobal'].temp.$v}'`; | ||
delstr += `throw new (async function*(){}).constructor['@@REPLGlobal'].Error(\`${errmsg}\`);};`; | ||
ifstr = ifstr.slice(0, -2); | ||
str += ifstr + delstr; | ||
} | ||
return str + '\n'; | ||
@@ -60,0 +74,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
105927
20
827
0
60