Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Commit

Permalink
External dependencies checks (only git for now).
Browse files Browse the repository at this point in the history
This might also fix Travis CI.
  • Loading branch information
x1ddos committed Nov 24, 2014
1 parent 00f7bd3 commit 5cad11e
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 73 deletions.
52 changes: 52 additions & 0 deletions app/deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* WSK dependencies check module
*/

var exec = require('child_process').exec;
var events = require('events');


function checkAll(reqs) {
var e = new events.EventEmitter();

var checks = [checkGit];

process.nextTick(function () {
var counter = 0, maybeDone = function (result, data) {
counter += 1;
e.emit(result, data);
if (counter === checks.length) {
e.emit('done');
}
};

for (var i = 0; i < checks.length; i++) {
checks[i](maybeDone);
};
});

return e;
}

function checkGit(callback) {
exec("git config --get-regexp 'user\..*'", function (err, stdout) {
if (err) {
callback('failed', {what: 'git', error: err});
return;
}

var user = stdout.match(/^user\.name\s+(.+)$/m);
var email = stdout.match(/^user\.email\s+(.+)$/m);

if (user && user[1] && email && email[1]) {
callback('passed', {what: 'git'});
} else {
callback('failed', {what: 'git', error: new Error('Git: not configured')});
}
});
}

module.exports = {
checkAll: checkAll,
checkGit: checkGit
}
95 changes: 64 additions & 31 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var chalk = require('chalk');
var prompt = require('./prompt');
var download = require('./download');
var hosting = require('./hosting');
var deps = require('./deps');


