1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.emitTree = exports.emitList = exports.treeNodeToJson = exports.treeNodeToTreeify = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const treeify_1 = require("treeify");
|
6 | const formatUtils = tslib_1.__importStar(require("./formatUtils"));
|
7 | function treeNodeToTreeify(printTree, { configuration }) {
|
8 | const target = {};
|
9 | const copyTree = (printNode, targetNode) => {
|
10 | const iterator = Array.isArray(printNode)
|
11 | ? printNode.entries()
|
12 | : Object.entries(printNode);
|
13 | for (const [key, { label, value, children }] of iterator) {
|
14 | const finalParts = [];
|
15 | if (typeof label !== `undefined`)
|
16 | finalParts.push(formatUtils.applyStyle(configuration, label, formatUtils.Style.BOLD));
|
17 | if (typeof value !== `undefined`)
|
18 | finalParts.push(formatUtils.pretty(configuration, value[0], value[1]));
|
19 | if (finalParts.length === 0)
|
20 | finalParts.push(formatUtils.applyStyle(configuration, `${key}`, formatUtils.Style.BOLD));
|
21 | const finalLabel = finalParts.join(`: `);
|
22 | const createdNode = targetNode[finalLabel] = {};
|
23 | if (typeof children !== `undefined`) {
|
24 | copyTree(children, createdNode);
|
25 | }
|
26 | }
|
27 | };
|
28 | if (typeof printTree.children === `undefined`)
|
29 | throw new Error(`The root node must only contain children`);
|
30 | copyTree(printTree.children, target);
|
31 | return target;
|
32 | }
|
33 | exports.treeNodeToTreeify = treeNodeToTreeify;
|
34 | function treeNodeToJson(printTree) {
|
35 | const copyTree = (printNode) => {
|
36 | var _a;
|
37 | if (typeof printNode.children === `undefined`) {
|
38 | if (typeof printNode.value === `undefined`)
|
39 | throw new Error(`Assertion failed: Expected a value to be set if the children are missing`);
|
40 | return formatUtils.json(printNode.value[0], printNode.value[1]);
|
41 | }
|
42 | const iterator = Array.isArray(printNode.children)
|
43 | ? printNode.children.entries()
|
44 | : Object.entries((_a = printNode.children) !== null && _a !== void 0 ? _a : {});
|
45 | const targetChildren = Array.isArray(printNode.children)
|
46 | ? []
|
47 | : {};
|
48 | for (const [key, child] of iterator)
|
49 | targetChildren[key] = copyTree(child);
|
50 | if (typeof printNode.value === `undefined`)
|
51 | return targetChildren;
|
52 | return {
|
53 | value: formatUtils.json(printNode.value[0], printNode.value[1]),
|
54 | children: targetChildren,
|
55 | };
|
56 | };
|
57 | return copyTree(printTree);
|
58 | }
|
59 | exports.treeNodeToJson = treeNodeToJson;
|
60 | function emitList(values, { configuration, stdout, json }) {
|
61 | const children = values.map(value => ({ value }));
|
62 | emitTree({ children }, { configuration, stdout, json });
|
63 | }
|
64 | exports.emitList = emitList;
|
65 | function emitTree(tree, { configuration, stdout, json, separators = 0 }) {
|
66 | var _a;
|
67 | if (json) {
|
68 | const iterator = Array.isArray(tree.children)
|
69 | ? tree.children.values()
|
70 | : Object.values((_a = tree.children) !== null && _a !== void 0 ? _a : {});
|
71 | for (const child of iterator)
|
72 | stdout.write(`${JSON.stringify(treeNodeToJson(child))}\n`);
|
73 | return;
|
74 | }
|
75 | let treeOutput = treeify_1.asTree(treeNodeToTreeify(tree, { configuration }), false, false);
|
76 |
|
77 | if (separators >= 1)
|
78 | treeOutput = treeOutput.replace(/^([├└]─)/gm, `│\n$1`).replace(/^│\n/, ``);
|
79 |
|
80 | if (separators >= 2)
|
81 | for (let t = 0; t < 2; ++t)
|
82 | treeOutput = treeOutput.replace(/^([│ ].{2}[├│ ].{2}[^\n]+\n)(([│ ]).{2}[├└].{2}[^\n]*\n[│ ].{2}[│ ].{2}[├└]─)/gm, `$1$3 │\n$2`).replace(/^│\n/, ``);
|
83 | if (separators >= 3)
|
84 | throw new Error(`Only the first two levels are accepted by treeUtils.emitTree`);
|
85 | stdout.write(treeOutput);
|
86 | }
|
87 | exports.emitTree = emitTree;
|