Skip to content

Commit

Permalink
bug fixes to better error handling after having done testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bchr02 committed May 26, 2016
1 parent ab873a2 commit ae40706
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bin/node-pre-gyp-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var program = require('commander');

program
.command('publish [options]')
.description('publish the contents of .\\bin\\stage to the current version\'s GitHub release')
.description('publishes the contents of .\\build\\stage\\{version} to the current version\'s GitHub release')
.option("-r, --release", "publish immediately, do not create draft")
.action(function(cmd, options){
var opts = {},
Expand All @@ -20,7 +20,7 @@ program
console.log();
console.log('Usage: node-pre-gyp-github publish');
console.log();
console.log('publish the contents of .\\bin\\stage to the current version\'s GitHub release');
console.log('publishes the contents of .\\build\\stage\\{version} to the current version\'s GitHub release');
});

program.parse(process.argv);
Expand Down
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NodePreGypGithub.prototype.init = function() {
this.package_json = JSON.parse(fs.readFileSync(path.join(cwd,'package.json')));

if(!this.package_json.repository || !this.package_json.repository.url){
throw new Error('Error: Missing repository.url in package.json');
throw new Error('Missing repository.url in package.json');
}
else {
ownerRepo = this.package_json.repository.url.match(/github\.com\/(.*)(?=\.git)/i);
Expand All @@ -31,15 +31,15 @@ NodePreGypGithub.prototype.init = function() {
this.owner = ownerRepo[0];
this.repo = ownerRepo[1];
}
else throw new Error('Error: A correctly formatted GitHub repository.url was not found within package.json');
else throw new 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){
throw new Error('Error: Missing binary.host in package.json, configure node-pre-gyp first');
throw new Error('Missing binary.host in package.json');
}
else if (this.package_json.binary.host.substr(0, hostPrefix.length) !== hostPrefix){
throw new Error('Error: binary.host in package.json should begin with: "' + hostPrefix + '"');
throw new Error('binary.host in package.json should begin with: "' + hostPrefix + '"');
}

this.github = new GitHubApi({ // set defaults
Expand All @@ -58,9 +58,11 @@ NodePreGypGithub.prototype.init = function() {
};

NodePreGypGithub.prototype.authenticate_settings = function(){
var token = process.env.NODE_PRE_GYP_GITHUB_TOKEN;
if(!token) throw new Error('NODE_PRE_GYP_GITHUB_TOKEN environment variable not found');
return {
type: "oauth",
token: process.env.NODE_PRE_GYP_GITHUB_TOKEN
"type": "oauth",
"token": token
};
};

Expand Down Expand Up @@ -106,14 +108,14 @@ NodePreGypGithub.prototype.uploadAssets = function(){
fs.readdir(path.join(this.stage_dir), function(err, files){
if(err) throw err;

if(typeof files === 'undefined') throw new Error('No file found to upload');
if(!files.length) throw new Error('No files found within the stage directory: ' + this.stage_dir);

files.forEach(function(file){
asset = this.release.assets.filter(function(element, index, array){
return element.name === file;
});
if(asset.length) {
console.log("Staged file " + file + " found but it already exists in release " + this.release.tag_name + ". If you would like to replace it, you must first manually delete it within GitHub.");
throw new Error("Staged file " + file + " found but it already exists in release " + this.release.tag_name + ". If you would like to replace it, you must first manually delete it within GitHub.");
}
else {
console.log("Staged file " + file + " found. Proceeding to upload it.");
Expand Down Expand Up @@ -158,11 +160,12 @@ NodePreGypGithub.prototype.publish = function(options) {
if(!release.length) {
this.createRelease(options, function(err, release) {
if(err) throw err;

this.release = release;
if (release.draft) {
console.log('Release ' + release.tag_name + " not found, so a draft release was created. YOU MUST MANUALLY PUBLISH THIS DRAFT WITHIN GITHUB FOR IT TO BE ACCESSIBLE.");
} else {
}
else {
console.log('Release ' + release.tag_name + " not found, so a new release was created and published.");
}
this.uploadAssets();
Expand Down

0 comments on commit ae40706

Please sign in to comment.