Skip to content

Commit

Permalink
Consistent user handling in CLI and API
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Sep 14, 2018
1 parent 7cf5c66 commit 631de39
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 183 deletions.
61 changes: 21 additions & 40 deletions bin/gh-pages.js
@@ -1,7 +1,6 @@
#!/usr/bin/env node

const ghpages = require('../lib/index');
const Git = require('../lib/git');
const program = require('commander');
const path = require('path');
const pkg = require('../package.json');
Expand All @@ -19,20 +18,6 @@ function publish(config) {
});
}

function getUser(cwd) {
return Promise.all([
new Git(cwd).exec('config', 'user.name'),
new Git(cwd).exec('config', 'user.email')
])
.then(results => {
return {name: results[0].output.trim(), email: results[1].output.trim()};
})
.catch(err => {
// git config exits with 1 if name or email is not set
return null;
});
}

function main(args) {
return Promise.resolve().then(() => {
program
Expand Down Expand Up @@ -74,38 +59,35 @@ function main(args) {
.option('-n, --no-push', 'Commit only (with no push)')
.parse(args);

let userPromise;
let user;
if (program.user) {
const parts = addr.parseOneAddress(program.user);
if (!parts) {
throw new Error(
'Could not parse "Full Name <email@example.com>" from ' + program.user
`Could not parse name and email from user option "${program.user}" ` +
'(format should be "Your Name <email@example.com>")'
);
}
userPromise = Promise.resolve({name: parts.name, email: parts.address});
} else {
userPromise = getUser();
user = {name: parts.name, email: parts.address};
}

return userPromise.then(user => {
const config = {
repo: program.repo,
silent: !!program.silent,
branch: program.branch,
src: program.src,
dest: program.dest,
message: program.message,
tag: program.tag,
dotfiles: !!program.dotfiles,
add: !!program.add,
only: program.remove,
remote: program.remote,
push: !!program.push,
user: user
};
const config = {
repo: program.repo,
silent: !!program.silent,
branch: program.branch,
src: program.src,
dest: program.dest,
message: program.message,
tag: program.tag,
dotfiles: !!program.dotfiles,
add: !!program.add,
only: program.remove,
remote: program.remote,
push: !!program.push,
user: user
};

return publish(config);
});
return publish(config);
});
}

Expand All @@ -115,9 +97,8 @@ if (require.main === module) {
process.stdout.write('Published\n');
})
.catch(err => {
process.stderr.write(err.message, () => process.exit(1));
process.stderr.write(`${err.message}\n`, () => process.exit(1));
});
}

exports = module.exports = main;
exports.getUser = getUser;
205 changes: 105 additions & 100 deletions lib/index.js
@@ -1,6 +1,7 @@
const Git = require('./git');
const filenamify = require('filenamify-url');
const copy = require('./util').copy;
const getUser = require('./util').getUser;
const fs = require('fs-extra');
const globby = require('globby');
const path = require('path');
Expand Down Expand Up @@ -97,111 +98,115 @@ exports.publish = function publish(basePath, config, callback) {
});

