Skip to content

Commit

Permalink
Merge pull request #14 from bchr02/dev
Browse files Browse the repository at this point in the history
**Changes:**

* made error handling more consistent and always exit with a non 0 error code. #13
* an error will now occur if NODE_PRE_GYP_GITHUB_TOKEN is not found.
* slightly changed some of the error messages and removed some of the prepended "Error: ". They are no longer needed since were are now throwing errors.
* fixed a bug that caused an error to not occur when binary files were not found in the stage directory.
* added a missing error catch for readdir that caused an error to not occur if the stage directory was missing.
* in bin/node-pre-gyp-github.js, corrected the path that was displayed in the help text.
  • Loading branch information
bchr02 committed May 26, 2016
2 parents 8426d75 + ae40706 commit 0803481
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 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
54 changes: 25 additions & 29 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('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('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('Missing binary.host in package.json');
}
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('binary.host in package.json should begin with: "' + hostPrefix + '"');
}

this.github = new GitHubApi({ // set defaults
// required
version: "3.0.0",
Expand All @@ -68,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 @@ -105,7 +97,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,13 +106,16 @@ 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(!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 All @@ -142,7 +137,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,12 +159,13 @@ 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) {
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
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 0803481

Please sign in to comment.