Comparing version 2.0.9 to 2.1.0
{ | ||
"type": "module", | ||
"name": "bun-repl", | ||
"version": "2.0.9", | ||
"version": "2.1.0", | ||
"description": "Experimental REPL for Bun", | ||
@@ -27,3 +27,3 @@ "main": "src/module/repl.ts", | ||
"dependencies": { | ||
"@swc/core": "^1.3.68" | ||
"@swc/core": "^1.3.90" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
@@ -645,2 +645,3 @@ import { type JSC } from 'bun-devtools'; | ||
else debuglog(`REPL history data loaded: (${history.lines.length} lines) ${history.path}`); | ||
const sessionHistoryNoErrors: string[] = []; | ||
@@ -766,9 +767,10 @@ if (!process.stdin.isTTY) { | ||
}); | ||
rl.on('line', async line => { | ||
rl.on('line', async (line, { loadingFile = false } = {}) => { | ||
line = StringTrim(line); | ||
if (!line) return rl.prompt(); | ||
if ( | ||
(line[0] === '.' && multiline.lines.length === 0 && !RegExpTest(/\d/, line[1])) | ||
!loadingFile && (line[0] === '.' && multiline.lines.length === 0 && !RegExpTest(/\d/, line[1])) | ||
) { | ||
switch (line) { | ||
const replCmd = StringPrototypeSplit(line, /\s+/g); | ||
switch (replCmd[0]) { | ||
case '.help': { | ||
@@ -780,2 +782,4 @@ console.log( | ||
` .multiline Toggle multi-line mode. ${$.gray}(${metaKey('M').trim()})${$.reset}\n` + | ||
` .save Save all successful commands evaluated in this REPL session to a file.\n` + | ||
` .load Load a file into the REPL session. ${$.yellow}(Experimental)${$.reset}\n` + | ||
` .clear Clear the screen. ${$.gray}(Ctrl+L)${$.reset}\n` + | ||
@@ -805,2 +809,38 @@ ` .exit Exit the REPL. ${$.gray}(Ctrl+C / Ctrl+D)${$.reset}\n` + | ||
} break; | ||
case '.load': { | ||
const filepath = replCmd[1]; | ||
if (!filepath) { | ||
console.log(`${$.red}Missing argument, usage: ${$.whiteBright}.load <filepath>${$.reset}`); | ||
break; | ||
} | ||
const resolved = pathresolve(filepath); | ||
try { | ||
const contents = readFileSync(resolved, 'utf8'); | ||
console.warn(`${$.yellow+$.dim}[!] Loading files into the REPL is experimental and may not work as expected.${$.reset}`); | ||
rl.emit('line', contents, { loadingFile: true }); | ||
} catch (e) { | ||
const err = e as Error; | ||
console.error(`Failed to load file ${$.cyan}${resolved}${$.red}: ${err.message ?? 'Unknown error'}${$.reset}`); | ||
if (IS_DEBUG) console.error(err); | ||
break; | ||
} | ||
} break; | ||
case '.save': { | ||
const filepath = replCmd[1]; | ||
if (!filepath) { | ||
console.log(`${$.red}Missing argument, usage: ${$.whiteBright}.save <filepath>${$.reset}`); | ||
break; | ||
} | ||
const resolved = pathresolve(filepath); | ||
const contents = ArrayPrototypeJoin(sessionHistoryNoErrors, '\n'); | ||
try { | ||
await write(resolved, contents); | ||
} catch (e) { | ||
const err = e as Error; | ||
console.error(`Failed to save REPL session to ${$.cyan}${resolved}${$.red}: ${err.message ?? 'Unknown error'}${$.reset}`); | ||
if (IS_DEBUG) console.error(err); | ||
break; | ||
} | ||
console.log(`Saved REPL session to: ${$.cyan}${resolved}${$.reset+$.dim} (${contents.length} chars)${$.reset}`); | ||
} break; | ||
case '.clear': { | ||
@@ -820,3 +860,3 @@ console.clear(); | ||
} else { | ||
if (multiline.enabled) { | ||
if (multiline.enabled && !loadingFile) { | ||
if (line[line.length - 1] === '.') { | ||
@@ -838,4 +878,7 @@ ArrayPush(multiline.lines, StringPrototypeSlice(line, 0, -1)); | ||
} | ||
const backupPrompt = rl.getPrompt(); | ||
if (loadingFile) rl.setPrompt(''); | ||
let code: string = line; | ||
try { | ||
if (RegExpTest(/^\s*{/, code) && !RegExpTest(/;\s*$/, code)) code = `(${code})`; | ||
code = transpiler.preprocess(code); | ||
@@ -873,4 +916,11 @@ code = transpiler.transpile(code); | ||
if (extraInfo.errored) console.error(evaled); | ||
else console.log(evaled); | ||
} else if (!extraInfo.errored) console.log(evaled); | ||
else { | ||
console.log(evaled); | ||
ArrayPush(sessionHistoryNoErrors, line); | ||
} | ||
} else if (!extraInfo.errored) { | ||
console.log(evaled); | ||
ArrayPush(sessionHistoryNoErrors, line); | ||
} | ||
if (loadingFile) rl.setPrompt(backupPrompt); | ||
} | ||
@@ -877,0 +927,0 @@ rl.prompt(); |
Sorry, the diff of this file is not supported yet
291275
4354
Updated@swc/core@^1.3.90