moxystudio/node-proper-lockfile

Throws error if path leading to a file doesn't exist yet.

ZeeCoder opened this issue · 4 comments

First of all: thanks for the great lib!

I've encountered a weird issue.

This works fine even if the file doesn't exist:

lockfile.lock('./file.txt');

This however throws errors, when the path doesn't exist yet:

lockfile.lock('./path/that/does/not/exist/file.txt');

For the time being I've just made sure to use a wrapper that ensures directory paths leading to files that are about to be locked (whether existing or not yet) exist.

Hello! That’s because we try to resolve the real path of the file. You may disable the realpath option to disable this behavior.

Yeah I tried that. 🤔

With realpath: false I get this error:

ENOENT: no such file or directory, mkdir (...)

With realpath: true, I get this:

Error: ENOENT: no such file or directory, lstat (...)

Oh I misunderstood the question. At the moment, the path to the file must exist.

Perhaps we could create the directory first with the mkdirp package, perhaps under and option. Are you willing to take some time to contribute? I'm happy to review a PR that implements this.

Yeah I might, I've simply used fs-extra's ensureDir function at the moment, not sure if graceful-fs has anything like it:
https://github.com/jprichardson/node-fs-extra/blob/master/docs/ensureDir.md