-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteractive.js
More file actions
199 lines (170 loc) · 5.24 KB
/
interactive.js
File metadata and controls
199 lines (170 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env node
const repl = require('repl');
const readline = require('readline');
const fs = require('fs');
const path = require('path');
// Add our extensions for the REPL
const { addExtensions, colors, color, last, pop, first, compact, get } = require('./extensions');
const { evaluateChain } = require('./evaluator');
// Add extensions to String and Array prototypes
addExtensions();
// Create a simple test data array
let testData = [
"file1.txt",
"document.pdf",
"image.png",
"data.json",
"script.js"
];
// Add REPL-specific helper functions
function help() {
console.log(`
js-stream-tool Interactive Mode
===============================
Commands:
.help - Show this help
.data - Show current test data
.test(str) - Apply operations to test data (e.g., .test('.toUpperCase()'))
.add(str) - Add string to test data
.clear - Clear test data
.sample - Load sample data
.exit or .quit - Exit interactive mode
Examples:
.test('.color("red")') - Color test data red
.test('.includes("json")') - Filter for JSON files
.test('.split(".").pop()') - Get file extensions
.test('.when(str => str.length > 8, str => str.toUpperCase())') - Uppercase longer strings
`);
}
function test(chain) {
console.log('Input data:', testData);
console.log('Applying chain:', chain);
console.log('Results:');
for (const item of testData) {
try {
// Use the same evaluation logic as the main application
const result = evaluateChain(item, chain);
console.log(' ', result);
} catch (e) {
console.log(' Error:', e.message);
}
}
}
function add(str) {
testData.push(str);
console.log(`Added "${str}" to test data`);
}
function data() {
console.log('Current test data:', testData);
}
function clear() {
testData = [];
console.log('Test data cleared');
}
function sample() {
testData = [
"README.md",
"package.json",
"src/index.js",
"tests/basic.test.js",
"docs/guide.md",
"2023-10-15.log",
"backup.tar.gz"
];
console.log('Loaded sample data');
}
// Check if data is being piped in
const isPiped = !process.stdin.isTTY;
let alreadyStarted = false;
if (isPiped) {
console.log('Reading piped input...');
let inputLines = [];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
inputLines.push(line);
if (inputLines.length >= 3) {
rl.close();
}
});
rl.on('close', () => {
// Use piped data if available, otherwise use default
if (inputLines.length > 0) {
testData = inputLines;
console.log('(Using piped input as test data)');
} else {
console.log('(No piped input received, using default data)');
}
if (!alreadyStarted) {
alreadyStarted = true;
startRepl();
}
});
// Set a timeout in case no data is provided
setTimeout(() => {
if (inputLines.length === 0 && !alreadyStarted) {
console.log('(No piped input received, using default data)');
alreadyStarted = true;
startRepl();
}
}, 1000);
} else {
// No piped data, start with default data
startRepl();
}
function startRepl() {
console.log('js-stream-tool Interactive Mode');
console.log('===============================');
console.log('Type ".help" for available commands');
console.log('');
// Create REPL server
const r = repl.start({
prompt: 'js> ',
useGlobal: true
});
// Add custom commands to the REPL context
r.context.help = help;
r.context.test = test;
r.context.add = add;
r.context.data = data;
r.context.clear = clear;
r.context.sample = sample;
// Add a function for testing chains (more flexible than REPL commands)
r.context.t = r.context.test = test;
// Add a function to run chains similar to js command
r.context.run = function(chain) {
console.log('Applying chain:', chain);
for (const item of testData) {
try {
const result = evaluateChain(item, chain);
console.log(' ', result);
} catch (e) {
console.log(' Error:', e.message);
}
}
};
// Add the extensions to the REPL context
r.context.String = String;
r.context.Array = Array;
// Add a function to simulate the main js functionality
r.context.js = function(chain) {
console.log('Applying chain:', chain);
for (const item of testData) {
try {
const result = evaluateChain(item, chain);
console.log(' ', result);
} catch (e) {
console.log(' Error:', e.message);
}
}
};
// Add the evaluateChain function to the context too
r.context.evaluateChain = evaluateChain;
// Welcome message
r.on('reset', () => {
console.log('js-stream-tool REPL - Type ".help" for commands');
});
}