@gmb/bitmark-cli
Version:
Bitmark command line interface
105 lines • 3.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bitmark_parser_generator_1 = require("@gmb/bitmark-parser-generator");
const core_1 = require("@oclif/core");
const StringUtils_1 = require("../utils/StringUtils");
const bitmarkTool = new bitmark_parser_generator_1.BitmarkParserGenerator();
/**
* Convert Text command
*/
// eslint-disable-next-line arca/no-default-export
class ConvertText extends core_1.Command {
async run() {
const { args, flags } = await this.parse(ConvertText);
const { input } = args;
const { output, textFormat, append, pretty, indent } = flags;
const prettyIndent = pretty ? Math.max(0, indent !== null && indent !== void 0 ? indent : 2) : undefined;
let dataIn;
if (input != undefined) {
dataIn = input;
}
else {
// Read from stdin
dataIn = await this.readStream(process.stdin);
}
let res;
// Bitmark tool text conversion
res = bitmarkTool.convertText(dataIn, {
textFormat: bitmark_parser_generator_1.BodyTextFormat.fromValue(textFormat),
outputFile: output,
fileOptions: {
append,
},
jsonOptions: {
prettify: prettyIndent,
},
});
if (!output) {
try {
if (!StringUtils_1.StringUtils.isString(res)) {
res = JSON.stringify(res, null, prettyIndent);
}
}
catch (e) {
// Ignore
}
console.log(res);
}
}
async readStream(stream) {
const chunks = [];
for await (const chunk of stream)
chunks.push(chunk);
return Buffer.concat(chunks).toString('utf8');
}
}
ConvertText.description = 'Convert between bitmark text formats';
ConvertText.examples = [
"<%= config.bin %> <%= command.id %> 'Hello World'",
'<%= config.bin %> <%= command.id %> \'[{"type":"paragraph","content":[{"text":"Hello World","type":"text"}],"attrs":{}}]\'',
'<%= config.bin %> <%= command.id %> input.json -o output.txt',
'<%= config.bin %> <%= command.id %> input.txt -o output.json',
];
ConvertText.flags = {
// General
textFormat: core_1.Flags.string({
char: 'f',
description: `conversion format`,
// helpValue: 'FORMAT',
default: bitmark_parser_generator_1.BodyTextFormat.bitmarkPlusPlus,
options: [...bitmark_parser_generator_1.BodyTextFormat.values().filter((v) => v === bitmark_parser_generator_1.BodyTextFormat.bitmarkPlusPlus)],
}),
// JSON formatting
pretty: core_1.Flags.boolean({
char: 'p',
description: 'prettify the JSON output with indent',
helpGroup: 'JSON Formatting',
}),
indent: core_1.Flags.integer({
description: 'prettify indent (default:2)',
helpValue: 'INDENT',
helpGroup: 'JSON Formatting',
dependsOn: ['pretty'],
}),
// File output
output: core_1.Flags.file({
helpGroup: 'File output',
char: 'o',
description: 'output file. If not specified, output will be to <stdout>',
helpValue: 'FILE',
}),
append: core_1.Flags.boolean({
helpGroup: 'File output',
char: 'a',
description: 'append to the output file (default is to overwrite)',
dependsOn: ['output'],
}),
};
ConvertText.args = {
input: core_1.Args.string({
description: 'file to read, or text or json string. If not specified, input will be from <stdin>',
required: false,
}),
};
exports.default = ConvertText;
//# sourceMappingURL=convertText.js.map