This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #737 from am11/master
Build: Implements GH API to upload binaries
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/*! | ||
* node-sass: scripts/upload.js | ||
*/ | ||
require('../lib/extensions'); | ||
|
||
var flags = require('meow')({ pkg: '../' }).flags; | ||
|
||
var fetchReleaseInfoUrl = ['https://api.github.com/repos/sass/node-sass/releases/tags/v', | ||
flags.tag ? flags.tag : require('../package.json').version].join(''), | ||
file = flags.path ? | ||
flags.path : | ||
require('path').resolve(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node'), | ||
fs = require('fs'), | ||
os = require('os'), | ||
request = require('request'), | ||
uploadReleaseAssetUrl = ['?name=', process.sassBinaryName, '.node', '&label=', process.sassBinaryName].join(''); | ||
|
||
/** | ||
* Upload binary using GitHub API | ||
* | ||
* @api private | ||
*/ | ||
|
||
function uploadBinary() { | ||
if (!fs.existsSync(file)) { | ||
throw new Error('Error reading file.'); | ||
} | ||
|
||
var post = function() { | ||
request.post({ | ||
url: uploadReleaseAssetUrl, | ||
headers: { | ||
'Authorization': ['Token ', flags.auth].join(''), | ||
'Content-Type': 'application/octet-stream' | ||
}, | ||
formData: { | ||
file: fs.createReadStream(file) | ||
} | ||
}, function(err, res, body) { | ||
if (err) { | ||
throwFormattedError(err); | ||
} | ||
|
||
var formattedResponse = JSON.parse(body); | ||
|
||
if (formattedResponse.errors) { | ||
throwFormattedError(formattedResponse.errors); | ||
} | ||
|
||
console.log(['Binary uploaded successfully.', | ||
'Please test the following link before announcement it:', | ||
formattedResponse.browser_download_url].join(os.EOL)); | ||
}); | ||
}; | ||
|
||
request.get({ | ||
url: fetchReleaseInfoUrl, | ||
headers: { | ||
'User-Agent': 'Node-Sass-Release-Agent' | ||
} | ||
}, function(err, res, body) { | ||
if (err) { | ||
throw new Error('Error fetching release id.'); | ||
} | ||
|
||
var upload_url = JSON.parse(body).upload_url; | ||
uploadReleaseAssetUrl = upload_url.replace(/{\?name}/, uploadReleaseAssetUrl); | ||
|
||
console.log('Upload URL is:', uploadReleaseAssetUrl); | ||
|
||
post(); | ||
}); | ||
} | ||
|
||
function throwFormattedError(err) { | ||
throw new Error([ | ||
'Error uploading release asset.', | ||
'The server returned:', JSON.stringify(err)].join(os.EOL)); | ||
} | ||
|
||
/** | ||
* Run | ||
*/ | ||
|
||
console.log('Preparing to uploading', file, '..'); | ||
uploadBinary(); |