1
1
/* jshint esversion:11 */
2
2
3
+ import path from "node:path" ;
3
4
import {
4
5
readFile ,
5
6
} from "node:fs/promises" ;
@@ -18,11 +19,7 @@ import {
18
19
}
19
20
from "../jsds/jsds.mjs" ;
20
21
21
- function default_callback ( candidate ) {
22
- console . log ( candidate . toString ( ) ) ;
23
- }
24
-
25
- function * search ( input_strings , buffer_limit = 1024 , callback = default_callback ) {
22
+ function * search ( input_strings , buffer_limit , callback ) {
26
23
buffer_size ( buffer_limit ) ;
27
24
28
25
let rules = { } ;
@@ -100,45 +97,54 @@ async function read_file_to_string_array(file_path) {
100
97
return results ;
101
98
}
102
99
100
+ function default_callback ( candidate ) {
101
+ console . log ( candidate . toString ( ) ) ;
102
+ }
103
+
103
104
async function main ( ) {
104
- const help_message = "Usage: node engine.mjs <file_path> <buffer_limit> [callback_module ]" ;
105
+ const help_message = "Usage: node engine.mjs <file_path> <buffer_limit> [config_module ]" ;
105
106
if ( argv . length != 4 && argv . length != 5 ) {
106
107
console . error ( help_message ) ;
107
108
exit ( 1 ) ;
108
109
}
109
110
const file_path = argv [ 2 ] ;
110
111
const buffer_limit = parseInt ( argv [ 3 ] , 10 ) ;
111
112
let callback ;
113
+ let await_each_step ;
112
114
if ( argv . length == 5 ) {
113
115
try {
114
- const callback_module = await import ( argv [ 4 ] ) ;
115
- if ( typeof callback_module . default === "function" ) {
116
- callback = callback_module . default ;
117
- } else {
118
- console . error ( "Callback module must export a default function." ) ;
119
- exit ( 1 ) ;
120
- }
116
+ const config_module = await import ( path . resolve ( argv [ 4 ] ) ) ;
117
+ callback = config_module . callback ;
118
+ await_each_step = config_module . await_each_step ;
121
119
} catch ( error ) {
122
120
console . error ( `Error importing callback module: ${ error . message } ` ) ;
123
121
exit ( 1 ) ;
124
122
}
125
123
} else {
126
124
callback = default_callback ;
125
+ await_each_step = true ;
127
126
}
128
127
129
128
const data = await read_file_to_string_array ( file_path ) ;
129
+ for ( const input of data ) {
130
+ callback ( new rule_t ( input ) ) ;
131
+ }
130
132
const generator = search ( data , buffer_limit , callback ) ;
131
133
const handle = createInterface ( {
132
134
input : stdin ,
133
135
output : stdout
134
136
} ) ;
137
+ console . log ( "Search starting..." ) ;
135
138
while ( true ) {
136
139
if ( generator . next ( ) . done ) {
137
140
console . log ( "Search completed." ) ;
138
141
break ;
139
142
}
140
- await handle . question ( "Press Enter to continue..." ) ;
143
+ if ( await_each_step ) {
144
+ await handle . question ( "Press Enter to continue..." ) ;
145
+ }
141
146
}
147
+ exit ( 0 ) ;
142
148
}
143
149
144
150
await main ( ) ; // jshint ignore:line
0 commit comments