added option to retrieve current branch name

and use it as the subfolder name for the destination
This commit is contained in:
Jan-Felix 2017-04-04 16:37:20 +02:00
parent fde7245554
commit e0dd4eaa54
2 changed files with 49 additions and 1 deletions

View file

@ -27,6 +27,8 @@ program
'Remove files that match the given pattern ' +
'(ignored if used together with --add).', '.')
.option('-n, --no-push', 'Commit only (with no push)')
.option('-e, --dest <dest>', 'The destination in the target branch', '.')
.option('-c, --branchname-as-dest', 'Use a subfolder named after the current as the destination')
.parse(process.argv);
ghpages.publish(path.join(process.cwd(), program.dist), {
@ -41,6 +43,8 @@ ghpages.publish(path.join(process.cwd(), program.dist), {
only: program.remove,
remote: program.remote,
push: !program.noPush,
dest: program.dest,
branchnameAsDest: program.branchnameAsDest,
logger: function(message) {
process.stderr.write(message + '\n');
}

View file

@ -35,6 +35,35 @@ function getRemoteUrl(dir, remote) {
});
}
/**
* Return a promise resolving to the currently checked out branch name.
* @param {string} cwd Repository directory.
* @return {Promise} A promise.
*/
function getBranchName(options) {
var name;
return git(['rev-parse', '--abbrev-ref', 'HEAD'], options.repo || process.cwd())
.progress(function(chunk) {
name = String(chunk).split(/[\n\r]/).shift();
})
.then(function() {
if (name) {
return Q.resolve(name);
} else {
return Q.reject(new Error(
'Failed to get branch name from options or current directory.'));
}
})
.fail(function(err) {
return Q.reject(new Error(
'Failed to get branch name (task must either be ' +
'run in a git repository or must be configured with the "repo" ' +
'option).'));
});
};
function getRepo(options) {
if (options.repo) {
return Q.resolve(options.repo);
@ -58,6 +87,7 @@ exports.publish = function publish(basePath, config, callback) {
var defaults = {
dest: '.',
branchnameAsDest: false,
add: false,
git: 'git',
clone: getCacheDir(),
@ -81,7 +111,7 @@ exports.publish = function publish(basePath, config, callback) {
for (var c in config) {
options[c] = config[c];
}
console.log(options);
function log(message) {
if (!options.silent) {
options.logger(message);
@ -130,6 +160,8 @@ exports.publish = function publish(basePath, config, callback) {
git.exe(options.git);
var dest = options.dest
var repoUrl;
getRepo(options)
.then(function(repo) {
@ -137,6 +169,17 @@ exports.publish = function publish(basePath, config, callback) {
log('Cloning ' + repo + ' into ' + options.clone);
return git.clone(repo, options.clone, options.branch, options);
})
.then(function() {
if (options.branchnameAsDest) {
log('Retrieving current branch name');
return getBranchName(options.clone)
.then(function(branchname) {
dest = './' + branchname;
});
} else {
return Q.resolve();
}
})
.then(function() {
return getRemoteUrl(options.clone, options.remote)
.then(function(url) {
@ -169,6 +212,7 @@ exports.publish = function publish(basePath, config, callback) {
.then(function() {
if (!options.add) {
log('Removing files');
var dest = options.branchNameAsDest
var outputFiles = only.map(function(file) {
return path.join(options.dest, file);
});