@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)
77 lines • 4.4 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 command_utils_1 = require("./command-utils");
const ora = require("ora-classic");
const mime = require("mime-types");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("sdi-file-upload")
.alias("sdu")
.option("-f, --file <file>", "file to upload to SDI")
.option("-h, --filepath <filepath>", "file path in SDI")
.option("-m, --mime [mime-type]", "mime type of the file (default: automatic recognition)")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-k, --passkey <passkey>", "passkey")
.option("-v, --verbose", "verbose output")
.description(`${color("upload file to SDI *")}`)
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkParameters(options);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
const spinner = ora("uploading file to SDI");
!options.verbose && spinner.start();
const uploadFile = path.resolve(options.file);
(0, command_utils_1.verboseLog)(`File to upload: ${color(uploadFile)}.`, options.verbose, spinner);
if (!fs.existsSync(uploadFile)) {
throw new Error(`Can't find file ${uploadFile}`);
}
const sdk = (0, command_utils_1.getSdk)(options);
const mimeType = options.mime || mime.lookup(uploadFile) || "application/octet-stream";
if (!fs.existsSync(uploadFile)) {
throw new Error(`Can't find file ${uploadFile}`);
}
(0, command_utils_1.verboseLog)(`Uploading the file: ${color(uploadFile)} with mime type ${mimeType}.`, options.verbose, spinner);
const startDate = new Date();
const filePath = options.filepath ? options.filepath : path.basename(uploadFile);
const result = yield sdk
.GetSemanticDataInterConnectClient()
.DataUpload(filePath, fs.readFileSync(uploadFile), mimeType);
const endDate = new Date();
!options.verbose && spinner.succeed("Done");
(0, console_1.log)(`Upload time: ${(endDate.getTime() - startDate.getTime()) / 1000} seconds`);
(0, console_1.log)(`\nYour file ${color(uploadFile)} (SDI Name: ${color(result.filePath)}) was succesfully uploaded.\n`);
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp sdi-file-upload --file <file> \t\t\t upload file to SDI`);
(0, console_1.log)(`\n Restriction:\n`);
(0, console_1.log)(` This works only for SDI only tenants (without Integrated Data Lake)`);
(0, console_1.log)(` If you are MindSphere Integrated Data Lake Customer use mdsp data-lake --mode upload command instead. `);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkParameters(options) {
!options.file &&
(0, command_utils_1.errorLog)("Missing file name for sdi-file-upload command. Run mdsp sdi-file-upload --help for full syntax and examples.", true);
}
//# sourceMappingURL=sdi-file-upload.js.map