Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #166

Merged
merged 2 commits into from
May 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 46 additions & 38 deletions bin/gh-pages
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,56 @@ var path = require('path');

program
.version(require('../package').version)
.option('-d, --dist <dist>',
'base directory for all source files')
.option('-s, --src <src>',
'pattern used to select which files should be published', '**/*')
.option('-r, --repo <repo>',
'URL of the repository you\'ll be pushing to')
.option('-d, --dist <dist>', 'base directory for all source files')
.option(
'-s, --src <src>',
'pattern used to select which files should be published',
'**/*'
)
.option('-r, --repo <repo>', "URL of the repository you'll be pushing to")
.option('-x, --silent', 'Do not output the repository url')
.option('-b, --branch <branch>',
'name of the branch you\'ll be pushing to', 'gh-pages')
.option('-o, --remote <name>',
'The name of the remote', 'origin')
.option('-m, --message <message>',
'commit message', 'Updates')
.option('-g, --tag <tag>',
'add tag to commit')
.option(
'-b, --branch <branch>',
"name of the branch you'll be pushing to",
'gh-pages'
)
.option('-o, --remote <name>', 'The name of the remote', 'origin')
.option('-m, --message <message>', 'commit message', 'Updates')
.option('-g, --tag <tag>', 'add tag to commit')
.option('-t, --dotfiles', 'Include dotfiles')
.option('-a, --add', 'Only add, and never remove existing files.')
.option('-v, --remove <pattern>',
'Remove files that match the given pattern ' +
'(ignored if used together with --add).', '.')
.option(
'-v, --remove <pattern>',
'Remove files that match the given pattern ' +
'(ignored if used together with --add).',
'.'
)
.option('-n, --no-push', 'Commit only (with no push)')
.parse(process.argv);