var MobileGenerator = module.exports = yeoman.generators.Base.extend({
Expand Down Expand Up @@ -41,6 +42,12 @@ var MobileGenerator = module.exports = yeoman.generators.Base.extend({

// load package
this.pkg = require('../package.json');

// info/error/warning messages during the generation process
this.messages = [];

// dependencies checks;
this.checks = {};
},

prompting: function () {
Expand Down Expand Up @@ -68,35 +75,49 @@ var MobileGenerator = module.exports = yeoman.generators.Base.extend({
},

configuring: function () {
var log = this.log,
verbose = this.verbose,
dest = this.destinationRoot(),
var self = this,
done = this.async();

verbose && log.write().info('Getting latest WSK release version ...');
this.verbose && this.log.write().info('Getting latest WSK release version ...');

download({extract: true, strip: 1}, function (err, downloader, url, ver) {
if (err) {
log.error(err);
return;
self.log.error(err);
process.exit(1);
}

if (verbose) {
log.info('Found release %s', ver.tag_name)
if (self.verbose) {
self.log.info('Found release %s', ver.tag_name)
.info('Fetching %s ...', url)
.info(chalk.yellow('This might take a few moments'));
downloader.use(function (res) {
res.on('data', function () { log.write('.') });
res.on('data', function () { self.log.write('.') }) ;
});
}

downloader.dest(dest).run(function (err) {
downloader.dest(self.destinationRoot()).run(function (err) {
if (err) {
log.write().error(err).write();
} else {
verbose && log.write().ok('Done').write();
self.log.write().error(err).write();
process.exit(1);
}
done();

if (self.verbose) {
self.log.write().ok('Done').info('Checking dependencies ...');
}

var checks = deps.checkAll(self.prompts);
checks.on('done', done);

checks.on('passed', function (res) {
self.checks[res.what] = true;
self.verbose && self.log.ok(res.what + ' ' + (res.result || ''));
});

checks.on('failed', function (res) {
self.checks[res.what] = false;
self.messages.push(res.error.message);
self.log.error(res.error.message);
});
});

});
Expand Down Expand Up @@ -241,26 +262,22 @@ var MobileGenerator = module.exports = yeoman.generators.Base.extend({
this.dest.write(path.join('app', 'CNAME'), this.prompts.siteHost);
}

if (!this.checks.git)
return;

var log = !this.quiet && this.log,
done = this.async();

exec('git --version', function (err) {
if (err) {
// TODO: remember to notify user and describe manual steps
done();
return;
}
var cmd = [
'git init .',
'git checkout -b ' + this.prompts.githubBranch,
'git commit --allow-empty -m "Initial empty commit"',
'git remote add origin [email protected]:' + this.prompts.githubTarget
];
exec(cmd.join(' && '), {cwd: path.join('dist')}, function (err, stdout) {
log && log.write().info(stdout);
done();
});
}.bind(this));
var cmd = [
'git init .',
'git checkout -b ' + this.prompts.githubBranch,
'git commit --allow-empty -m "Initial empty commit"',
'git remote add origin [email protected]:' + this.prompts.githubTarget
];
exec(cmd.join(' && '), {cwd: path.join('dist')}, function (err, stdout) {
log && log.write().info(stdout);
done();
});
}

},
Expand All @@ -279,6 +296,9 @@ var MobileGenerator = module.exports = yeoman.generators.Base.extend({
},

git: function () {
if (!this.checks.git)
return;

var self = this, done = this.async(),
cmd = ['git init', 'git add .'],
gitignore = this.readFileAsString('.gitignore');
Expand All @@ -301,5 +321,18 @@ var MobileGenerator = module.exports = yeoman.generators.Base.extend({
done();
});
}
},

end: function () {
if (this.messages.length === 0) {
this.verbose && this.log.write().ok('You are all set now. Happy coding!');
return;
}

this.log.write().error('There were some errors during the process:').write();

for (var i = 0, m; m = this.messages[i]; i++) {
this.log.write((i + 1) + ' ' + m);
}
}
});
104 changes: 62 additions & 42 deletions test/test-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var fs = require('fs');
var exec = require('child_process').exec;
var path = require('path');
var assert = require('yeoman-generator').assert;

var testUtil = require('./util');
var deps = require('../app/deps');

describe('mobile:app - GitHub hosting', function () {

Expand All @@ -21,49 +23,58 @@ describe('mobile:app - GitHub hosting', function () {
testUtil.runGenerator(answers, done);
});

it('creates repo in dist/', function (done) {
assert.file('dist/.git');
exec('git status', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/working directory clean/m.test(stdout), stdout);
done();
});
it('has "gulp deploy" task', function () {
assert.fileContent('tasks/deploy.js', /gulp\.task\('deploy'/);
});

it('includes dist/ subrepo into the main repo', function (done) {
assert.noFileContent('.gitignore', /^dist\/?$/m);
exec('git status', function (err, stdout) {
assert.ok(!err, err);
assert.ok(/working directory clean/m.test(stdout), stdout);
done();
});
it('has no CNAME file', function () {
assert.noFile('app/CNAME');
});

it('sets remote to github in dist/.git', function (done) {
exec('git remote -v', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/^origin\s+git@github\.com:owner\/repo\s+/m.test(stdout), stdout);
done();
deps.checkGit(function (res) {

if (res !== 'passed') {
xit('skipping git-based tests');
return;
}

it('creates repo in dist/', function (done) {
assert.file('dist/.git');
exec('git status', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/working directory clean/m.test(stdout), stdout);
done();
});
});
});

it('initalizes gh-pages branch in dist/.git', function (done) {
exec('git branch', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/^\*\sgh-pages$/m.test(stdout), stdout);
done();
it('includes dist/ subrepo into the main repo', function (done) {
assert.noFileContent('.gitignore', /^dist\/?$/m);
exec('git status', function (err, stdout) {
assert.ok(!err, err);
assert.ok(/working directory clean/m.test(stdout), stdout);
done();
});
});
});

it('has "gulp deploy" task', function () {
assert.fileContent('tasks/deploy.js', /gulp\.task\('deploy'/);
});
it('sets remote to github in dist/.git', function (done) {
exec('git remote -v', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/^origin\s+git@github\.com:owner\/repo\s+/m.test(stdout), stdout);
done();
});
});

it('initalizes gh-pages branch in dist/.git', function (done) {
exec('git branch', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/^\*\sgh-pages$/m.test(stdout), stdout);
done();
});
});

it('has no CNAME file', function () {
assert.noFile('app/CNAME');
});

});
}); // describe project

describe('org/user', function () {

Expand All @@ -78,19 +89,28 @@ describe('mobile:app - GitHub hosting', function () {
testUtil.runGenerator(answers, done);
});

it('initalizes master branch in dist/.git', function (done) {
exec('git branch', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/^\*\smaster$/m.test(stdout), stdout);
done();
});
});

it('has no CNAME file', function () {
assert.noFile('app/CNAME');
});

});
deps.checkGit(function (res) {

if (res !== 'passed') {
xit('skipping git-based tests');
return;
}

it('initalizes master branch in dist/.git', function (done) {
exec('git branch', {cwd: 'dist'}, function (err, stdout) {
assert.ok(!err, err);
assert.ok(/^\*\smaster$/m.test(stdout), stdout);
done();
});
});

});

}); // describe org/user

describe('custom domain', function () {

Expand Down Expand Up @@ -122,6 +142,6 @@ describe('mobile:app - GitHub hosting', function () {
});
});

});
}); // describe custom domain

});

0 comments on commit 5cad11e

Please sign in to comment.