ADD unit tests, split and refactor source code

This commit is contained in:
RecuencoJones
2019-03-25 20:38:29 +01:00
parent 6eaf5fc9af
commit 8257010937
18 changed files with 782 additions and 392 deletions

25
src/assets/binary.js Normal file
View File

@@ -0,0 +1,25 @@
const { join } = require('path');
const { existsSync, renameSync, chmodSync } = require('fs');
const { getInstallationPath } = require('../common');
function verifyAndPlaceBinary(binName, binPath, callback) {
if (!existsSync(join(binPath, binName))) {
return callback(`Downloaded binary does not contain the binary specified in configuration - ${binName}`);
}
getInstallationPath((err, installationPath) => {
if (err) {
return callback(err);
}
// Move the binary file and make sure it is executable
renameSync(join(binPath, binName), join(installationPath, binName));
chmodSync(join(installationPath, binName), '755');
console.log('Placed binary on', join(installationPath, binName));
callback(null);
});
}
module.exports = verifyAndPlaceBinary;

17
src/assets/move.js Normal file
View File

@@ -0,0 +1,17 @@
const { join } = require('path');
const { createWriteStream } = require('fs');
/**
* Move strategy for binary resources without compression.
*/
function move({ opts, req, onSuccess, onError }) {
const stream = createWriteStream(join(opts.binPath, opts.binName));
stream.on('error', onError);
stream.on('close', onSuccess);
req.pipe(stream);
}
module.exports = move;

25
src/assets/untar.js Normal file
View File

@@ -0,0 +1,25 @@
const tar = require('tar');
const zlib = require('zlib');
/**
* Unzip strategy for resources using `.tar.gz`.
*
* First we will Un-GZip, then we will untar. So once untar is completed,
* binary is downloaded into `binPath`. Verify the binary and call it good.
*/
function untar({ opts, req, onSuccess, onError }) {
const ungz = zlib.createGunzip();
const untar = tar.Extract({ path: opts.binPath });
ungz.on('error', onError);
untar.on('error', onError);
// First we will Un-GZip, then we will untar. So once untar is completed,
// binary is downloaded into `binPath`. Verify the binary and call it good
untar.on('end', onSuccess);
req.pipe(ungz).pipe(untar);
}
module.exports = untar;