UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

96 lines 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadPack = exports.installPack = void 0; const _ = require("lodash"); const fs = require("fs"); const path = require("path"); const targz = require("targz"); const sha256File = require("sha256-file"); const admzip = require("adm-zip"); const axios = require("axios"); const util_1 = require("util"); const versionDb_1 = require("./versionDb"); async function installPack(baseDir) { return downloadPack(versionDb_1.versionDb, baseDir, process.platform, process.arch, true); } exports.installPack = installPack; async function downloadPack(versionDb, baseDir, os, arch, force) { // Find pack version based on os let p = _.find(versionDb, (f) => f.os == os && f.arch == arch); if (p !== undefined) { let nDir = path.normalize(baseDir); let installPath = path.join(nDir, 'bin'); let tempPath = path.join(nDir, 'tmp'); // Check if pack already installed if (!verifyInstall(installPath, p.filename, p.filesize)) { // Download and Install Pack await install(p.url, p.os, installPath, tempPath, p.filename, p.sha256, p.compressed); // Verify installation if (!verifyInstall(installPath, p.filename, p.filesize)) { throw new Error('Unable to install pack. Please contact Control Plane support'); } } return path.join(installPath, 'pack'); } else { throw new Error(`Unable to find requested pack version. OS: ${os}, Arch: ${arch}`); } } exports.downloadPack = downloadPack; function verifyInstall(installPath, filename, fileSize) { let fullPath = path.join(installPath, filename); return fs.existsSync(fullPath) && fs.statSync(fullPath).size == fileSize; } function install(url, os, installPath, tempPath, packFilename, checksum, compressed) { return new Promise(async (resolve, reject) => { let filename = path.join(tempPath, path.basename(url)); // Make temp directory fs.mkdirSync(tempPath, { recursive: true }); await axios .default({ method: 'get', url: url, responseType: 'stream', }) .then(function (response) { response.data.pipe(fs.createWriteStream(filename)).on('finish', async function () { // Verify checksum of downloaded file if (sha256File(filename) !== checksum) { // Delete downloaded file fs.unlinkSync(filename); reject(new Error('Invalid checksum for downloaded file')); return; } // Decompress downloaded file if (compressed) { if (os == 'linux' || os == 'darwin') { await (0, util_1.promisify)(targz.decompress)({ src: filename, dest: tempPath }); } else { new admzip(filename).extractAllTo(tempPath, true); } } // Set permissions on pack let tmpPack = path.join(tempPath, packFilename); if (!fs.existsSync(tmpPack)) { // Delete downloaded file fs.unlinkSync(filename); // Reject reject(new Error('Invalid filename for downloaded file')); return; } if (os == 'linux' || os == 'darwin') { fs.chmodSync(tmpPack, 0o755); } // Delete downloaded file fs.unlinkSync(filename); // Make install directory fs.mkdirSync(installPath, { recursive: true }); // Move pack to install path fs.renameSync(tmpPack, path.join(installPath, packFilename)); resolve(); }); }); }); } //# sourceMappingURL=download.js.map