@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
86 lines • 5.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const console_1 = require("console");
const fs = require("fs");
const path = require("path");
const util = require("util");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const ora = require("ora-classic");
const streamPipeline = util.promisify(require("stream").pipeline);
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("download-file")
.alias("df")
.option("-f, --file <fileToDownload>", "file to download from the file service")
.option("-h, --filepath [filepath]", "file path in the mindsphere", "")
.option("-i, --assetid <assetid>", "mindsphere asset id")
.option("-p, --passkey <passkey>", `passkey`)
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(`${color("download the file from mindsphere file service *")}`)
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkParameters(options);
color = (0, command_utils_1.adjustColor)(color, options);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
const sdk = (0, command_utils_1.getSdk)(options);
const iotFileClient = sdk.GetIoTFileClient();
let fullpath = options.filepath ? `${options.filepath}/${options.file}` : `${options.file}`;
fullpath = fullpath.replace("//", "/");
let filter = `name eq ${path.basename(fullpath)}`;
if (path.dirname(fullpath) !== ".")
filter += ` and path eq ${path.dirname(fullpath)}/`;
const fileInfo = yield iotFileClient.GetFiles(options.assetid, {
filter: filter,
});
fileInfo.length !== 1 &&
(0, command_utils_1.errorLog)(`There were ${color(fileInfo.length)} files found with that name and path.`, true);
const spinner = ora("downloading file");
!options.verbose && spinner.start();
(0, command_utils_1.verboseLog)(JSON.stringify(fileInfo, null, 2), options.verbose);
(0, command_utils_1.verboseLog)(`Downloading file ${color(fileInfo[0].name)} with size of ${color((0, command_utils_1.humanFileSize)(fileInfo[0].size || 0))} from MindSphere.`, options.verbose, spinner);
const startDate = new Date();
const download = yield (0, utils_1.retry)(options.retry, () => iotFileClient.GetFile(options.assetid, fullpath));
!download.ok && (0, utils_1.throwError)(`Unexpected response ${download.statusText}`);
const outputfile = fileInfo[0].name || "output.bin";
const file = fs.createWriteStream(outputfile);
yield streamPipeline(download.body, file);
const endDate = new Date();
!options.verbose && spinner.succeed("Done");
const hash = yield (0, utils_1.checksumFile)("md5", outputfile);
(0, console_1.log)(`Download time: ${(endDate.getTime() - startDate.getTime()) / 1000} seconds`);
(0, console_1.log)(`\nYour file ${color(outputfile)} with md5 hash ${hash} was successfully downloaded`);
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp download-file -f CHANGELOG.md --assetid 5..f \t\t\t download file ${color("CHANGELOG.md")} from specified asset`);
(0, console_1.log)(` mdsp download-file --file CHANGELOG.md --assetid 5...f --filepath upload \t download file ${color("upload/CHANGELOG.md")} from specified asset`);
(0, console_1.log)(` mdsp download-file --file upload/CHANGELOG.md --assetid 5...f \t\t download file ${color("upload/CHANGELOG.md")} from specified asset`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkParameters(options) {
!options.file &&
(0, command_utils_1.errorLog)("Missing file name for download-file command. Run mdsp df --help for full syntax and examples.", true);
!options.assetid &&
(0, command_utils_1.errorLog)(" You have to specify assetid. Run mdsp df --help for full syntax and examples.", true);
}
//# sourceMappingURL=mc-download-file.js.map