Skip to content

Instantly share code, notes, and snippets.

@dgwyer
Last active May 24, 2023 16:58
Show Gist options
  • Save dgwyer/1be559032c8019c9a6b6e09086bb58a3 to your computer and use it in GitHub Desktop.
Save dgwyer/1be559032c8019c9a6b6e09086bb58a3 to your computer and use it in GitHub Desktop.
import fs from 'fs';
import needle from 'needle';
import cryptojs from 'crypto-js';
import config from '../../../../caller/deploy.config.js';
export function deployZip() {
// Base 64 URL encode.
const base64_url_encode = function (str) {
str = Buffer.from(str).toString('base64');
str = str.replace(/=/g, '');
return str;
};
const resource_url = '/v1/developers/' + config.developerId + '/plugins/' + config.pluginId + '/tags.json';
const reqUrl = `https://api.freemius.com${resource_url}`;
const boundary = '----' + (new Date().getTime()).toString(16);
const content_md5 = '';
const date = new Date().toUTCString();
const string_to_sign = [
'POST',
content_md5,
'multipart/form-data; boundary=' + boundary,
date,
resource_url
].join("\n");
const hash = cryptojs.HmacSHA256(string_to_sign, config.developerSecretKey);
const base64_hash = base64_url_encode(hash.toString());
const auth = `FS ${config.developerId}:${config.developerPublicKey}:${base64_hash}`;
const buffer = fs.readFileSync(config.zipFilename);
const options = {
multipart: true,
boundary: boundary,
headers: {
"Content-MD5": content_md5,
"Date": date,
"Authorization": auth
}
};
const data = {
release_mode: config.releaseStatus,
add_contributor: config.addContributor,
file: {
buffer: buffer,
filename: config.zipFilename,
content_type: 'application/zip'
}
};
try {
needle.post(reqUrl, data, options, function (error, response, body) {
var message;
if (error) {
console.log('Error deploying to Freemius.');
return;
}
if (typeof body === 'object') {
if (typeof body.error !== 'undefined') {
message = `Error: ${body.error.message}`;
console.log(message);
return;
}
console.log(`Successfully deployed v${body.version} to Freemius.`);
}
});
}
catch (err) {
console.log('Error deploying plugin zip file to Freemius', err);
process.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment