From 9459c823a72c3a56c226fc497d74fee35dbaa942 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Tue, 22 Mar 2016 00:41:24 +0100 Subject: [PATCH] Use git to find repo name and repo owner for init --- lib/init/git.js | 33 +++++++++++++++++++++++++++++++++ lib/init/prompt.js | 33 +++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 lib/init/git.js diff --git a/lib/init/git.js b/lib/init/git.js new file mode 100644 index 0000000..f83cfde --- /dev/null +++ b/lib/init/git.js @@ -0,0 +1,33 @@ +'use strict'; + +var spawn = require('child_process').spawn; + +function getRemoteOriginData(cb) { + var output = ''; + var git = spawn('git', 'config --get remote.origin.url'.split(' ')); + git.stdout.on('data', function (data) { + output += data; + }); + + git.stderr.on('data', cb); + git.on('close', function () { + cb(null, output); + }); +} + +function parse(originUrl) { + var result = /\:(\w+)\/([A-Za-z0-9-_]+)/.exec(originUrl); + return { + projectOwner: result[1], + projectName: result[2] + }; +} + +module.exports = function getRepoInfo(cb) { + getRemoteOriginData(function (error, originUrl) { + if (error) { + return cb(error); + } + return cb(null, parse(originUrl)); + }); +}; diff --git a/lib/init/prompt.js b/lib/init/prompt.js index a1079cf..e75c427 100644 --- a/lib/init/prompt.js +++ b/lib/init/prompt.js @@ -3,6 +3,8 @@ var _ = require('lodash/fp'); var inquirer = require('inquirer'); +var getRepoInfo = require('./git'); + var questions = [{ type: 'input', name: 'projectName', @@ -44,18 +46,25 @@ var uniqueFiles = _.flow( ); module.exports = function prompt(cb) { - inquirer.prompt(questions, function treatAnswers(answers) { - var config = { - projectName: answers.projectName, - projectOwner: answers.projectOwner, - files: uniqueFiles([answers.contributorFile, answers.badgeFile]), - imageSize: answers.imageSize, - contributors: [] - }; - return cb({ - config: config, - contributorFile: answers.contributorFile, - badgeFile: answers.badgeFile + getRepoInfo(function (error, repoInfo) { + if (error) { + return cb(error); + } + questions[0].default = repoInfo.projectName; + questions[1].default = repoInfo.projectOwner; + inquirer.prompt(questions, function treatAnswers(answers) { + var config = { + projectName: answers.projectName, + projectOwner: answers.projectOwner, + files: uniqueFiles([answers.contributorFile, answers.badgeFile]), + imageSize: answers.imageSize, + contributors: [] + }; + return cb({ + config: config, + contributorFile: answers.contributorFile, + badgeFile: answers.badgeFile + }); }); }); };