@gmb/bitmark-cli
Version:
Bitmark command line interface
178 lines • 7.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const bitmark_grammar_1 = require("bitmark-grammar");
const fs = tslib_1.__importStar(require("fs-extra"));
const path = tslib_1.__importStar(require("path"));
const StringUtils_1 = require("../utils/StringUtils");
const bitmark_parser_generator_1 = require("@gmb/bitmark-parser-generator");
const bitmarkTool = new bitmark_parser_generator_1.BitmarkParserGenerator();
/**
* Convert command
*/
// eslint-disable-next-line arca/no-default-export
class Convert extends core_1.Command {
async run() {
const { args, flags } = await this.parse(Convert);
const { input } = args;
const { version, output, format, append, warnings, pretty, indent, plainText, excludeUnknownProperties, explicitTextFormat, spacesAroundValues: spacesAroundValuesIn, cardSetVersion, parser, } = flags;
const prettyIndent = pretty ? Math.max(0, indent !== null && indent !== void 0 ? indent : 2) : undefined;
const spacesAroundValues = spacesAroundValuesIn != null ? Math.max(0, spacesAroundValuesIn !== null && spacesAroundValuesIn !== void 0 ? spacesAroundValuesIn : 1) : undefined;
const outputFormat = bitmark_parser_generator_1.Output.fromValue(format);
const bitmarkParserType = parser === 'antlr' ? 'antlr' : bitmark_parser_generator_1.BitmarkParserType.fromValue(parser);
let dataIn;
if (input != undefined) {
dataIn = input;
}
else {
// Read from stdin
dataIn = await this.readStream(process.stdin);
}
let res;
if (bitmarkParserType === 'antlr') {
// Antlr parser
const jsonStr = (0, bitmark_grammar_1.parse)(dataIn);
res = JSON.parse(jsonStr);
if (output) {
const jsonPrettyStr = JSON.stringify(res, null, prettyIndent);
// Write JSON to file
const flag = append ? 'a' : 'w';
fs.ensureDirSync(path.dirname(output));
fs.writeFileSync(output, jsonPrettyStr, {
flag,
});
}
}
else {
// Bitmark tool conversion (Peggy parser)
res = bitmarkTool.convert(dataIn, {
bitmarkVersion: bitmark_parser_generator_1.BitmarkVersion.fromValue(version),
bitmarkParserType,
outputFile: output,
outputFormat,
fileOptions: {
append,
},
jsonOptions: {
enableWarnings: warnings,
prettify: prettyIndent,
textAsPlainText: plainText !== null && plainText !== void 0 ? plainText : undefined, // undefined means use default
excludeUnknownProperties: excludeUnknownProperties,
},
bitmarkOptions: {
explicitTextFormat,
spacesAroundValues,
cardSetVersion: bitmark_parser_generator_1.CardSetVersion.fromValue(cardSetVersion),
},
});
}
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');
}
}
Convert.description = 'Convert between bitmark formats';
Convert.examples = [
"<%= config.bin %> <%= command.id %> '[.article] Hello World'",
'<%= config.bin %> <%= command.id %> \'[{"bitmark": "[.article] Hello World","bit": { "type": "article", "format": "bitmark++", "body": "Hello World" }}]\'',
'<%= config.bin %> <%= command.id %> input.json -o output.bitmark',
'<%= config.bin %> <%= command.id %> input.bitmark -o output.json',
'<%= config.bin %> <%= command.id %> -f ast input.json -o output.ast.json',
];
Convert.flags = {
// General
version: core_1.Flags.integer({
char: 'v',
description: 'version of bitmark to use (default: latest)',
options: [...Object.values(bitmark_parser_generator_1.BitmarkVersion).map((v) => `${v}`)], // Must convert integer to string for options
// default: 1,
}),
format: core_1.Flags.string({
char: 'f',
description: `output format. If not specified, bitmark is converted to JSON, and JSON / AST is converted to bitmark`,
// helpValue: 'FORMAT',
options: [...Object.values(bitmark_parser_generator_1.Output)],
}),
warnings: core_1.Flags.boolean({
char: 'w',
description: 'enable warnings in the output',
}),
// 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'],
}),
plainText: core_1.Flags.boolean({
description: 'output text as plain text rather than JSON (default: set by bitmark version)',
helpGroup: 'JSON Formatting',
}),
excludeUnknownProperties: core_1.Flags.boolean({
description: 'exclude unknown properties in the JSON output',
helpGroup: 'JSON Formatting',
}),
// Bitmark formatting
explicitTextFormat: core_1.Flags.boolean({
description: 'include bitmark text format in bitmark even if it is the default (bitmark++)',
helpGroup: 'Bitmark Formatting',
}),
spacesAroundValues: core_1.Flags.integer({
description: 'number of spaces around values in bitmark (default: 1)',
helpGroup: 'Bitmark Formatting',
}),
cardSetVersion: core_1.Flags.integer({
description: 'version of card set to use in bitmark (default: set by bitmark version)',
helpGroup: 'Bitmark Formatting',
options: [...Object.values(bitmark_parser_generator_1.CardSetVersion).map((v) => `${v}`)], // Must convert integer to string for options
// default: 1,
}),
// 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'],
}),
// Parser
parser: core_1.Flags.string({
description: `parser to use`,
helpGroup: 'Parser Options',
options: [...Object.values(bitmark_parser_generator_1.BitmarkParserType), 'antlr'],
default: bitmark_parser_generator_1.BitmarkParserType.peggy,
}),
};
Convert.args = {
input: core_1.Args.string({
description: 'file to read, or bitmark or json string. If not specified, input will be from <stdin>',
required: false,
}),
};
exports.default = Convert;
//# sourceMappingURL=convert.js.map