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 _ = require('lodash/fp');
var inquirer = require('inquirer'); var inquirer = require('inquirer');
var getRepoInfo = require('./git');
var questions = [{ var questions = [{
type: 'input', type: 'input',
name: 'projectName', name: 'projectName',
@ -44,18 +46,25 @@ var uniqueFiles = _.flow(
); );
module.exports = function prompt(cb) { module.exports = function prompt(cb) {
inquirer.prompt(questions, function treatAnswers(answers) { getRepoInfo(function (error, repoInfo) {
var config = { if (error) {
projectName: answers.projectName, return cb(error);
projectOwner: answers.projectOwner, }
files: uniqueFiles([answers.contributorFile, answers.badgeFile]), questions[0].default = repoInfo.projectName;
imageSize: answers.imageSize, questions[1].default = repoInfo.projectOwner;
contributors: [] inquirer.prompt(questions, function treatAnswers(answers) {
}; var config = {
return cb({ projectName: answers.projectName,
config: config, projectOwner: answers.projectOwner,
contributorFile: answers.contributorFile, files: uniqueFiles([answers.contributorFile, answers.badgeFile]),
badgeFile: answers.badgeFile imageSize: answers.imageSize,
contributors: []
};
return cb({
config: config,
contributorFile: answers.contributorFile,
badgeFile: answers.badgeFile
});
}); });
}); });
}; };