Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bchr02 committed May 26, 2016
1 parent 8426d75 commit ab873a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
41 changes: 17 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,31 @@ NodePreGypGithub.prototype.release = {};
NodePreGypGithub.prototype.stage_dir = path.join(cwd,"build","stage");

NodePreGypGithub.prototype.init = function() {
var ownerRepo, hostPrefix,
error = function(){
process.exit(1);
};
var ownerRepo, hostPrefix;

this.package_json = JSON.parse(fs.readFileSync(path.join(cwd,'package.json')));

if(!this.package_json.repository || !this.package_json.repository.url){
console.error('Error: Missing repository.url in package.json');
error();
throw new Error('Error: Missing repository.url in package.json');
}
else {
ownerRepo = this.package_json.repository.url.match(/github\.com\/(.*)(?=\.git)/i);
if (ownerRepo) {
if(ownerRepo) {
ownerRepo = ownerRepo[1].split('/');
this.owner = ownerRepo[0];
this.repo = ownerRepo[1];
}
else {
console.error('Error: Not a GitHub repository.url in package.json');
error();
}
else throw new Error('Error: A correctly formatted GitHub repository.url was not found within package.json');
}

hostPrefix = 'https://github.com/' + this.owner + '/' + this.repo + '/releases/download/';
if(!this.package_json.binary || 'object' !== typeof this.package_json.binary ||
'string' !== typeof this.package_json.binary.host){
console.error('Error: Missing binary.host in package.json, configure node-pre-gyp first');
error();
if(!this.package_json.binary || 'object' !== typeof this.package_json.binary || 'string' !== typeof this.package_json.binary.host){
throw new Error('Error: Missing binary.host in package.json, configure node-pre-gyp first');
}
else if (this.package_json.binary.host.substr(0, hostPrefix.length) !== hostPrefix){
console.error('Error: binary.host in package.json should begin with: "' + hostPrefix + '"');
error();
throw new Error('Error: binary.host in package.json should begin with: "' + hostPrefix + '"');
}

this.github = new GitHubApi({ // set defaults
// required
version: "3.0.0",
Expand Down Expand Up @@ -105,7 +95,7 @@ NodePreGypGithub.prototype.uploadAsset = function(cfg){
name: cfg.fileName,
filePath: cfg.filePath
}, function(err){
if(err) {console.error(err); return;}
if(err) throw err;
console.log('Staged file ' + cfg.fileName + ' saved to ' + this.owner + '/' + this.repo + ' release ' + this.release.tag_name + ' successfully.');
}.bind(this));
};
Expand All @@ -114,7 +104,10 @@ NodePreGypGithub.prototype.uploadAssets = function(){
var asset;
console.log("Stage directory path: " + path.join(this.stage_dir));
fs.readdir(path.join(this.stage_dir), function(err, files){
if(typeof files === 'undefined') {console.log('no files found'); return;}
if(err) throw err;

if(typeof files === 'undefined') throw new Error('No file found to upload');

files.forEach(function(file){
asset = this.release.assets.filter(function(element, index, array){
return element.name === file;
Expand Down Expand Up @@ -142,7 +135,7 @@ NodePreGypGithub.prototype.publish = function(options) {
}, function(err, data){
var release;

if(err) {console.error(err); return;}
if(err) throw err;

// when remote_path is set expect files to be in stage_dir / remote_path after substitution
if (this.package_json.binary.remote_path) {
Expand All @@ -164,7 +157,7 @@ NodePreGypGithub.prototype.publish = function(options) {

if(!release.length) {
this.createRelease(options, function(err, release) {
if(err) {console.error(err); return;}
if(err) throw err;

this.release = release;
if (release.draft) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-pre-gyp-github",
"version": "1.2.0",
"version": "1.2.1",
"description": "A node-pre-gyp module which provides the ability to publish to GitHub releases.",
"bin": "./bin/node-pre-gyp-github.js",
"main": "index.js",
Expand Down

0 comments on commit ab873a2

Please sign in to comment.