diff --git a/bin/gh-pages b/bin/gh-pages index 5277cb5..3743697 100755 --- a/bin/gh-pages +++ b/bin/gh-pages @@ -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 ', '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'); } diff --git a/lib/index.js b/lib/index.js index 2db972f..6ca80c8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); });