Skip to content
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

Utility methods to get Git info #314

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/actions/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var logger = process.logging || require('./utils/log');
var logger = process.logging || require('../util/log');
var log = logger('generators:action');

var fs = require('fs');
Expand Down
3 changes: 3 additions & 0 deletions lib/actions/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var shell = require('shelljs');

module.exports.shell = shell;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a separate file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for concern separation, but I'm open to suggestion.

I hesitated to merge this with spawn_command as they're both related to process running in a shell.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just require shelljs directly in user.js and base.js?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could work. I just based myself on actions/string.js to keep consistency. This module does mostly the same thing as I did in actions/shell.js (less a little logic to merge Lodash and underscore.string).

So maybe it'll be best to stick to a style?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated for the current. Might be good to consider it for the string module too in the future.

28 changes: 28 additions & 0 deletions lib/actions/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// User related helper methods

var shell = require('./shell').shell;
var _ = require('lodash');


// Exports module
var user = module.exports = {};


// Git related method
//
// The value will come from the global scope or the project scope (it'll take what git
// will use in the current context)

user.git = {};

// Get the current git user.name
// Returns String
user.git.getUsername = function (cb) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make these getters instead?

Eg: user.git.username

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should remain a function call so it is obvious that it has a side effect, especially since it can potentially fail.

(Edit: typo)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same opinion than @passy here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, with a smart cache system that'll take into account CWD context, I think @sindresorhus's right and it'll make for a better API façade - will do!

return shell.exec('git config --get user.name', { silent: true }).output.trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a sync slow operation it should be cached so it's only hit one time during the whole lifetime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just memoize it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I though about this. The problem is that the username may change depending on where the CWD is.

So, we could cache the result related to the CWD, but that seemed like unnecessary optimisation to me.

};

// Get the current git user.email
// Returns String
user.git.getEmail = function (cb) {
return shell.exec('git config --get user.email', { silent: true }).output.trim();
};
2 changes: 2 additions & 0 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ _.extend(Base.prototype, require('./actions/install'));
_.extend(Base.prototype, require('./actions/string'));
_.extend(Base.prototype, require('./actions/wiring'));
_.extend(Base.prototype, require('./util/common'));
Base.prototype.user = require('./actions/user');
Base.prototype.shell = require('./actions/shell');
Base.prototype.prompt = require('./actions/prompt');
Base.prototype.invoke = require('./actions/invoke');
Base.prototype.spawnCommand = require('./actions/spawn_command');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"dargs": "~0.1.0",
"async": "~0.2.8",
"inquirer": "~0.2.0",
"iconv-lite": "~0.2.10"
"iconv-lite": "~0.2.10",
"shelljs": "~0.1.4"
},
"devDependencies": {
"mocha": "~1.11.0",
Expand Down
19 changes: 19 additions & 0 deletions test/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*global describe, it */
var assert = require('assert');
var shelljs = require('shelljs');
var shellModule = require('../lib/actions/shell');
var _ = require('lodash');

describe('Generator shell methods API', function () {

it('should be exposed on the Base generator', function () {
assert.equal(shellModule, require('../lib/base').prototype.shell);
});

it('should extend shelljs module', function () {
_.each(shelljs, function (method, name) {
assert.equal(method, shellModule.shell[name]);
});
});

});
39 changes: 39 additions & 0 deletions test/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*global describe, before, it, after, before */
var userUtils = require('../lib/actions/user');
var shell = require('shelljs');
var assert = require('assert');

describe('user utility', function () {

it('should be exposed on the Base generator', function () {
assert.equal(userUtils, require('../lib/base').prototype.user);
});

describe('git methods', function () {

before(function () {
this.cwd = process.cwd();
this.tmp = shell.tempdir();
shell.cd(this.tmp);
shell.exec('git init --quiet');
shell.exec('git config --local user.name Yeoman');
shell.exec('git config --local user.email yo@yeoman.io');
});

after(function () {
shell.cd(this.cwd);
});

it('git.getUsername should return the username used by git', function () {
var username = userUtils.git.getUsername();
assert.equal(username, 'Yeoman');
});

it('git.getEmail should return the email used by git', function () {
var email = userUtils.git.getEmail();
assert.equal(email, 'yo@yeoman.io');
});

});

});