-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdev-tail.js
More file actions
142 lines (115 loc) · 3.65 KB
/
dev-tail.js
File metadata and controls
142 lines (115 loc) · 3.65 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
/**
* node bash/fs/dev-tail.mjs var/tail.log var/server_js_https.log var/server_js_http.log
*
* This script will attach and do 'tail -f var/tail.log' continuously
* and also on every keystroke will dumpt to process.stdout.write() content of any file after the first argument
* in our case these are:
* var/server_js_https.log
* var/server_js_http.log
*
* node bash/fs/dev-tail.mjs var/tail.log var/server_js_https.log var/server_js_http.log --flag <flag_value>
* this flag will be ignored but it useful to find process by custom flag with ps aux
*/
import readline from "readline";
import { promises as fsPromises, watch, createReadStream, statSync } from "fs";
import fss from "fs";
import { resolve } from "path";
import path from "path";
import { fileURLToPath } from "url";
// handle live 'tail'ing file
// handle live 'tail'ing file
// handle live 'tail'ing file
const args = process.argv.slice(2);
const filteredArgs = [];
for (let i = 0; i < args.length; i++) {
if (args[i] === "--flag") {
i++; // Skip the flag and its argument
} else {
filteredArgs.push(args[i]);
}
}
// Get the file path from command line arguments
const filePath = filteredArgs[0];
const files = filteredArgs.slice(1);
if (!filePath || !fss.existsSync(filePath)) {
console.error("Please provide a file path as an argument");
console.error("Usage: node wait2.js <file_path>");
process.exit(1);
}
// Resolve to absolute path
const absolutePath = resolve(filePath);
// Variables to track file position
let fileSize = 0;
// Function to read and print file content from a specific position
const readFromPosition = (position) => {
try {
const stats = statSync(absolutePath);
const currentSize = stats.size;
if (currentSize > position) {
const stream = createReadStream(absolutePath, {
start: position,
end: currentSize - 1,
});
stream.on("data", (chunk) => process.stdout.write(chunk));
stream.on("end", () => {
fileSize = currentSize;
});
stream.on("error", (err) => {
console.error(`Error reading file: ${err.message}`);
});
}
} catch (err) {
console.error(`Error accessing file: ${err.message}`);
}
};
// Main function to handle file watching
const tailFile = async () => {
// Initial read of the entire file
console.log(`Monitoring file: ${absolutePath}`);
readFromPosition(0);
// Watch for changes
const watcher = watch(absolutePath, (eventType, filename) => {
if (eventType === "change") {
readFromPosition(fileSize);
}
});
return watcher;
};
// Start monitoring
const watcher = await tailFile();
// now let's handle stdin
// now let's handle stdin
// now let's handle stdin
const __filename = fileURLToPath(import.meta.url);
const rel_script = path.relative(process.cwd(), __filename);
let i = 0;
function keypressed(key) {
i += 1;
process.stdout.write(`\n${rel_script}: ${i}`);
files.forEach((file) => {
const content = fss.readFileSync(file, "utf8");
process.stdout.write(content);
});
}
// Configure stdin
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
// Set up event listener for keypress events
process.stdin.on("keypress", (str, key) => {
// Exit on ctrl+c
if (key.ctrl && key.name === "c") {
watcher.close();
process.exit();
}
// if enter key pressed
if (key.name === "return") {
process.stdout.write("\n");
} else {
// Call the keypressed function with the key information
keypressed(str);
}
});
console.log(`${rel_script}: Keyboard listener activated. Press any key to trigger the keypressed function.`);
console.log("Press Ctrl+C to exit.");