@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)
142 lines (123 loc) • 4.58 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 utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
let color = (0, command_utils_1.getColor)("green");
exports.default = (program) => {
program
.command("markdown-help")
.alias("mdhelp")
.description(color(`generates folder with markdown help commands for the CLI*`))
.option("-d, --dir <dir>", "folder folder", "markdown-help")
.option("-o, --overwrite", "overwrite files if they already exist")
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
let index = ``;
program.commands.forEach((cmd) => {
const name = cmd.name();
const description = cmd.description();
const help = cmd.helpInformation();
const examples = extractExamples(cmd);
index += `| [\`mdsp ${name}\`](${name}.md) | ${stripAnsi(description)} |\n`;
writeCommandText(options, name, commandTemplate(name, description, help, examples).trim());
});
writeCommandText(options, "index", indexTemplate(index).trim());
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp mdhelp`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function writeCommandText(options, commandName, text) {
const fileName = `${options.dir}/${commandName}.md`;
const filePath = path.resolve(fileName);
!fs.existsSync(path.resolve(options.dir)) && fs.mkdirSync(path.resolve(options.dir));
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(filePath, text);
console.log(`The data was written into ${color(filePath)}`);
}
function extractExamples(cmd) {
const preservedConsoleLog = console.log;
cmd.configureOutput({ writeOut: (str) => 0 });
let text = "";
console.log = (...args) => {
text += args.join("") + "\n";
};
cmd.outputHelp();
console.log = preservedConsoleLog;
return text;
}
const commandTemplate = (name, description, help, examples) => {
return `
---
title: MindSphere CLI - mdsp ${stripAnsi(name)} Command
next:
- title: Overview
link: ../cli/index
- title: Setting up the CLI
link: ../cli/setting-up-the-cli
---
# MindSphere CLI: mdsp ${stripAnsi(name)} Command
Syntax:
\`\`\`bash
mdsp ${stripAnsi(name)}
\`\`\`
Help:
\`\`\`bash
mdsp ${stripAnsi(name)} --help
\`\`\`
Alternative form:
\`\`\`bash
mc ${stripAnsi(name)}
\`\`\`
(The CLI was using \`mc\` as default command name in older versions)
## Description
${stripAnsi(description)}
## Usage
Parameter list:
\`\`\`text
${stripAnsi(help)}
\`\`\`
## Examples
Here are some examples of how to use the \`mdsp ${stripAnsi(name)}\` command:
\`\`\`text
${stripAnsi(examples)}
\`\`\`
See [MindSphere API documentation](https://developer.siemens.com/insights-hub/overview.html) for more information about MindSphere APIs.
`;
};
const indexTemplate = (index) => `
---
title: MindSphere CLI: List of all commands
---
# MindSphere CLI: List of all commands
| Command | Description |
| ----------- | ----------- |
${index}\n`;
const stripAnsi = (x) => {
return x
.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "")
.replace(/(<([^>]+)>)/gi, "");
};
//# sourceMappingURL=markdown-help.js.map