yeoman/generator — pull request #220 by stephenplusplus (2013-04-28 02:52:51+00:00)
Title: actions -> read() -> check if file exists before returning its contents.

Yo,

I originally got an error while toying around with the Backbone generator:

```
~ mkdir ~/dev/hellohello
~ cd ~/dev/hellohello && backbone yo (all nos)
~ yo backbone:model fakeModel
fs.js:427
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory '/Users/stephen/dev/hellohello/app/scripts/main.js'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at Generator.read (/Users/stephen/dev/generator-backbone/node_modules/yeoman-generator/lib/actions/actions.js:110:13)
at Generator.isUsingRequireJS (/Users/stephen/dev/generator-backbone/script-base.js:57:40)
at Generator.createModelFiles (/Users/stephen/dev/generator-backbone/model/index.js:37:30)
at next (/Users/stephen/dev/generator-backbone/node_modules/yeoman-generator/lib/base.js:275:18)
at Generator.run (/Users/stephen/dev/generator-backbone/node_modules/yeoman-generator/lib/base.js:289:4)
at Environment.run (/Users/stephen/dev/yo/node_modules/yeoman-generator/lib/env.js:424:20)
at init (/Users/stephen/dev/yo/bin/yo:72:7)
```

I thought I'd take a stab at a fix. I know there are a few ways to look at this problem...

1) It's a Backbone generator issue, when Backbone checks if my project is using require.js: [:57](https://github.com/yeoman/generator-backbone/blob/master/script-base.js). My first thought was, the BB generator could take it upon itself to see if the file exists before trying to continue.
2) It's a core Generator issue: [:110](https://github.com/yeoman/generator/blob/master/lib/actions/actions.js). My next thought was, I'm sure the `read` method is used in a lot of different places, so why not check if a file exists before trying to read it?
3) It's not an issue, and I just did something incorrectly (?) is my lingering third thought.

So, basically, I just added in a `fs.existsSync()` check before returning the content of the file. If the file wasn't found, it returns an empty string.