mirror of
https://github.com/scratchfoundation/gh-pages.git
synced 2024-11-28 18:25:38 -05:00
added option to retrieve current branch name
and use it as the subfolder name for the destination
This commit is contained in:
parent
fde7245554
commit
e0dd4eaa54
2 changed files with 49 additions and 1 deletions
|
@ -27,6 +27,8 @@ program
|
||||||
'Remove files that match the given pattern ' +
|
'Remove files that match the given pattern ' +
|
||||||
'(ignored if used together with --add).', '.')
|
'(ignored if used together with --add).', '.')
|
||||||
.option('-n, --no-push', 'Commit only (with no push)')
|
.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);
|
.parse(process.argv);
|
||||||
|
|
||||||
ghpages.publish(path.join(process.cwd(), program.dist), {
|
ghpages.publish(path.join(process.cwd(), program.dist), {
|
||||||
|
@ -41,6 +43,8 @@ ghpages.publish(path.join(process.cwd(), program.dist), {
|
||||||
only: program.remove,
|
only: program.remove,
|
||||||
remote: program.remote,
|
remote: program.remote,
|
||||||
push: !program.noPush,
|
push: !program.noPush,
|
||||||
|
dest: program.dest,
|
||||||
|
branchnameAsDest: program.branchnameAsDest,
|
||||||
logger: function(message) {
|
logger: function(message) {
|
||||||
process.stderr.write(message + '\n');
|
process.stderr.write(message + '\n');
|
||||||
}
|
}
|
||||||
|
|
46
lib/index.js
46
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) {
|
function getRepo(options) {
|
||||||
if (options.repo) {
|
if (options.repo) {
|
||||||
return Q.resolve(options.repo);
|
return Q.resolve(options.repo);
|
||||||
|
@ -58,6 +87,7 @@ exports.publish = function publish(basePath, config, callback) {
|
||||||
|
|
||||||
var defaults = {
|
var defaults = {
|
||||||
dest: '.',
|
dest: '.',
|
||||||
|
branchnameAsDest: false,
|
||||||
add: false,
|
add: false,
|
||||||
git: 'git',
|
git: 'git',
|
||||||
clone: getCacheDir(),
|
clone: getCacheDir(),
|
||||||
|
@ -81,7 +111,7 @@ exports.publish = function publish(basePath, config, callback) {
|
||||||
for (var c in config) {
|
for (var c in config) {
|
||||||
options[c] = config[c];
|
options[c] = config[c];
|
||||||
}
|
}
|
||||||
|
console.log(options);
|
||||||
function log(message) {
|
function log(message) {
|
||||||
if (!options.silent) {
|
if (!options.silent) {
|
||||||
options.logger(message);
|
options.logger(message);
|
||||||
|
@ -130,6 +160,8 @@ exports.publish = function publish(basePath, config, callback) {
|
||||||
|
|
||||||
git.exe(options.git);
|
git.exe(options.git);
|
||||||
|
|
||||||
|
var dest = options.dest
|
||||||
|
|
||||||
var repoUrl;
|
var repoUrl;
|
||||||
getRepo(options)
|
getRepo(options)
|
||||||
.then(function(repo) {
|
.then(function(repo) {
|
||||||
|
@ -137,6 +169,17 @@ exports.publish = function publish(basePath, config, callback) {
|
||||||
log('Cloning ' + repo + ' into ' + options.clone);
|
log('Cloning ' + repo + ' into ' + options.clone);
|
||||||
return git.clone(repo, options.clone, options.branch, options);
|
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() {
|
.then(function() {
|
||||||
return getRemoteUrl(options.clone, options.remote)
|
return getRemoteUrl(options.clone, options.remote)
|
||||||
.then(function(url) {
|
.then(function(url) {
|
||||||
|
@ -169,6 +212,7 @@ exports.publish = function publish(basePath, config, callback) {
|
||||||
.then(function() {
|
.then(function() {
|
||||||
if (!options.add) {
|
if (!options.add) {
|
||||||
log('Removing files');
|
log('Removing files');
|
||||||
|
var dest = options.branchNameAsDest
|
||||||
var outputFiles = only.map(function(file) {
|
var outputFiles = only.map(function(file) {
|
||||||
return path.join(options.dest, file);
|
return path.join(options.dest, file);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue