fs.watch('log-file', function(event, filename) {}); only returns the file name that was changed.
Is it possible to only get what was actually changed? I don't want to read the entire file, and rather want to know what was modified from my file (in my case, there is always addition to a log file. nothing ever gets erased).
It seems that you are looking to solve a similar problem to How to do `tail -f logfile.txt`-like processing in node.js?
As per the first response I would look into the the node-tail module.
Related
I've got images inserting into the editor as base64 encoded images (uploader option insertImageAsBase64URI is set to true). However, I'd like to call a function after the image has been inserted and also read the source filename for the inserted image.
I'm new to the JODIT editor, it seems great so far, but I need to tweak it a bit and am not sure how to register an event callback for this, or if there is another/better way. Any help is appreciated!
I think the best solution is to fork JODIT in github and edit the code. For some reason, however, I have been unable to build the code on my mac laptop for at least a couple of reasons (missing file in node module, fixed, and a build error "TypeError: require(...) is not a function" that may indicate circular dependencies in node modules?). Anyway, I found a complete and limited "HACK" for my needs and that is to actually capture the filename when the file is added by attaching an "onchange" handler function to the JODIT instance's file input element. This works roughly as follows (I'm using jQuery):
var selectedFile = null;
function setSelectedFile(){
$('.jodit').find('input[type="file"]').removeProp('multiple');
$('.jodit').find('input[type="file"]').on('change', function(){
var files = $(this).prop('files');
selectedFile = files[0].name;
});
});
$('.jodit').find('button[aria-label="Insert file"]').on('click',
function(){
setSelectedFile();
}
);
I run something like this after the page has loaded. This works only for the "change" event (where you select a file directly) and I could not figure out how to read the filename after a file is "dropped". Dropping a file does not seem to trigger the "change" event in the file input element. If anyone knows how to get the filename of a dropped file for the JODIT editor I'd appreciate sharing. I will update this if I get around to fixing that.
I have a file and I am reading it through chokidar. I am also writing to the same file in some another function using fs.writeFileSync.
I want to emit the watcher.on('change') function only when the file is changed manually (i.e. not through WriteFileSync). Is there a way of determining that ?
No, there isn't, not to my knowledge.
You can only know the file was changed, not what process/... changed it.
I implemented big-text file reading using createReadStream. In my implementation i wanted to test for error handling.
So i started process and renamed file, still entire file was read and all data were printed on console. In second try i did same but this time i deleted the file, and results were same, entire file was read and printed on console.
In short, I am reading a file using createReadStream, and after reading started even if i rename/delete the file, it still reads till the end.
Here is a sample implementation,
const fs = require('fs');
const readableStream = fs.createReadStream('mySampleTextFile.txt');
readableStream.on('data', (chunk) => console.log(chunk));
readableStream.on('end', () => console.log('Read till end !'));
mySampleTextFile.txt contains 600k rows with just number on each line, and i verified the last number logged to be exactly same as in the original file.
I am deleting the file before execution ends, i have two terminals open and in one i start execution and in second i fire delete/rename command.
I tried also deleting permanently means directly delete, not in trash.
Am i missing some fundamental of Stream or something? Please help me on this, it's not breaking anything but why it is not breaking ?
At least on Unix-type OS'es, when you remove a file while it's open, the process that opened the file can still read from it (or write to it).
Removing such a file merely removes its directory entry so you can't see it anymore when running ls or a file browser, but the disk space it takes up won't be marked "available" until there are no more processes that are referencing the file (i.e. have it open).
What you could try is truncating the file instead of removing it:
cp /dev/null mySampleTextFile.txt
I am able to copy a file in node.js using the following:
var readStream = fs.createReadStream(fromFilePath);
readStream.pipe(fs.createWriteStream(toFilePath));
The question is how to also copy/keep the modified time (mtime) like in a regular file copy command.
There are methods in the fs module to access mtime:
var stat = fs.statSync(fromFilePath);
fs.utimesSync(toFilePath, stat.atime, stat.mtime)
Use https://nodejs.org/api/fs.html#fs_fs_copyfile_src_dest_flags_callback .
The documentation does not say it but based on my tests it does keep/set the Modified-time to be the same as in the source-file, at least on Windows-10.
It does set the Created -time to the time the copy was made. But your question is about the modified-time so this is probably the simplest way to get what you want.
BTW. I find it curious that it now seems like the file was modified before it was created. How could that be! But so it seems, at least on Windows 10. I guess that's a good hint for us that the file was copied from somewhere else.
I've two methods. One is for image comparison and one is for file reading. what i do is I call a process that compares images between two folders and creates a logfile. Now the second method reads that logfile and parse the data.
But when i call the second method, it says, file not exist, this is because the exe take a little time to make the logfile.
I have used Thread.Sleep() but it still don't work, neither i can use that file.exist method as if i use it and if file does not exist then it will skip that method/file which i don't want.
You can use Process.WaitForExit to wait until your first process is finished, then you know when your log file will be complete/exist when calling your second method.