mirror of
https://github.com/ershisan99/go-npm.git
synced 2026-01-31 05:12:14 +00:00
ADD unit tests, split and refactor source code
This commit is contained in:
25
src/assets/binary.js
Normal file
25
src/assets/binary.js
Normal 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
17
src/assets/move.js
Normal 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
25
src/assets/untar.js
Normal 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;
|
||||
Reference in New Issue
Block a user