UNPKG

@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)

75 lines 4.66 kB
"use strict"; 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 path = require("path"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const ora = require("ora-classic"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("delete-file") .alias("de") .option("-f, --file <fileToDelete>", "file to delete 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("delete the file from mindsphere file service *")}`) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkRequiredParameters(options); const sdk = (0, command_utils_1.getSdk)(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 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("deleting file"); !options.verbose && spinner.start(); (0, command_utils_1.verboseLog)(JSON.stringify(fileInfo, null, 2), options.verbose); (0, command_utils_1.verboseLog)(`Deleting file ${color(fileInfo[0].name)} with size of ${color((0, command_utils_1.humanFileSize)(fileInfo[0].size || 0))} from MindSphere.`, options.verbose, spinner); yield (0, utils_1.retry)(options.retry, () => iotFileClient.DeleteFile(options.assetid, fullpath)); !options.verbose && spinner.succeed("Done"); (0, console_1.log)(`\nYour file ${color(fullpath)} was sucessfully deleted`); } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp delete-file -f CHANGELOG.md --assetid 5..f \t\t\t\t delete file ${color("CHANGELOG.md")} from specified asset`); (0, console_1.log)(` mdsp delete-file --file CHANGELOG.md --assetid 5...f --filepath upload \t delete file ${color("upload/CHANGELOG.md")} from specified asset`); (0, console_1.log)(` mdsp delete-file --file upload/CHANGELOG.md --assetid 5...f \t\t delete file ${color("upload/CHANGELOG.md")} from specified asset`); (0, command_utils_1.serviceCredentialLog)(); }); }; function checkRequiredParameters(options) { !options.file && (0, command_utils_1.errorLog)("Missing file name for delete-file command. Run mdsp de --help for full syntax and examples.", true); !options.assetid && (0, command_utils_1.errorLog)(" You have to specify assetid. Run mdsp de --help for full syntax and examples.", true); } //# sourceMappingURL=delete-file.js.map