|
| 1 | +/*! |
| 2 | + * node-sass: scripts/upload.js |
| 3 | + */ |
| 4 | +require('../lib/extensions'); |
| 5 | + |
| 6 | +var flags = require('meow')({ pkg: '../' }).flags; |
| 7 | + |
| 8 | +var fetchReleaseInfoUrl = ['https://api.github.com/repos/sass/node-sass/releases/tags/v', |
| 9 | + flags.tag ? flags.tag : require('../package.json').version].join(''), |
| 10 | + file = flags.path ? |
| 11 | + flags.path : |
| 12 | + require('path').resolve(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node'), |
| 13 | + fs = require('fs'), |
| 14 | + os = require('os'), |
| 15 | + request = require('request'), |
| 16 | + uploadReleaseAssetUrl = ['?name=', process.sassBinaryName, '.node', '&label=', process.sassBinaryName].join(''); |
| 17 | + |
| 18 | +/** |
| 19 | + * Upload binary using GitHub API |
| 20 | + * |
| 21 | + * @api private |
| 22 | + */ |
| 23 | + |
| 24 | +function uploadBinary() { |
| 25 | + if (!fs.existsSync(file)) { |
| 26 | + throw new Error('Error reading file.'); |
| 27 | + } |
| 28 | + |
| 29 | + var post = function() { |
| 30 | + request.post({ |
| 31 | + url: uploadReleaseAssetUrl, |
| 32 | + headers: { |
| 33 | + 'Authorization': ['Token ', flags.auth].join(''), |
| 34 | + 'Content-Type': 'application/octet-stream' |
| 35 | + }, |
| 36 | + formData: { |
| 37 | + file: fs.createReadStream(file) |
| 38 | + } |
| 39 | + }, function(err, res, body) { |
| 40 | + if (err) { |
| 41 | + throwFormattedError(err); |
| 42 | + } |
| 43 | + |
| 44 | + var formattedResponse = JSON.parse(body); |
| 45 | + |
| 46 | + if (formattedResponse.errors) { |
| 47 | + throwFormattedError(formattedResponse.errors); |
| 48 | + } |
| 49 | + |
| 50 | + console.log(['Binary uploaded successfully.', |
| 51 | + 'Please test the following link before announcement it:', |
| 52 | + formattedResponse.browser_download_url].join(os.EOL)); |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + request.get({ |
| 57 | + url: fetchReleaseInfoUrl, |
| 58 | + headers: { |
| 59 | + 'User-Agent': 'Node-Sass-Release-Agent' |
| 60 | + } |
| 61 | + }, function(err, res, body) { |
| 62 | + if (err) { |
| 63 | + throw new Error('Error fetching release id.'); |
| 64 | + } |
| 65 | + |
| 66 | + var upload_url = JSON.parse(body).upload_url; |
| 67 | + uploadReleaseAssetUrl = upload_url.replace(/{\?name}/, uploadReleaseAssetUrl); |
| 68 | + |
| 69 | + console.log('Upload URL is:', uploadReleaseAssetUrl); |
| 70 | + |
| 71 | + post(); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +function throwFormattedError(err) { |
| 76 | + throw new Error([ |
| 77 | + 'Error uploading release asset.', |
| 78 | + 'The server returned:', JSON.stringify(err)].join(os.EOL)); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Run |
| 83 | + */ |
| 84 | + |
| 85 | +console.log('Preparing to uploading', file, '..'); |
| 86 | +uploadBinary(); |
0 commit comments