Skip to content

A better require statement #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kevinawoo
Copy link
Contributor

Closes issue #430 with a PR

@meeDamian
Copy link
Contributor

You need to add package.json entry.

@kevinawoo
Copy link
Contributor Author

@chester1000 Sorry, but I don't know what package.json entry is.

@meeDamian
Copy link
Contributor

In app/templates/_package.json file, add relevant line in dependencies section.

If you're using this, then it would be:

"better-require": "~0.0.3"

@kevinawoo
Copy link
Contributor Author

Oh wow, apparently I picked a bad name for my branch/PR.

What I actually did was added a new global function in app/templates/server/app.js

global.rootRequire = function (name) {
    return require(__dirname + '/' + name);
}

There wasn't any new package installs.

@meeDamian
Copy link
Contributor

Oops, my bad (:

@kingcody
Copy link
Member

@kevinawoo
I'm definitely not trying to be a sticker, but this project uses an automated task to generate the change log. It parses the commit messages looking for a particular format; outline here:
https://github.com/DaftMonk/generator-angular-fullstack/blob/master/contributing.md

If you could squash your commits into one that matches that format, it would be really nice.

If you have any trouble please feel free to MSG me or send me an email and I'll gladly see what I can do.

Thanks again for the PR!

@kingcody
Copy link
Member

@kevinawoo, i'm wondering...

Should rootRequire use path.normalize() ?
ex:

var path = require('path');
...
global.rootRequire = function (name) {
  return require(path.normalize(config.root + '/' + name));
};

(btw, I threw in the use of config.root instead of __dirname, either way)

It would allow for:
rootRequire('config/environment')
OR
rootRequire('/config/environment')

Just a thought...

Add:
- global.rootRequire() is a require statement based from `server/` when generated
  Use to replace long, relative require statements e.g. `require('../../../auth/auth.service')`

Closes angular-fullstack#430
@kevinawoo
Copy link
Contributor Author

You're absolutely right about path.normalize.

As for the config.root, my thinking behind using __dirname is to keep app.js and server/config/environment portable in the future, and to not confuse the reader, as they may think its a circular import.

@kingcody
Copy link
Member

I see. My only thought was that if app.js wasn't in the 'root' folder (not really sure why it would be, but) config.root could be used to get the app's real root.

But I'm not sure if there would be much of a use case for that.

Long story, short:
either __dirname or config .root, probably doesn't matter that much :)

@kevinawoo
Copy link
Contributor Author

@kingcody you did get me thinking about config.root though. Root always confused me a bit. "Did you mean root project, server root, ...client root??"

Since we're here, what do you think about changing it to:

config = {
   ...
  path: {
    project: ... '.',
    client: ... '/client',
    server: ... '/server'
  },
...
}

@kingcody
Copy link
Member

@kevinawoo you've made a good point. I'd definitely be interested to see what others have to say in regards to this.

Although I will point out that it is a server config. Valid point none the less :)

@Awk34 Awk34 added this to the 2.3.0 milestone Jul 16, 2015
@Awk34 Awk34 closed this Jun 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants