Use git to find repo name and repo owner for init

This commit is contained in:
Jeroen Engels 2016-03-22 00:41:24 +01:00
parent 0382e43089
commit 9459c823a7
2 changed files with 54 additions and 12 deletions

33
lib/init/git.js Normal file
View file

@ -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));
});
};

View file

@ -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
});
});
});
};