1
+ import repl from 'node:repl' ;
2
+
1
3
import { frodo } from '@rockcarver/frodo-lib' ;
2
- import fuzzy from 'fuzzy' ;
3
- import inquirer from 'inquirer' ;
4
- import inquirerPrompt from 'inquirer-autocomplete-prompt' ;
4
+ import { Option } from 'commander' ;
5
+ import vm from 'vm' ;
5
6
6
7
import * as s from '../../help/SampleData' ;
7
8
import { getTokens } from '../../ops/AuthenticateOps' ;
8
- import { printError , printMessage } from '../../utils/Console' ;
9
9
import { FrodoCommand } from '../FrodoCommand' ;
10
10
11
- const exits = [ 'exit' , 'quit' , 'q' ] ;
12
- const functions = frodo . utils . json . getPaths ( frodo , 'this.' ) ;
11
+ async function startRepl ( allowAwait = false ) {
12
+ const baseConfig = {
13
+ prompt : '> ' ,
14
+ ignoreUndefined : true ,
15
+ useGlobal : true ,
16
+ } ;
17
+
18
+ const configWithoutAwait = {
19
+ ...baseConfig ,
20
+ eval : async function myEval ( cmd , context , _filename , callback ) {
21
+ callback ( null , await vm . runInNewContext ( cmd , context ) ) ;
22
+ } ,
23
+ } ;
13
24
14
- function searchFunctions ( _answers , input = '' ) {
15
- return new Promise ( ( resolve ) => {
16
- setTimeout (
17
- ( ) => {
18
- const results = fuzzy . filter ( input , functions ) . map ( ( el ) => el . original ) ;
19
- // results.splice(5, 0, new inquirer.Separator());
20
- // results.push(new inquirer.Separator());
21
- resolve ( results ) ;
22
- } ,
23
- Math . random ( ) * 470 + 30
24
- ) ;
25
- } ) ;
25
+ const replServer = repl . start ( allowAwait ? baseConfig : configWithoutAwait ) ;
26
+
27
+ replServer . context . frodoLib = frodo ;
26
28
}
27
29
28
30
export default function setup ( ) {
@@ -41,6 +43,12 @@ export default function setup() {
41
43
` Launch a frodo shell using a connection profile (identified by a unique substring of the AM base URL):\n` +
42
44
` $ frodo shell ${ s . connId } \n` [ 'brightCyan' ]
43
45
)
46
+ . addOption (
47
+ new Option (
48
+ '--allow-await' ,
49
+ 'Allows top-level awaits to be used in the shell.'
50
+ )
51
+ )
44
52
. action ( async ( host , realm , user , password , options , command ) => {
45
53
command . handleDefaultArgsAndOpts (
46
54
host ,
@@ -51,32 +59,7 @@ export default function setup() {
51
59
command
52
60
) ;
53
61
if ( host ) await getTokens ( ) ;
54
- let exit = false ;
55
- do {
56
- try {
57
- inquirer . registerPrompt ( 'autocomplete' , inquirerPrompt ) ;
58
- const response = await inquirer . prompt ( [
59
- {
60
- type : 'autocomplete' ,
61
- prefix : '' ,
62
- name : 'command' ,
63
- message : '>' ,
64
- source : searchFunctions ,
65
- suggestOnly : true ,
66
- } ,
67
- ] ) ;
68
- exit = exits . includes ( response . command ) ;
69
- // evaluate code with context
70
- if ( ! exit ) {
71
- const result = await function ( str : string ) {
72
- return eval ( str ) ;
73
- } . call ( frodo , `${ response . command } ` ) ;
74
- printMessage ( result , 'data' ) ;
75
- }
76
- } catch ( error ) {
77
- printError ( error ) ;
78
- }
79
- } while ( ! exit ) ;
62
+ startRepl ( options . allowAwait ) ;
80
63
} ) ;
81
64
return program ;
82
65
}
0 commit comments