Skip to content

Commit

Permalink
Add "binary.tag" option to specify the tag name separate from the rem…
Browse files Browse the repository at this point in the history
…ote path

Fies #18
  • Loading branch information
thom-nic committed Jan 16, 2017
1 parent c239d3f commit f8fad5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ npm install -g node-pre-gyp-github
```

## Configuration
This module is intended to be used with node-pre-gyp. Therefore, be sure to configure and install node-pre-gyp first. After having done that, within **```package.json```** update the ```binary``` properties ```host``` and ```remote_path``` so it matches the following format:
This module is intended to be used with node-pre-gyp. Therefore, be sure to configure and install node-pre-gyp first. After having done that, within **```package.json```** update the `binary` section properties so they match the following format:

```
"host": "https://github.com/[owner]/[repo]/releases/download/",
"remote_path": "{version}"
"host": "https://github.com",
"remote_path": "/[owner]/[repo]/releases/download/",
"tag": "{version}"
```

Be sure to replace ```[owner]```, ```[repo]```, with actual values,
but DO NOT replace ```{version}``` with actual version.
Be sure to replace `[owner]`, `[repo]`, with actual values,
but DO NOT replace `{version}` with actual version.

***WARNING: Variable substitutions are not supported on the ```host``` property and on the ```remote_path``` only ```{version}``` placeholder is supported. The value of ```remote_path``` after substitution will become a release tag name. Do not use [forbidden git tag characters](https://git-scm.com/docs/git-check-ref-format) for ```version``` and ```remote_path``` properties.***
***WARNING: Variable substitutions are not supported on the `host` or `remote_path` property. Only `{version}` placeholder is supported for the `tag` property. The value of `tag` after substitution will become a release tag name. Do not use [forbidden git tag characters](https://git-scm.com/docs/git-check-ref-format) in your `package.version` or `package.binary.tag` properties.***

Within GitHub, create a new authorization:

Expand Down
40 changes: 23 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ NodePreGypGithub.prototype.stage_dir = path.join(cwd,"build","stage");

NodePreGypGithub.prototype.init = function() {
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){
throw new Error('Missing repository.url in package.json');
}
Expand All @@ -47,17 +47,17 @@ NodePreGypGithub.prototype.init = function() {
}
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('Missing binary.host in package.json');
}
else if (this.package_json.binary.host.substr(0, hostPrefix.length) !== hostPrefix){
throw new Error('binary.host in package.json should begin with: "' + hostPrefix + '"');
}

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

};

NodePreGypGithub.prototype.authenticate_settings = function(){
Expand All @@ -80,13 +80,13 @@ NodePreGypGithub.prototype.createRelease = function(args, callback) {
'draft': true,
'prerelease': false
};

Object.keys(args).forEach(function(key) {
if(args.hasOwnProperty(key) && options.hasOwnProperty(key)) {
options[key] = args[key];
}
});

this.github.authenticate(this.authenticate_settings());
this.github.releases.createRelease(options, callback);
};
Expand All @@ -110,9 +110,9 @@ NodePreGypGithub.prototype.uploadAssets = function(){
consoleLog("Stage directory path: " + path.join(this.stage_dir));
fs.readdir(path.join(this.stage_dir), function(err, files){
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;
Expand Down Expand Up @@ -141,18 +141,24 @@ NodePreGypGithub.prototype.publish = function(options) {
'repo': this.repo
}, function(err, data){
var release;

if(err) throw err;


// prefer `binary.tag` if present, before remote_path - see https://github.com/bchr02/node-pre-gyp-github/issues/18
if (this.package_json.binary.tag) {
options.tag_name = this.package_json.binary.tag.replace(/\{version\}/g, this.package_json.version);
this.stage_dir = path.join(this.stage_dir, options.tag_name);
}
// when remote_path is set expect files to be in stage_dir / remote_path after substitution
if (this.package_json.binary.remote_path) {
// {version} substitution only takes place in `remote_path` if `package_json.binary.tag` is not present:
else if (this.package_json.binary.remote_path) {
options.tag_name = this.package_json.binary.remote_path.replace(/\{version\}/g, this.package_json.version);
this.stage_dir = path.join(this.stage_dir, options.tag_name);
} else {
// This is here for backwards compatibility for before binary.remote_path support was added in version 1.2.0.
options.tag_name = this.package_json.version;
}

release = (function(){ // create a new array containing only those who have a matching version.
if(data) {
data = data.filter(function(element, index, array){
Expand All @@ -162,13 +168,13 @@ NodePreGypGithub.prototype.publish = function(options) {
}
else return [];
}.bind(this))();

this.release = release[0];

if(!release.length) {
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.");
Expand All @@ -185,4 +191,4 @@ NodePreGypGithub.prototype.publish = function(options) {
}.bind(this));
};

module.exports = NodePreGypGithub;
module.exports = NodePreGypGithub;

0 comments on commit f8fad5e

Please sign in to comment.