let repoUrl;
return getRepo(options)
.then(repo => {
repoUrl = repo;
let clone = options.clone;
if (!clone) {
clone = path.join(getCacheDir(), filenamify(repo));
}
log('Cloning %s into %s', repo, clone);
return Git.clone(repo, clone, options.branch, options);
})
.then(git => {
return git.getRemoteUrl(options.remote).then(url => {
if (url !== repoUrl) {
const message =
'Remote url mismatch. Got "' +
url +
'" ' +
'but expected "' +
repoUrl +
'" in ' +
git.cwd +
'. Try running the `gh-pages-clean` script first.';
throw new Error(message);
let userPromise;
if (options.user) {
userPromise = Promise.resolve(options.user);
} else {
userPromise = getUser();
}
return userPromise.then(user =>
getRepo(options)
.then(repo => {
repoUrl = repo;
const clone = path.join(getCacheDir(), filenamify(repo));
log('Cloning %s into %s', repo, clone);
return Git.clone(repo, clone, options.branch, options);
})
.then(git => {
return git.getRemoteUrl(options.remote).then(url => {
if (url !== repoUrl) {
const message =
'Remote url mismatch. Got "' +
url +
'" ' +
'but expected "' +
repoUrl +
'" in ' +
git.cwd +
'. Try running the `gh-pages-clean` script first.';
throw new Error(message);
}
return git;
});
})
.then(git => {
// only required if someone mucks with the checkout between builds
log('Cleaning');
return git.clean();
})
.then(git => {
log('Fetching %s', options.remote);
return git.fetch(options.remote);
})
.then(git => {
log('Checking out %s/%s ', options.remote, options.branch);
return git.checkout(options.remote, options.branch);
})
.then(git => {
if (!options.add) {
log('Removing files');
return git.rm(only.join(' '));
} else {
return git;
}
return git;
});
})
.then(git => {
// only required if someone mucks with the checkout between builds
log('Cleaning');
return git.clean();
})
.then(git => {
log('Fetching %s', options.remote);
return git.fetch(options.remote);
})
.then(git => {
log('Checking out %s/%s ', options.remote, options.branch);
return git.checkout(options.remote, options.branch);
})
.then(git => {
if (!options.add) {
log('Removing files');
return git.rm(only.join(' '));
} else {
return git;
}
})
.then(git => {
log('Copying files');
return copy(files, basePath, path.join(git.cwd, options.dest)).then(
function() {
})
.then(git => {
log('Copying files');
return copy(files, basePath, path.join(git.cwd, options.dest)).then(
function() {
return git;
}
);
})
.then(git => {
log('Adding all');
return git.add('.');
})
.then(git => {
return git.exec('config', 'user.email', user.email).then(() => {
if (!user.name) {
return git;
}
return git.exec('config', 'user.name', user.name);
});
})
.then(git => {
log('Committing');
return git.commit(options.message);
})
.then(git => {
if (options.tag) {
log('Tagging');
return git.tag(options.tag).catch(error => {
// tagging failed probably because this tag alredy exists
log(error);
log('Tagging failed, continuing');
return git;
});
} else {
return git;
}
);
})
.then(git => {
log('Adding all');
return git.add('.');
})
.then(git => {
if (options.user) {
return git
.exec('config', 'user.email', options.user.email)
.then(() => git.exec('config', 'user.name', options.user.name));
} else {
return git;
}
})
.then(git => {
log('Committing');
return git.commit(options.message);
})
.then(git => {
if (options.tag) {
log('Tagging');
return git.tag(options.tag).catch(error => {
// tagging failed probably because this tag alredy exists
log(error);
log('Tagging failed, continuing');
})
.then(git => {
if (options.push) {
log('Pushing');
return git.push(options.remote, options.branch);
} else {
return git;
});
} else {
return git;
}
})
.then(git => {
if (options.push) {
log('Pushing');
return git.push(options.remote, options.branch);
} else {
return git;
}
})
.then(
() => done(),
error => {
if (options.silent) {
error = new Error(
'Unspecified error (run without silent option for detail)'
);
}
done(error);
}
);
})
.then(
() => done(),
error => {
if (options.silent) {
error = new Error(
'Unspecified error (run without silent option for detail)'
);
}
done(error);
}
)
);
};

/**
Expand Down
16 changes: 15 additions & 1 deletion lib/util.js
@@ -1,5 +1,5 @@
const path = require('path');

const Git = require('./git');
const async = require('async');
const fs = require('graceful-fs');

Expand Down Expand Up @@ -153,3 +153,17 @@ exports.copy = function(files, base, dest) {
});
});
};

exports.getUser = function(cwd) {
return Promise.all([
new Git(cwd).exec('config', 'user.name'),
new Git(cwd).exec('config', 'user.email')
])
.then(results => {
return {name: results[0].output.trim(), email: results[1].output.trim()};
})
.catch(err => {
// git config exits with 1 if name or email is not set
return null;
});
};
20 changes: 0 additions & 20 deletions readme.md
Expand Up @@ -228,26 +228,6 @@ ghpages.publish('dist', {
```


#### <a id="optionsclone">options.clone</a>
* type: `string`
* default: temporary directory inside the `gh-pages` directory

Path to a directory where your repository will be cloned. If this directory doesn't already exist, it will be created. If it already exists, it is assumed to be a clone of your repository.

Example use of the `clone` option:

```js
/**
* If you already have a temp directory, and want the repository cloned there,
* use the `clone` option as below. To avoid re-cloning every time the task is
* run, this should be a directory that sticks around for a while.
*/
ghpages.publish('dist', {
clone: 'path/to/tmp/dir'
}, callback);
```


#### <a id="optionspush">options.push</a>
* type: `boolean`
* default: `true`
Expand Down

0 comments on commit 631de39

Please sign in to comment.