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)

276 lines 15.1 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 fs = require("fs"); const path = require("path"); const util = require("util"); const __1 = require("../.."); const sdk_1 = require("../../api/sdk"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const mime = require("mime-types"); const streamPipeline = util.promisify(require("stream").pipeline); let color = (0, command_utils_1.getColor)("blue"); exports.default = (program) => { program .command("data-exchange") .alias("dx") .option("-m, --mode [list|info|download|upload|rename|delete|renamedir|mkdir|rmdir]", "mode [list | info | download | upload | rename | renamedir | delete | mkdir | rmdir ]", "list") .option("-f, --file <file>", "file path ") .option("-n, --dirname <dirname>", "directory name for --mode mkdir command") .option("-w, --newname <newname>", "new file or directory name for --mode rename or renamedir command") .option("-d, --dirid <dirid>", `directory id [private | public | <id>]`, "public") .option("-i, --fileid <fileid>", `fileid`) .option("-r, --recursive", `used for --mode rmdir command, deletes all content in directory`) .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(color("list, upload, download and manage data exchange files and directories *")) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkRequiredParamaters(options); const sdk = (0, command_utils_1.getSdk)(options); color = (0, command_utils_1.adjustColor)(color, options, true); (0, command_utils_1.homeDirLog)(options.verbose, color); (0, command_utils_1.proxyLog)(options.verbose, color); options.dirid === "public" && (options.dirid = sdk_1.DataExchangeModels.Root.Public); options.dirid === "private" && (options.dirid = sdk_1.DataExchangeModels.Root.Private); switch (options.mode) { case "list": yield listFiles(sdk, options); break; case "download": yield downloadFile(options, sdk); break; case "upload": yield uploadFile(options, sdk); break; case "delete": yield deleteFile(options, sdk); break; case "rename": yield renameFile(options, sdk); break; case "renamedir": yield renameDir(options, sdk); break; case "mkdir": yield makeDirectory(options, sdk); break; case "rmdir": yield removeDirectory(options, sdk); break; case "info": yield entryInfo(options, sdk); break; default: throw Error(`no such option: ${options.mode}`); } } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp data-exchange --mode list \t\t\t\t\t list all entries in public data exchange root`); (0, console_1.log)(` mdsp data-exchange --mode list --dirid private \t\t\t list all entries in private data exchange root`); (0, console_1.log)(` mdsp data-exchange --mode info --dirid <dirid> \t\t\t get full info about the specified directory`); (0, console_1.log)(` mdsp data-exchange --mode info --fileid <fileid> \t\t\t get full info about the specified`); (0, console_1.log)(` mdsp data-exchange --mode download --fileid <fileid> \t\t\t download file with specified id`); (0, console_1.log)(` mdsp data-exchange --mode upload --file <file> --dirid <dirid> \t upload file to specified directory`); (0, console_1.log)(` mdsp data-exchange --mode rename --fileid <fileid> --newname <newname> rename the specified file`); (0, console_1.log)(` mdsp data-exchange --mode renamedir --dirid <dirid> --newname <newname> rename the specified directory`); (0, console_1.log)(` mdsp data-exchange --mode delete --fileid <fileid> \t\t\t delete file with specified id`); (0, console_1.log)(` mdsp data-exchange --mode rmdir --dirid <dirid> \t\t\t delete directory with specified id`); (0, console_1.log)(` mdsp data-exchange --mode rmdir --dirid <dirid> --recursive \t\t delete directory with specified id recrusively`); (0, command_utils_1.serviceCredentialLog)(); }); }; function uploadFile(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const result = yield sdk .GetDataExchangeClient() .PostFile({ name: path.basename(options.file), parentId: options.dirid, type: mime.lookup(options.file) }, file); (0, command_utils_1.verboseLog)(JSON.stringify(result, null, 2), options.verbose); console.log(`the file ${color(result.name)} with size of ${result.sizeInBytes} bytes and type ${result.type} was uploaded.`); }); } function downloadFile(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const fileInfo = yield sdk.GetDataExchangeClient().GetFileProperties(options.fileid); const download = (yield (0, __1.retry)(options.retry, () => sdk.GetDataExchangeClient().GetFile(options.fileid))); !download.ok && (0, command_utils_1.errorLog)(`Unexpected response ${download.statusText}`, true); const downloadPath = path.resolve(options.file || fileInfo.name); const file = fs.createWriteStream(downloadPath); yield streamPipeline(download.body, file); console.log(`${color(downloadPath)} was written successfully.`); }); } function deleteFile(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const fileInfo = yield sdk.GetDataExchangeClient().GetFileProperties(options.fileid); yield sdk.GetDataExchangeClient().DeleteFile(options.fileid); console.log(`File ${color(fileInfo.name)} with id ${options.fileid} was deleted.`); }); } function renameFile(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const dxClient = sdk.GetDataExchangeClient(); const result = yield dxClient.PatchFileProperties(options.fileid, { name: options.newname }); console.log(`The file was renamed`); printInfos(result, "File"); (0, command_utils_1.verboseLog)(JSON.stringify(result, null, 2), options.verbose); }); } function renameDir(options, sdk) { return __awaiter(this, void 0, void 0, function* () { if (options.dirid === sdk_1.DataExchangeModels.Root.Private || options.dirid === sdk_1.DataExchangeModels.Root.Public) { (0, utils_1.throwError)(`You can't rename the root directory ${options.dirid}!`); } const dxClient = sdk.GetDataExchangeClient(); const result = yield dxClient.PatchDirectoryProperties(options.dirid, { name: options.newname }); console.log(`The directory was renamed`); printInfos(result, "Dir"); (0, command_utils_1.verboseLog)(JSON.stringify(result, null, 2), options.verbose); }); } function listFiles(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const dxClient = sdk.GetDataExchangeClient(); let page = 0; let filesOrDirs; const filter = buildFilter(options); (0, command_utils_1.verboseLog)(JSON.stringify(filter, null, 2), options.verbose); console.log(`${color("type")} id ${color("name")} (modifiedBy) modifiedDate [size] [type]`); let entryCount = 0; do { filesOrDirs = (yield (0, __1.retry)(options.retry, () => dxClient.GetDirectory(options.dirid, { pageNumber: page, pageSize: 100, filter: Object.keys(filter).length === 0 ? undefined : JSON.stringify(filter), }))); filesOrDirs.page = filesOrDirs.page || { totalPages: 0 }; for (const dir of filesOrDirs.directories || []) { entryCount++; console.log(`${color("D:")} ${dir.id} ${color(dir.name)} (${dir.modifiedBy}) ${dir.modifiedDate} [-] [${color("DIR")}]`); (0, command_utils_1.verboseLog)(JSON.stringify(dir, null, 2), options.verbose); } for (const file of filesOrDirs.files || []) { entryCount++; console.log(`F: ${file.id} ${color(file.name)} (${file.modifiedBy}) ${file.modifiedDate} [${file.sizeInBytes}] [${file.type}]`); (0, command_utils_1.verboseLog)(JSON.stringify(file, null, 2), options.verbose); } } while (page++ < (filesOrDirs.page.totalPages || 0)); console.log(`${color(entryCount)} entries listed.\n`); }); } function buildFilter(options) { const filter = (options.filter && JSON.parse(options.filter)) || {}; let pointer = filter; if (options.jobid !== undefined) { filter.and = {}; pointer = filter.and; } if (options.jobid) { pointer.id = { contains: `${options.jobid}` }; } if (options.typeid) { pointer.id = { contains: `${options.typeid}` }; } return filter; } function entryInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { if (options.fileid) { const fileInfo = yield sdk.GetDataExchangeClient().GetFileProperties(options.fileid); printInfos(fileInfo, "File"); (0, command_utils_1.verboseLog)(JSON.stringify(fileInfo, null, 2), options.verbose); } else if (options.dirid) { const dirInfo = yield sdk.GetDataExchangeClient().GetDirectoryProperties(options.dirid); printInfos(dirInfo, "Dir"); (0, command_utils_1.verboseLog)(JSON.stringify(dirInfo, null, 2), options.verbose); } else { console.log("Couldn't find the file or dir."); } }); } function printInfos(fileInfo, type) { console.log(`${color(type)} Information:`); console.log(`\tid: ${color(fileInfo.id)}`); console.log(`\tname: ${color(fileInfo.name)}`); console.log(`\ttype: ${fileInfo.type}`); console.log(`\tparentId: ${fileInfo.parentId}`); console.log(`\tsize: ${(0, command_utils_1.humanFileSize)(fileInfo.sizeInBytes || 0)}`); console.log(`\tmodified date: ${fileInfo.modifiedDate}`); console.log(`\tmodified by: ${color(fileInfo.modifiedBy)}`); } function makeDirectory(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const dxClient = sdk.GetDataExchangeClient(); const result = yield dxClient.PostDirectory({ name: options.dirname, parentId: options.dirid }); console.log(`the directory ${color(result.name)} with dirid ${color(result.id)} was created in ${result.parentId} directory.`); }); } function removeDirectory(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const dxClient = sdk.GetDataExchangeClient(); yield dxClient.DeleteDirectory(options.dirid, { recursive: options.recursive || false }); console.log(`the directory with id ${color(options.dirid)} ${options.recursive ? "and all its content" : ""} was deleted.`); }); } function checkRequiredParamaters(options) { //[list|info|download|upload|rename|delete|renamedir|mkdir|rmdir] options.mode === "list" && !options.dirid && (0, command_utils_1.errorLog)("you have to provide the --dirid for --mode list command.", true); options.mode === "info" && !(options.dirid || options.fileid) && (0, command_utils_1.errorLog)("you have to provide the --dirid or --fileid for --mode info command.", true); options.mode === "download" && !options.fileid && (0, command_utils_1.errorLog)("you have to provide the --fileid for --mode download command.", true); options.mode === "upload" && !options.file && (0, command_utils_1.errorLog)("you have to provide the --file for --mode upload command.", true); options.mode === "upload" && !options.dirid && (0, command_utils_1.errorLog)("you have to provide the --dirid for --mode upload command.", true); options.mode === "rename" && !options.fileid && (0, command_utils_1.errorLog)("you have to provide the --fileid for --mode rename command.", true); options.mode === "rename" && !options.newname && (0, command_utils_1.errorLog)("you have to provide the --newname for --mode rename command.", true); options.mode === "renamedir" && !options.dirid && (0, command_utils_1.errorLog)("you have to provide the --dirid for --mode renamedir command.", true); options.mode === "renamedir" && !options.newname && (0, command_utils_1.errorLog)("you have to provide the --newname for --mode renamedir command.", true); options.mode === "mkdir" && !options.dirname && (0, command_utils_1.errorLog)("you have to provide the --dirname for --mode mkdir command.", true); options.mode === "mkdir" && !options.dirid && (0, command_utils_1.errorLog)("you have to provide the --dirid with parent id for --mode mkdir command.", true); options.mode === "rmdir" && !options.dirid && (0, command_utils_1.errorLog)("you have to provide the --dirid with --mode rmdir command.", true); } //# sourceMappingURL=data-exchange.js.map