@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)
113 lines • 5.56 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 utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const fs = require("fs");
const path = require("path");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("sdi-search-schemas")
.alias("sds")
.option("-m, --mode [template|search]", "search | template", "search")
.option("-s, --searchrequest <searchrequest>", "search request for --search command", "sdi.searchrequest.mdsp.json")
.option("-o, --overwrite", "overwrite template file if it already exists")
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color("search SDI schemas *"))
.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);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
switch (options.mode) {
case "search":
yield searchSchemas(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
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 sdi-search-schemas --mode template \t create template file`);
(0, console_1.log)(` mdsp sdi-search-schemas --mode search --searchrequest <searchrequestfile> \t search for sdi schemas`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
options.mode === "search" &&
!options.searchrequest &&
(0, command_utils_1.errorLog)("you have to provide a searchrequest file to search the SDI schemas (see mdsp sdi-search-schemas --help for more details)", true);
}
function searchSchemas(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const searchFilePath = path.resolve(options.searchrequest);
const searchData = fs.readFileSync(searchFilePath);
const searchRequest = JSON.parse(searchData.toString());
let pageToken = undefined;
let count = 0;
do {
const schemas = yield sdiClient.SearchSchemas(searchRequest);
(_a = schemas.schemas) === null || _a === void 0 ? void 0 : _a.forEach((schema) => {
(0, command_utils_1.printObjectInfo)(`Schema Properties for ${color(schema.schemaName)}:\n`, schema || {}, options, ["id", "registryId", "category"], color);
console.log("\n");
count++;
});
pageToken = (_b = schemas.page) === null || _b === void 0 ? void 0 : _b.nextToken;
} while (pageToken);
console.log(`${color(count)} sdi schemas listed.`);
});
}
function createTemplate(options) {
const templateType = {
schemas: [
{
dataTag: "string",
schemaName: "string",
category: "ENTERPRISE",
aspectName: "string",
assetId: "string",
sourceName: "string",
metaDataTags: ["teamcenter"],
},
],
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeToFile(options, templateType);
}
function writeToFile(options, schema) {
const fileName = options.file || `sdi.searchrequest.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(filePath, JSON.stringify(schema, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp sdi-search-schemas --mode search --searchrequest ${fileName} \n\nto search the schemas`);
}
//# sourceMappingURL=sdi-search-schemas.js.map