Files
enm-cli/util/logAttributes.js
Vyacheslav.Sviridov bcc0a42f73 bulk import creation
2022-06-26 21:09:48 +06:00

34 lines
1.0 KiB
JavaScript
Executable File

const chalk = require('chalk')
function transformAttributes(element) {
if (Array.isArray(element)) {
return element.map(item => transformAttributes(item))
}
if (Array.isArray(element.value)) {
return { [element.key]: transformAttributes(element.value) }
}
return element.key ? { [element.key]: element.value } : element
}
function colorize(attributes) {
const sorted = attributes.sort ? attributes.sort((a, b) => a.key < b.key ? -1 : 1) : attributes
return JSON.stringify(transformAttributes(sorted), null, 1)
.replace(/["(){}\[\]]/mg, '')
.replace(/^\s*,*\n/mg, '')
.replace(/,$/mg, '')
.replace(/^(\s{2}\w+):/mg, chalk.green('$1:'))
.replace(/^(\s{4}\w+):/mg, chalk.yellow('$1:'))
.replace(/^(\s{5}\w+):/mg, chalk.cyan('$1:'))
}
function logAttributes(fdn, attributes) {
const output = `
${chalk.yellow.bold('FDN')}: ${chalk.bold(fdn.replace(/\((\w+)\)/g, ''))}
${colorize(attributes)}`
console.log(output)
}
module.exports = logAttributes