Skip to content

Commit

Permalink
Uses a hack rather than change the output
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis committed Jun 15, 2023
1 parent 913848f commit 397c02f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/plugin-npm-cli/sources/npmAuditUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export function getReportTree(result: npmAuditTypes.AuditExtendedResponse) {
for (const [packageName, advisories] of miscUtils.sortMap(Object.entries(result), advisory => advisory[0])) {
for (const advisory of miscUtils.sortMap(advisories, advisory => `${advisory.id}`)) {
auditTreeChildren[`${packageName}/${advisory.id}`] = {
label: `${packageName} - #${advisory.id}`,
value: formatUtils.tuple(formatUtils.Type.IDENT, structUtils.parseIdent(packageName)),
children: {
ID: {
label: `ID`,
value: formatUtils.tuple(formatUtils.Type.NUMBER, advisory.id),
},
Issue: {
label: `Issue`,
value: formatUtils.tuple(formatUtils.Type.NO_HINT, advisory.title),
Expand Down
13 changes: 11 additions & 2 deletions packages/yarnpkg-core/sources/treeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type TreeifyNode = {

export function treeNodeToTreeify(printTree: TreeNode, {configuration}: {configuration: Configuration}) {
const target = {};
let n = 0;

const copyTree = (printNode: Array<TreeNode> | TreeMap, targetNode: TreeifyNode) => {
const iterator = Array.isArray(printNode)
Expand All @@ -40,7 +41,9 @@ export function treeNodeToTreeify(printTree: TreeNode, {configuration}: {configu
finalParts.push(formatUtils.applyStyle(configuration, `${key}`, formatUtils.Style.BOLD));

const finalLabel = finalParts.join(`: `);
const createdNode = targetNode[finalLabel] = {};

const uniquePrefix = `\0${n++}\0`;
const createdNode = targetNode[`${uniquePrefix}${finalLabel}`] = {};

if (typeof children !== `undefined`) {
copyTree(children, createdNode);
Expand Down Expand Up @@ -73,7 +76,7 @@ export function treeNodeToJson(printTree: TreeNode) {
: {};

for (const [key, child] of iterator)
targetChildren[key] = copyTree(child);
targetChildren[cleanKey(key)] = copyTree(child);

if (typeof printNode.value === `undefined`)
return targetChildren;
Expand Down Expand Up @@ -106,6 +109,8 @@ export function emitTree(tree: TreeNode, {configuration, stdout, json, separator

let treeOutput = asTree(treeNodeToTreeify(tree, {configuration}) as any, false, false);

treeOutput = treeOutput.replace(/\0[0-9]+\0/g, ``);

// A slight hack to add line returns between two top-level entries
if (separators >= 1)
treeOutput = treeOutput.replace(/^([β”œβ””]─)/gm, `β”‚\n$1`).replace(/^β”‚\n/, ``);
Expand All @@ -120,3 +125,7 @@ export function emitTree(tree: TreeNode, {configuration, stdout, json, separator

stdout.write(treeOutput);
}

function cleanKey(key: string | number) {
return typeof key === `string` ? key.replace(/^\0[0-9]+\0/, ``) : key;
}

0 comments on commit 397c02f

Please sign in to comment.