diff --git a/app/github.js b/app/github.js new file mode 100644 index 0000000..17dbff7 --- /dev/null +++ b/app/github.js @@ -0,0 +1,27 @@ +var github = require('octonode'); + +var githubReleases = { + fetch: function (user, repo, cb) { + var client = github.client(); + + var repo = client.repo(user + '/' + repo); + + repo.releases(function (err, status, body) { + if (err) { + return cb(err); + } + + if (status !== 200) { + return cb(new Error("Bad response from Github API: ")); + } + + var releases = body.reduce(function (memo, releaseInfo) { + memo[releaseInfo.name] = releaseInfo; + }, {}); + + cb(null, releases); + }); + } +}; + +module.exports = githubReleases; \ No newline at end of file diff --git a/app/index.js b/app/index.js index 7f2a628..efa5072 100644 --- a/app/index.js +++ b/app/index.js @@ -3,6 +3,7 @@ var util = require('util'); var path = require('path'); var spawn = require('child_process').spawn; var yeoman = require('yeoman-generator'); +var githubReleases = require('./github'); var GhostGenerator = module.exports = function GhostGenerator(args, options, config) { @@ -18,22 +19,38 @@ var GhostGenerator = module.exports = function GhostGenerator(args, options, con util.inherits(GhostGenerator, yeoman.generators.Base); GhostGenerator.prototype.askFor = function askFor() { + var self = this; var cb = this.async(); // have Yeoman greet the user. console.log(this.yeoman); - var prompts = [{ - type: 'input', - name: 'version', - message: 'What version of Ghost do you want to download?', - default: '0.3.3' - }]; - - this.prompt(prompts, function (props) { - this.version = props.version; - cb(); - }.bind(this)); + // Grab the github releases for Ghost + githubReleases.fetch('TryGhost', 'Ghost', function (err, releases) { + // Store the available release map for later. + self.availableReleases = releases; + + // Store the release names as the choices + var choices = []; + for(var key in releases) { + if (!releases.hasOwnProperty(key)) { continue; } + + choices.push(key); + } + + var prompts = [{ + type: 'list', + name: 'version', + message: 'What version of Ghost do you want to download?', + choices: choices, + default: '0.3.3' + }]; + + self.prompt(prompts, function (props) { + self.version = props.version; + cb(); + }); + }); }; GhostGenerator.prototype.app = function app() { diff --git a/package.json b/package.json index bf89655..6204434 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ }, "dependencies": { "yeoman-generator": "~0.13.0", - "fs-extra": "~0.8.1" + "fs-extra": "~0.8.1", + "octonode": "~0.4.1" }, "devDependencies": { "mocha": "~1.12.0" diff --git a/test/test-creation.js b/test/test-creation.js index 8cd12c3..a1134db 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -20,14 +20,17 @@ describe('ghost generator', function () { }); it('creates expected files', function (done) { + // Extend the timeout since the github release download takes some time + this.timeout(10000); + var expected = [ // add files you expect to exist here. - '.jshintrc', - '.editorconfig' + 'Gruntfile.js', + 'core/ghost.js' ]; helpers.mockPrompt(this.app, { - 'someOption': true + 'version': '0.3.3' }); this.app.options['skip-install'] = true; this.app.run({}, function () {