Node.js has a method fs.watch() , which is used for watching for file changes. But the api is unstable. Using other fs methods which are stable, what is the quickest way to check if a file has changed ?
EDIT: I don't want to use external modules.
Have you looked into:
fs.stat(path, [callback])
The documentation gives last modified date:
http://nodejs.org/docs/v0.4.12/api/fs.html#fs.stat
ala:
if (curr.mtime - prev.mtime) {
// file has been changed
}
edit: leaving here just in case others would like to use a wrapper.
Look here, this seems to be something that could be useful: It is a wrapper for fs.watch / fs.watchFile
https://github.com/paulmillr/chokidar
Related
Is there any way to parse data to a .yaml file in node.js? I've been searching about this for hours and found nothing useful.
Let's say I have a string in a .yaml file:
message: Hello, ${user.fullName}, have a nice day.
As I've been reading, there's something called "placeholders" but it seems only available in spring framework.
This is what I found.
And in other places, they use a different syntax like {{user.fullName}}
So, is there any way to replace ${user.fullName} with an actual value in node.js? I don't know if I should manually handle this thing that they call "placeholder" myself with string.replace(). I checked the main YAML docs and there's nothing called "placeholder" or even "variable" in their entire docs!
But, similarly, I can achieve a similar result with pug.js using #{user.fullName} (with # instead) and then call something like: pug.renderFile('template.pug', {fullName: 'Timothy'}). Check pug.js docs for more details.
Thanks for your help!
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.
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.
I just started playing with node debugger and noticed a node.js file which is invoked at the very beginning of node execution.
As the comment in the file says
This file is invoked by node::Load in src/node.cc, and responsible for bootstrapping the node.js core.
I'd like to change content of this file to something else (yes, I know there's no need to do that), is there a way to replace content of that file / specify path to new file without compiling node from source?
Short answer: Nope.
If I read the source correctly, the file node.js gets compiled into the resulting binary, as a string - the file itself does not exist anywhere on the filesystem so you cannot modify it and, for the same reason, you cannot tell Node to execute your own version of it.
Best look at the sources - mainly the LoadEnvironment method.
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.