ghpages.publish(path.join(process.cwd(), program.dist), {
repo: program.repo,
silent: !!program.silent,
branch: program.branch,
src: program.src,
message: program.message,
tag: program.tag,
dotfiles: !!program.dotfiles,
add: !!program.add,
only: program.remove,
remote: program.remote,
push: !program.noPush,
logger: function(message) {
process.stderr.write(message + '\n');
ghpages.publish(
path.join(process.cwd(), program.dist),
{
repo: program.repo,
silent: !!program.silent,
branch: program.branch,
src: program.src,
message: program.message,
tag: program.tag,
dotfiles: !!program.dotfiles,
add: !!program.add,
only: program.remove,
remote: program.remote,
push: !program.noPush,
logger: function(message) {
process.stderr.write(message + '\n');
}
},
function(err) {
if (err) {
process.stderr.write(err.message + '\n');
return process.exit(1);
}
process.stderr.write('Published\n');
}
}, function(err) {
if (err) {
process.stderr.write(err.message + '\n');
return process.exit(1);
}
process.stderr.write('Published\n');
});
);
136 changes: 63 additions & 73 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
var cp = require('child_process');
var fs = require('fs-extra');
var path = require('path');
var util = require('util');

var Q = require('q');
var fs = require('q-io/fs');

var git = 'git';


/**
* @constructor
* @param {number} code Error code.
Expand All @@ -23,7 +20,6 @@ function ProcessError(code, message) {
}
util.inherits(ProcessError, Error);


/**
* Util function for handling spawned processes as promises.
* @param {string} exe Executable.
Expand All @@ -32,40 +28,35 @@ util.inherits(ProcessError, Error);
* @return {Promise} A promise.
*/
function spawn(exe, args, cwd) {
var deferred = Q.defer();
var child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
var buffer = [];
child.stderr.on('data', function(chunk) {
buffer.push(chunk.toString());
});
child.stdout.on('data', function(chunk) {
deferred.notify(chunk);
});
child.on('close', function(code) {
if (code) {
var msg = buffer.join('') || 'Process failed: ' + code;
deferred.reject(new ProcessError(code, msg));
} else {
deferred.resolve(code);
}
return new Promise(function(resolve, reject) {
var child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
var buffer = [];
child.stderr.on('data', function(chunk) {
buffer.push(chunk.toString());
});
child.on('close', function(code) {
var output = buffer.join('');
if (code) {
var msg = output || 'Process failed: ' + code;
reject(new ProcessError(code, msg));
} else {
resolve(output);
}
});
});
return deferred.promise;
}


/**
* Execute a git command.
* @param {Array.<string>} args Arguments (e.g. ['remote', 'update']).
* @param {string} cwd Repository directory.
* @return {Promise} A promise. The promise will be resolved with the exit code
* or rejected with an error. To get stdout, use a progress listener (e.g.
* `promise.progress(function(chunk) {console.log(String(chunk);}))`).
* @return {Promise} A promise. The promise will be resolved with stdout output
* or rejected with an error.
*/
exports = module.exports = function(args, cwd) {
return spawn(git, args, cwd);
};


/**
* Set the Git executable to be used by exported methods (defaults to 'git').
* @param {string} exe Git executable (full path if not already on path).
Expand All @@ -74,7 +65,6 @@ exports.exe = function(exe) {
git = exe;
};


/**
* Initialize repository.
* @param {string} cwd Repository directory.
Expand All @@ -84,7 +74,6 @@ exports.init = function init(cwd) {
return spawn(git, ['init'], cwd);
};


/**
* Clone a repo into the given dir if it doesn't already exist.
* @param {string} repo Repository URL.
Expand All @@ -96,14 +85,23 @@ exports.init = function init(cwd) {
exports.clone = function clone(repo, dir, branch, options) {
return fs.exists(dir).then(function(exists) {
if (exists) {
return Q.resolve();
return Promise.resolve();
} else {
return fs.makeTree(path.dirname(path.resolve(dir))).then(function() {
var args = ['clone', repo, dir, '--branch', branch, '--single-branch', '--origin', options.remote];
return fs.mkdirp(path.dirname(path.resolve(dir))).then(function() {
var args = [
'clone',
repo,
dir,
'--branch',
branch,
'--single-branch',
'--origin',
options.remote
];
if (options.depth) {
args.push('--depth', options.depth);
}
return spawn(git, args).fail(function(err) {
return spawn(git, args).catch(function(err) {
// try again without banch options
return spawn(git, ['clone', repo, dir, '--origin', options.remote]);
});
Expand All @@ -112,16 +110,14 @@ exports.clone = function clone(repo, dir, branch, options) {
});
};


/**
* Clean up unversioned files.
* @param {string} cwd Repository directory.
* @return {Promise} A promise.
*/
var clean = exports.clean = function clean(cwd) {
var clean = (exports.clean = function clean(cwd) {
return spawn(git, ['clean', '-f', '-d'], cwd);
};

});

/**
* Hard reset to remote/branch
Expand All @@ -130,10 +126,9 @@ var clean = exports.clean = function clean(cwd) {
* @param {string} cwd Repository directory.
* @return {Promise} A promise.
*/
var reset = exports.reset = function reset(remote, branch, cwd) {
var reset = (exports.reset = function reset(remote, branch, cwd) {
return spawn(git, ['reset', '--hard', remote + '/' + branch], cwd);
};

});

/**
* Fetch from a remote.
Expand All @@ -145,7 +140,6 @@ exports.fetch = function fetch(remote, cwd) {
return spawn(git, ['fetch', remote], cwd);
};


/**
* Checkout a branch (create an orphan if it doesn't exist on the remote).
* @param {string} remote Remote alias.
Expand All @@ -155,28 +149,29 @@ exports.fetch = function fetch(remote, cwd) {
*/
exports.checkout = function checkout(remote, branch, cwd) {
var treeish = remote + '/' + branch;
return spawn(git, ['ls-remote', '--exit-code', '.', treeish], cwd)
.then(function() {
// branch exists on remote, hard reset
return spawn(git, ['checkout', branch], cwd)
.then(function() {
return clean(cwd);
})
.then(function() {
return reset(remote, branch, cwd);
});
}, function(error) {
if (error instanceof ProcessError && error.code === 2) {
// branch doesn't exist, create an orphan
return spawn(git, ['checkout', '--orphan', branch], cwd);
} else {
// unhandled error
return Q.reject(error);
}
});
return spawn(git, ['ls-remote', '--exit-code', '.', treeish], cwd).then(
function() {
// branch exists on remote, hard reset
return spawn(git, ['checkout', branch], cwd)
.then(function() {
return clean(cwd);
})
.then(function() {
return reset(remote, branch, cwd);
});
},
function(error) {
if (error instanceof ProcessError && error.code === 2) {
// branch doesn't exist, create an orphan
return spawn(git, ['checkout', '--orphan', branch], cwd);
} else {
// unhandled error
throw error;
}
}
);
};


/**
* Remove all unversioned files.
* @param {string} files Files argument.
Expand All @@ -187,7 +182,6 @@ exports.rm = function rm(files, cwd) {
return spawn(git, ['rm', '--ignore-unmatch', '-r', '-f', files], cwd);
};


/**
* Add files.
* @param {string} files Files argument.
Expand All @@ -198,25 +192,22 @@ exports.add = function add(files, cwd) {
return spawn(git, ['add', files], cwd);
};


/**
* Commit.
* @param {string} message Commit message.
* @param {string} cwd Repository directory.
* @return {Promise} A promise.
*/
exports.commit = function commit(message, cwd) {
return spawn(git, ['diff-index', '--quiet', 'HEAD', '.'], cwd)
.then(function() {
// nothing to commit
return Q.resolve();
})
.fail(function() {
return spawn(git, ['commit', '-m', message], cwd);
});
return spawn(
git,
['diff-index', '--quiet', 'HEAD', '.'],
cwd
).catch(function() {
return spawn(git, ['commit', '-m', message], cwd);
});
};


/**
* Add tag
* @param {string} name Name of tag.
Expand All @@ -227,7 +218,6 @@ exports.tag = function tag(name, cwd) {
return spawn(git, ['tag', name], cwd);
};


/**
* Push a branch.
* @param {string} remote Remote alias.
Expand Down
Loading