Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-pre-gyp-github

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-pre-gyp-github - npm Package Compare versions

Comparing version 1.3.1 to 1.4.0

112

index.js

@@ -5,3 +5,3 @@ "use strict";

var fs = require('fs');
var GitHubApi = require("github");
var mime = require("mime-types");
var cwd = process.cwd();

@@ -15,21 +15,4 @@

function NodePreGypGithub() {}
NodePreGypGithub.prototype.github = new GitHubApi({ // set defaults
// required
version: "3.0.0",
// optional
debug: false,
protocol: "https",
host: "api.github.com",
pathPrefix: "", // for some GHEs; none for GitHub
timeout: 5000,
headers: {}
});
NodePreGypGithub.prototype.owner = "";
NodePreGypGithub.prototype.repo = "";
NodePreGypGithub.prototype.package_json = {};
NodePreGypGithub.prototype.release = {};
NodePreGypGithub.prototype.octokit = require("@octokit/rest");
NodePreGypGithub.prototype.stage_dir = path.join(cwd,"build","stage");
NodePreGypGithub.prototype.init = function() {

@@ -44,5 +27,6 @@ var ownerRepo, hostPrefix;

else {
ownerRepo = this.package_json.repository.url.match(/github\.com\/(.*)(?=\.git)/i);
ownerRepo = this.package_json.repository.url.match(/https?:\/\/([^\/]+)\/(.*)(?=\.git)/i);
if(ownerRepo) {
ownerRepo = ownerRepo[1].split('/');
this.host = ownerRepo[1];
ownerRepo = ownerRepo[2].split('/');
this.owner = ownerRepo[0];

@@ -54,3 +38,3 @@ this.repo = ownerRepo[1];

hostPrefix = 'https://github.com/' + this.owner + '/' + this.repo + '/releases/download/';
hostPrefix = 'https://' + this.host + '/' + this.owner + '/' + this.repo + '/releases/download/';
if(!this.package_json.binary || 'object' !== typeof this.package_json.binary || 'string' !== typeof this.package_json.binary.host){

@@ -62,5 +46,9 @@ throw new Error('Missing binary.host in package.json');

}
this.github.headers = {"user-agent": (this.package_json.name) ? this.package_json.name : "node-pre-gyp-github"}; // GitHub is happy with a unique user agent
this.octokit = NodePreGypGithub.prototype.octokit({
baseUrl: 'https://' + this.host + '/api/v3',
headers: {
"user-agent": (this.package_json.name) ? this.package_json.name : "node-pre-gyp-github"
}
});
};

@@ -79,2 +67,3 @@

var options = {
'host': this.host,
'owner': this.owner,

@@ -95,10 +84,10 @@ 'repo': this.repo,

});
this.github.authenticate(this.authenticate_settings());
this.github.releases.createRelease(options, callback);
this.octokit.authenticate(this.authenticate_settings());
this.octokit.repos.createRelease(options, callback);
};
NodePreGypGithub.prototype.uploadAsset = function(cfg){
this.github.authenticate(this.authenticate_settings());
this.github.releases.uploadAsset({
this.octokit.authenticate(this.authenticate_settings());
this.octokit.repos.uploadAsset({
url: this.release.upload_url,
owner: this.owner,

@@ -108,3 +97,5 @@ id: this.release.id,

name: cfg.fileName,
filePath: cfg.filePath
file: cfg.filePath,
contentType: mime.contentType(cfg.fileName) || 'application/octet-stream',
contentLength: fs.statSync(cfg.filePath).size,
}, function(err){

@@ -125,15 +116,15 @@ if(err) throw err;

files.forEach(function(file){
asset = this.release.assets.filter(function(element, index, array){
return element.name === file;
if(this.release && this.release.assets) {
asset = this.release.assets.filter(function(element, index, array){
return element.name === file;
});
if(asset.length) {
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.");
}
}
consoleLog("Staged file " + file + " found. Proceeding to upload it.");
this.uploadAsset({
fileName: file,
filePath: path.join(this.stage_dir, file)
});
if(asset.length) {
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 {
consoleLog("Staged file " + file + " found. Proceeding to upload it.");
this.uploadAsset({
fileName: file,
filePath: path.join(this.stage_dir, file)
});
}
}.bind(this));

@@ -147,4 +138,4 @@ }.bind(this));

this.init();
this.github.authenticate(this.authenticate_settings());
this.github.releases.listReleases({
this.octokit.authenticate(this.authenticate_settings());
this.octokit.repos.getReleases({
'owner': this.owner,

@@ -154,3 +145,2 @@ 'repo': this.repo

var release;
if(err) throw err;

@@ -166,22 +156,11 @@

}
release = (function(){ // create a new array containing only those who have a matching version.
if(data) {
data = data.filter(function(element, index, array){
return element.tag_name === options.tag_name;
}.bind(this));
return data;
}
else return [];
}.bind(this))();
this.release = release[0];
if(!release.length) {
release = data.data.filter(function(element, index, array){
return element.tag_name === options.tag_name;
});
if(release.length === 0) {
this.createRelease(options, function(err, release) {
if(err) throw err;
this.release = release;
if (release.draft) {
consoleLog('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.");
this.release = release.data;
if (this.release.draft) {
consoleLog('Release ' + this.release.tag_name + " not found, so a draft release was created. YOU MUST MANUALLY PUBLISH THIS DRAFT WITHIN GITHUB FOR IT TO BE ACCESSIBLE.");
}

@@ -191,6 +170,7 @@ else {

}
this.uploadAssets();
this.uploadAssets(this.release.upload_url);
}.bind(this));
}
else {
this.release = release[0];
this.uploadAssets();

@@ -201,2 +181,2 @@ }

module.exports = NodePreGypGithub;
module.exports = NodePreGypGithub;
{
"name": "node-pre-gyp-github",
"version": "1.3.1",
"version": "1.4.0",
"description": "A node-pre-gyp module which provides the ability to publish to GitHub releases.",

@@ -28,4 +28,6 @@ "bin": "./bin/node-pre-gyp-github.js",

"dependencies": {
"github": "^0.2.4",
"commander": "^2.9.0"
"@octokit/rest": "^15.9.5",
"commander": "^2.9.0",
"mime-types": "^2.1.19",
"sinon": "^6.1.4"
},

@@ -32,0 +34,0 @@ "devDependencies": {

@@ -8,2 +8,5 @@ "use strict";

var stage_dir = index.stage_dir;
var Octokit = require("@octokit/rest");
var octokit = Octokit();
var sinon = require('sinon')
var reset_index = function(index_string_ref) {

@@ -13,3 +16,7 @@ delete require.cache[require.resolve(index_string_ref)];

};
var sandbox = sinon.createSandbox();
var reset_mocks = function() {
sandbox.restore();
process.env.NODE_PRE_GYP_GITHUB_TOKEN = "secret";

@@ -19,10 +26,11 @@ fs = reset_index('fs');

index.stage_dir = stage_dir;
index.github.authenticate = function(){};
index.github.releases.listReleases = function(options, cb){
cb(null, [{"tag_name":"0.0.0","assets":[{"name":"filename"}]}]);
};
index.github.releases.createRelease = function(options, cb){
cb(null,{"tag_name":"0.0.1","draft":true,"assets":[{}]});
};
index.github.releases.uploadAsset = function(cfg,cb){cb();};
Index.prototype.octokit = function() {return octokit;};
sandbox.stub(octokit, 'authenticate');
sandbox.stub(octokit.repos, 'getReleases').callsFake(function(options, cb){
cb(null, {data: [{"tag_name":"0.0.0","assets":[{"name":"filename"}]}]});
});
sandbox.stub(octokit.repos, 'createRelease').callsFake(function(options, cb){
cb(null,{data: {"tag_name":"0.0.1","draft":true,"assets":[{}]}});
});
sandbox.stub(octokit.repos, 'uploadAsset').callsFake(function(cfg,cb){cb();});
};

@@ -43,5 +51,4 @@

};
index.github.releases.createRelease = function(options, cb){
cb(null,{"tag_name":"0.0.1","draft":false,"assets":[{}]});
};
fs.statSync = function() {return 0;}
index.publish(options)
expect(function(){ index.publish(options); }).to.not.throw();

@@ -98,9 +105,11 @@ });

it("should throw an error when github.releases.listReleases returns an error", function() {
it("should throw an error when octokit.repos.getReleases returns an error", function() {
var options = {'draft': true, 'verbose': false};
reset_mocks();
index.github.releases.listReleases = function(options, cb){
cb(new Error('listReleases error'));
};
expect(function(){ index.publish(options); }).to.throw('listReleases error');
octokit.repos.getReleases.restore();
sandbox.stub(octokit.repos, 'getReleases').callsFake(function(options, cb){
cb(new Error('getReleases error'));
});
expect(function(){ index.publish(options); }).to.throw('getReleases error');
});

@@ -111,6 +120,6 @@

reset_mocks();
index.github.releases.listReleases = function(options, cb){
cb(null,null);
octokit.repos.getReleases = function(options, cb){
cb(null,{data: []});
};
index.github.releases.createRelease = function(options, cb){
octokit.repos.createRelease = function(options, cb){
cb(new Error('createRelease error'));

@@ -145,4 +154,4 @@ };

};
index.github.releases.listReleases = function(options, cb){
cb(null, [{"tag_name":"0.0.1","assets":[{"name":"filename"}]}]);
octokit.repos.getReleases = function(options, cb){
cb(null, {data: [{"tag_name":"0.0.1","assets":[{"name":"filename"}]}]});
};

@@ -158,3 +167,3 @@ expect(function(){ index.publish(options); }).to.throw(/^Staged file .* found but it already exists in release .*. If you would like to replace it, you must first manually delete it within GitHub./i);

};
index.github.releases.uploadAsset = function(cfg,cb){
octokit.repos.uploadAsset = function(cfg,cb){
cb(new Error('uploadAsset error'));

@@ -176,4 +185,5 @@ };

};
index.github.releases.createRelease = function(options, cb){
cb(null,{"tag_name":"0.0.1","draft":false,"assets":[{}]});
fs.statSync = function() {return 0;}
octokit.reposcreateRelease = function(options, cb){
cb(null,{data: {"tag_name":"0.0.1","draft":false,"assets":[{}]}});
};

@@ -184,2 +194,2 @@ expect(function(){ index.publish(options); }).to.not.throw();

});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc