lonix
lonix

Reputation: 20591

node script or npm module to check if file changed

I'm setting up a build system using node modules and npm scripts - no gulp, etc.

I need to determine if two files are equal, before copying unnecessarily. When I used to use gulp I used the gulp-changed plugin. I need something like that.

How could I do this in plain node?

I couldn't find an existing npm module that does this. I also checked the fs module but didn't find anything I could use.

I need something like this: function hasChanged(file1, file2) { /* ... */ } but I'm not sure how to compare the files.

UPDATE
Using the advice given so far, this problem seems simple enough to code myself, so I'm doing that. But if you know of a node module that does this already, I'd appreciate it.

Upvotes: 0

Views: 1024

Answers (2)

Lars C. Magnusson
Lars C. Magnusson

Reputation: 349

You might use fs.stat function to get file info, and compare mtime to see if they differ. I also recommend you to lookup the sourcecode of gulp-change, its actully just a few lines of code.

https://github.com/sindresorhus/gulp-changed?files=1

Upvotes: 1

hong4rc
hong4rc

Reputation: 4103

Do you try use fs.watch.

It is still possible to use fs.watchFile(), which uses stat polling, but this method is slower and less reliable.

If you want check 2 file, try using hash code of them with md5.

Upvotes: 1

Related Questions