-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwrite-file.js
More file actions
31 lines (26 loc) · 806 Bytes
/
Copy pathwrite-file.js
File metadata and controls
31 lines (26 loc) · 806 Bytes
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
#!/usr/bin/env node
var fs = require('fs');
var filepath = process.argv[2];
if (!filepath) {
console.log("-- error: please provide a path");
return;
}
// Initialize write stream
var ws = fs.createWriteStream(__dirname + '/' + filepath);
/*
Standard input will emit a 'readable' event when it has data to read
and adding this listener puts the terminal into interactive mode
*/
process.stdin.on('readable', function(){
// Read the data the user gives
var chunk = process.stdin.read();
// Guard against null chunk (calling toSting().toLowerCase() on null will break the program
if (chunk) {
// Check for the end command to terminate stdin stream
if (chunk.toString().toLowerCase() === "end\n") {
process.stdin.end();
} else {
ws.write(chunk);
}
}
});