node-pre-gyp-github
Advanced tools
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(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19450
4
6
337
+ Added@octokit/rest@^15.9.5
+ Addedmime-types@^2.1.19
+ Addedsinon@^6.1.4
+ Added@octokit/rest@15.18.3(transitive)
+ Added@sinonjs/commons@1.8.6(transitive)
+ Added@sinonjs/formatio@3.2.2(transitive)
+ Added@sinonjs/samsam@2.1.33.3.3(transitive)
+ Added@sinonjs/text-encoding@0.7.3(transitive)
+ Addedagent-base@4.3.0(transitive)
+ Addedarray-from@2.1.1(transitive)
+ Addedbefore-after-hook@1.4.0(transitive)
+ Addedbtoa-lite@1.0.0(transitive)
+ Addedcross-spawn@6.0.5(transitive)
+ Addeddebug@3.1.03.2.7(transitive)
+ Addeddiff@3.5.0(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedes6-promise@4.2.8(transitive)
+ Addedes6-promisify@5.0.0(transitive)
+ Addedexeca@1.0.0(transitive)
+ Addedget-stream@4.1.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedhttp-proxy-agent@2.1.0(transitive)
+ Addedhttps-proxy-agent@2.2.4(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjust-extend@4.2.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash.get@4.4.2(transitive)
+ Addedlolex@2.7.55.1.2(transitive)
+ Addedmacos-release@2.5.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednice-try@1.0.5(transitive)
+ Addednise@1.5.3(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedos-name@3.1.0(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpath-to-regexp@1.9.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsinon@6.3.5(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedtype-detect@4.0.84.1.0(transitive)
+ Addeduniversal-user-agent@2.1.0(transitive)
+ Addedurl-template@2.0.8(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedwindows-release@3.3.3(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedgithub@^0.2.4
- Removedgithub@0.2.4(transitive)
- Removedmime@1.6.0(transitive)