Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #264 from tschaub/user-option
Better user handling
  • Loading branch information
tschaub committed Sep 14, 2018
2 parents 32fdb32 + 631de39 commit 515b05a
Show file tree
Hide file tree
Showing 14 changed files with 704 additions and 613 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,6 +3,7 @@ sudo: false

language: node_js
node_js:
- "6"
- "8"
- "10"

Expand Down
128 changes: 79 additions & 49 deletions bin/gh-pages.js
Expand Up @@ -4,46 +4,74 @@ const ghpages = require('../lib/index');
const program = require('commander');
const path = require('path');
const pkg = require('../package.json');
const addr = require('email-addresses');

function publish(config) {
return new Promise((resolve, reject) => {
const basePath = path.join(process.cwd(), program.dist);
ghpages.publish(basePath, config, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}

function main(args) {
program
.version(pkg.version)
.option('-d, --dist <dist>', 'Base directory for all source files')
.option(
'-s, --src <src>',
'Pattern used to select which files to publish',
'**/*'
)
.option(
'-b, --branch <branch>',
'Name of the branch you are pushing to',
'gh-pages'
)
.option(
'-e, --dest <dest>',
'Target directory within the destination branch (relative to the root)',
'.'
)
.option('-a, --add', 'Only add, and never remove existing files')
.option('-x, --silent', 'Do not output the repository url')
.option('-m, --message <message>', 'commit message', 'Updates')
.option('-g, --tag <tag>', 'add tag to commit')
.option('-t, --dotfiles', 'Include dotfiles')
.option('-r, --repo <repo>', 'URL of the repository you are pushing to')
.option('-p, --depth <depth>', 'depth for clone', 1)
.option('-o, --remote <name>', 'The name of the remote', 'origin')
.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(args);
return Promise.resolve().then(() => {
program
.version(pkg.version)
.option('-d, --dist <dist>', 'Base directory for all source files')
.option(
'-s, --src <src>',
'Pattern used to select which files to publish',
'**/*'
)
.option(
'-b, --branch <branch>',
'Name of the branch you are pushing to',
'gh-pages'
)
.option(
'-e, --dest <dest>',
'Target directory within the destination branch (relative to the root)',
'.'
)
.option('-a, --add', 'Only add, and never remove existing files')
.option('-x, --silent', 'Do not output the repository url')
.option('-m, --message <message>', 'commit message', 'Updates')
.option('-g, --tag <tag>', 'add tag to commit')
.option('-t, --dotfiles', 'Include dotfiles')
.option('-r, --repo <repo>', 'URL of the repository you are pushing to')
.option('-p, --depth <depth>', 'depth for clone', 1)
.option('-o, --remote <name>', 'The name of the remote', 'origin')
.option(
'-u, --user <address>',
'The name and email of the user (defaults to the git config). Format is "Your Name <email@example.com>".'
)
.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(args);

ghpages.publish(
path.join(process.cwd(), program.dist),
{
let user;
if (program.user) {
const parts = addr.parseOneAddress(program.user);
if (!parts) {
throw new Error(
`Could not parse name and email from user option "${program.user}" ` +
'(format should be "Your Name <email@example.com>")'
);
}
user = {name: parts.name, email: parts.address};
}

const config = {
repo: program.repo,
silent: !!program.silent,
branch: program.branch,
Expand All @@ -55,20 +83,22 @@ function main(args) {
add: !!program.add,
only: program.remove,
remote: program.remote,
push: !!program.push
},
function(err) {
if (err) {
process.stderr.write(err.message + '\n');
return process.exit(1);
}
process.stderr.write('Published\n');
}
);
push: !!program.push,
user: user
};

return publish(config);
});
}

if (require.main === module) {
main(process.argv);
main(process.argv)
.then(() => {
process.stdout.write('Published\n');
})
.catch(err => {
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}

module.exports = main;
exports = module.exports = main;
60 changes: 23 additions & 37 deletions lib/git.js
Expand Up @@ -26,16 +26,16 @@ util.inherits(ProcessError, Error);
* @return {Promise} A promise.
*/
function spawn(exe, args, cwd) {
return new Promise(function(resolve, reject) {
return new Promise((resolve, reject) => {
const child = cp.spawn(exe, args, {cwd: cwd || process.cwd()});
const buffer = [];
child.stderr.on('data', function(chunk) {
child.stderr.on('data', chunk => {
buffer.push(chunk.toString());
});
child.stdout.on('data', function(chunk) {
child.stdout.on('data', chunk => {
buffer.push(chunk.toString());
});
child.on('close', function(code) {
child.on('close', code => {
const output = buffer.join('');
if (code) {
const msg = output || 'Process failed: ' + code;
Expand Down Expand Up @@ -66,17 +66,15 @@ function Git(cwd, cmd) {
* or rejected with an error.
*/
Git.prototype.exec = function() {
return spawn(this.cmd, [].slice.call(arguments), this.cwd).then(
function(output) {
this.output = output;
return this;
}.bind(this)
);
return spawn(this.cmd, [].slice.call(arguments), this.cwd).then(output => {
this.output = output;
return this;
});
};

/**
* Initialize repository.
* @return {ChildProcess} Child process.
* @return {Promise} A promise.
*/
Git.prototype.init = function() {
return this.exec('init');
Expand Down Expand Up @@ -118,29 +116,21 @@ Git.prototype.fetch = function(remote) {
Git.prototype.checkout = function(remote, branch) {
const treeish = remote + '/' + branch;
return this.exec('ls-remote', '--exit-code', '.', treeish).then(
function() {
() => {
// branch exists on remote, hard reset
return this.exec('checkout', branch)
.then(
function() {
return this.clean();
}.bind(this)
)
.then(
function() {
return this.reset(remote, branch);
}.bind(this)
);
}.bind(this),
function(error) {
.then(() => this.clean())
.then(() => this.reset(remote, branch));
},
error => {
if (error instanceof ProcessError && error.code === 2) {
// branch doesn't exist, create an orphan
return this.exec('checkout', '--orphan', branch);
} else {
// unhandled error
throw error;
}
}.bind(this)
}
);
};

Expand Down Expand Up @@ -168,10 +158,8 @@ Git.prototype.add = function(files) {
* @return {Promise} A promise.
*/
Git.prototype.commit = function(message) {
return this.exec('diff-index', '--quiet', 'HEAD').catch(
function() {
return this.exec('commit', '-m', message);
}.bind(this)
return this.exec('diff-index', '--quiet', 'HEAD').catch(() =>
this.exec('commit', '-m', message)
);
};

Expand Down Expand Up @@ -201,7 +189,7 @@ Git.prototype.push = function(remote, branch) {
*/
Git.prototype.getRemoteUrl = function(remote) {
return this.exec('config', '--get', 'remote.' + remote + '.url')
.then(function(git) {
.then(git => {
const repo = git.output && git.output.split(/[\n\r]/).shift();
if (repo) {
return repo;
Expand All @@ -211,7 +199,7 @@ Git.prototype.getRemoteUrl = function(remote) {
);
}
})
.catch(function(err) {
.catch(err => {
throw new Error(
'Failed to get remote.' +
remote +
Expand All @@ -233,11 +221,11 @@ Git.prototype.getRemoteUrl = function(remote) {
* @return {Promise<Git>} A promise.
*/
Git.clone = function clone(repo, dir, branch, options) {
return fs.exists(dir).then(function(exists) {
return fs.exists(dir).then(exists => {
if (exists) {
return Promise.resolve(new Git(dir, options.git));
} else {
return fs.mkdirp(path.dirname(path.resolve(dir))).then(function() {
return fs.mkdirp(path.dirname(path.resolve(dir))).then(() => {
const args = [
'clone',
repo,
Expand All @@ -251,7 +239,7 @@ Git.clone = function clone(repo, dir, branch, options) {
options.depth
];
return spawn(options.git, args)
.catch(function(err) {
.catch(err => {
// try again without banch or depth options
return spawn(options.git, [
'clone',
Expand All @@ -261,9 +249,7 @@ Git.clone = function clone(repo, dir, branch, options) {
options.remote
]);
})
.then(function() {
return new Git(dir, options.git);
});
.then(() => new Git(dir, options.git));
});
}
});
Expand Down

0 comments on commit 515b05a

Please sign in to comment.