btnexus-node
Version:
Baseline libary for btNexus development.
129 lines (112 loc) • 3.6 kB
JavaScript
/**
* Blackout Technologies Nexus, Hook data abstract
* @author Marc Fiedler, Kejsi Sali
* @copyright 2020 Blackout Technologies, all rights reserved
*/
// Use Strict mode ECMA Script 5+
"use_strict";
const fs = require('fs');
const request = require('../lib/request');
const chalk = require('chalk');
const md5file = require('md5-file');
var root = process.cwd();
var connectHash = process.env.CONNECT_HASH;
var manifest = JSON.parse(fs.readFileSync(root+"/package.json", "utf-8"));
var flavor = "";
if( connectHash == undefined ){
try{
connectHash = fs.readFileSync(root+"/.btnexusrc", "utf-8");
}catch(e){
console.error(e);
process.exit(-1);
}
}
if( manifest.type == "web-app" ){
try{
flavor = "-"+fs.readFileSync(root+"/dist/flavor", "utf-8");
}catch(e){
console.error(e);
process.exit(-1);
}
}
var config = undefined;
var buffer = Buffer.from(connectHash, 'base64');
try{
config = JSON.parse(buffer.toString('ascii'));
}catch(e){
console.error("Invalid connect hash! Connect impossible");
process.exit(-2);
}
const bundle = manifest.name+flavor+'-'+manifest.version+'.pck';
const path = process.env.HOME+'/.btnexus/';
try {
if (fs.existsSync(path+bundle)) {
//file exists
console.log(
chalk.green("[")+chalk.blue("btNexus")+chalk.green("]")+": Dispatching "+chalk.yellow(bundle)
);
}
} catch(err) {
console.log(
chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+": Error unable to dispatch "+chalk.yellow(bundle)+". File does not exist."
);
process.exit(-3);
}
const instanceId = config.hostId;
const instanceHost = config.host;
const marketHost = config.marketHost;
const token = config.token;
const bundleId = config.id;
const portalToken = config.portalToken;
const bundleVersion = config.version;
// provide checksum
const bundleChecksum = md5file.sync(path+bundle);
const formData = {
// Pass a simple key-value pair
btNexus: 'market-upload',
btInstanceId: instanceId,
btInstance: instanceHost,
btToken: token,
btPortalToken: portalToken,
// Pass data via Streams
btBundleId: bundleId,
btBundleVersion: bundleVersion,
btBundleChecksum: bundleChecksum,
btBundle: fs.createReadStream(path+bundle)
};
var error = false;
var bundleKeys = Object.keys(formData);
for( var i in bundleKeys ){
var key = bundleKeys[i];
if( formData[key] == undefined ){
console.log(
chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+" Your connect hash seems to be outdated! "+key+" does not exist in your connect hash. Please make sure that you have the newest version of both btNexus and the btNexus Hook library"
);
error = true;
break;
}
}
if( error ){
process.exit(-4);
}
request.post(marketHost+'/upload', formData, (err, resp, code) => {
// console.dir( httpResponse );
// console.log("Code: "+httpResponse.code);
if (err) {
console.log(
chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+": upload failed: "+err
);
}else{
if( code != 200 ){
console.log(
chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+" "+chalk.yellow("Upload failed: ")+": "+body
);
process.exit(-4);
}else{
console.log(
chalk.green("[")+chalk.blue("btNexus")+chalk.green("]")+" "+chalk.yellow("Upload successful: ")+": "+body
);
process.exit(0); // success
}
}
});