Testing for the existence of a file


I am trying to check for a specific file in an open project using atom.project.contains(atom.project.path + "/filename"), where filename is the name of the file. This seems to return true no matter what filename I give it. What is the proper way to check for the existence of a file?

P.S. I know that this approach will not likely work on Windows due to the hardcode “/” path separator.



fs = require 'fs'
path = require 'path'

projectPath = atom.project.path
fileExists = fs.existsSync(path.join(projectPath, 'filename'))


Still not sure why contains doesn’t seem to work, but I have determined that I can use atom.project.rootDirectory.getFile("filename").exists() to test for the file.


Just missed your reply before replying myself - thanks.


The documentation states:

Returns whether the given path is inside this project.

Which is probably poorly worded. Just because the path string supplied is under the base path of the project doesn’t mean that a file exists at that path. I’ll see if I can enhance the documentation on that method.


Created a PR with some better documentation for the function in question: