UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.emitTree = exports.emitList = exports.treeNodeToJson = exports.treeNodeToTreeify = void 0;
4const tslib_1 = require("tslib");
5const treeify_1 = require("treeify");
6const formatUtils = tslib_1.__importStar(require("./formatUtils"));
7function 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}
33exports.treeNodeToTreeify = treeNodeToTreeify;
34function 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}
59exports.treeNodeToJson = treeNodeToJson;
60function emitList(values, { configuration, stdout, json }) {
61 const children = values.map(value => ({ value }));
62 emitTree({ children }, { configuration, stdout, json });
63}
64exports.emitList = emitList;
65function 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 // A slight hack to add line returns between two top-level entries
77 if (separators >= 1)
78 treeOutput = treeOutput.replace(/^([├└]─)/gm, `│\n$1`).replace(/^│\n/, ``);
79 // Another one for the second level fields. We run it twice because in some pathological cases the regex matches would
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}
87exports.emitTree = emitTree;