Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artifacts: Get rid of the markdown parser. #870

Merged
merged 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ce/ce/cli/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { getArtifact, RegistryDisplayContext, RegistryResolver } from '../regist
import { Session } from '../session';
import { Channels } from '../util/channels';
import { Uri } from '../util/uri';
import { Table } from './console-table';
import { addVersionToArtifactIdentity, artifactIdentity } from './format';
import { Table } from './markdown-table';
import { debug, error, log } from './styling';

export async function showArtifacts(artifacts: Iterable<ResolvedArtifact>, registries: RegistryDisplayContext, options?: { force?: boolean }) {
Expand Down
2 changes: 1 addition & 1 deletion ce/ce/cli/commands/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { i } from '../../i18n';
import { session } from '../../main';
import { Uri } from '../../util/uri';
import { Command } from '../command';
import { Table } from '../markdown-table';
import { Table } from '../console-table';
import { log } from '../styling';
import { Clear } from '../switches/clear';

Expand Down
6 changes: 4 additions & 2 deletions ce/ce/cli/commands/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { buildRegistryResolver } from '../../artifacts/artifact';
import { i } from '../../i18n';
import { session } from '../../main';
import { Command } from '../command';
import { Table } from '../markdown-table';
import { Table } from '../console-table';
import { error, log } from '../styling';
import { Project } from '../switches/project';
import { Version } from '../switches/version';
Expand Down Expand Up @@ -36,6 +36,7 @@ export class FindCommand extends Command {
await buildRegistryResolver(session, (await this.project.manifest)?.metadata.registries));
const table = new Table(i`Artifact`, i`Version`, i`Summary`);

let anyEntries = false;
for (const each of this.inputs) {
const hasColon = each.indexOf(':') > -1;
// eslint-disable-next-line prefer-const
Expand All @@ -52,13 +53,14 @@ export class FindCommand extends Command {
}
for (const result of artifactVersions) {
if (!result.metadata.dependencyOnly) {
anyEntries = true;
table.push(display, result.metadata.version, result.metadata.summary || '');
}
}
}
}

if (!table.anyRows) {
if (!anyEntries) {
error(i`No artifacts found matching criteria: ${cyan.bold(this.inputs.join(', '))}`);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion ce/ce/cli/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import { i } from '../../i18n';
import { session } from '../../main';
import { Command } from '../command';
import { Table } from '../console-table';
import { artifactIdentity } from '../format';
import { Table } from '../markdown-table';
import { log } from '../styling';
import { Installed } from '../switches/installed';

Expand Down
57 changes: 57 additions & 0 deletions ce/ce/cli/console-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { strict } from 'assert';
import { red } from 'chalk';
import stripAnsi from 'strip-ansi';

function leftPad(text: string, length: number) {
const remain = length - text.length;
if (remain <= 0) { return text; }
return text + ' '.repeat(remain);
}

export class Table {
private readonly columnNames: Array<string>;
private readonly rows = new Array<Array<string>>();
constructor(...columnNames: Array<string>) {
this.columnNames = columnNames;
}
push(...values: Array<string>) {
strict.equal(values.length, this.columnNames.length, 'unexpected number of arguments in table row');
this.rows.push(Array.from(values));
}
toString() {
const lengths = new Array<number>(this.columnNames.length);
for (let colNum = 0; colNum < this.columnNames.length; ++colNum) {
lengths[colNum] = this.columnNames[colNum].length;
}

for (const row of this.rows) {
for (let colNum = 0; colNum < this.columnNames.length; ++colNum) {
const colLen = stripAnsi(row[colNum]).length;
if (colLen > lengths[colNum]) {
lengths[colNum] = colLen;
}
}
}

const formattedRows = new Array<string>();
const thisFormattedRow = new Array<string>(this.columnNames.length);
for (let colNum = 0; colNum < this.columnNames.length; ++colNum) {
thisFormattedRow[colNum] = red(leftPad(this.columnNames[colNum], lengths[colNum]));
}

formattedRows.push(thisFormattedRow.join(' '));

for (const row of this.rows) {
for (let colNum = 0; colNum < this.columnNames.length; ++colNum) {
thisFormattedRow[colNum] = leftPad(row[colNum], lengths[colNum]);
}

formattedRows.push(thisFormattedRow.join(' '));
}

return formattedRows.join('\n');
}
}
57 changes: 0 additions & 57 deletions ce/ce/cli/markdown-table.ts

This file was deleted.

10 changes: 2 additions & 8 deletions ce/ce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"scripts": {
"eslint-fix": "eslint . --fix --ext .ts",
"eslint": "eslint . --ext .ts",
"clean": "shx rm -rf dist .rush *.log && shx echo Done",
"build": "tsc -p .",
"watch": "tsc -p . --watch",
"prepare": "npm run build",
Expand Down Expand Up @@ -43,13 +42,9 @@
"@types/semver": "7.3.9",
"@types/tar-stream": "~2.2.0",
"typescript": "4.5.5",
"shx": "0.3.4",
"chalk": "4.1.2",
"translate-strings": "1.1.15",
"@types/marked-terminal": "3.1.3",
"@types/marked": "4.0.2",
"@types/cli-progress": "3.11.0",
"@types/mocha": "9.1.0",
"source-map-support": "0.5.21"
},
"dependencies": {
Expand All @@ -62,9 +57,8 @@
"sed-lite": "0.8.4",
"unbzip2-stream": "1.4.3",
"chalk": "4.1.2",
"marked-terminal": "5.1.1",
"marked": "4.0.12",
"cli-progress": "3.11.1",
"xml-writer": "1.7.0"
"xml-writer": "1.7.0",
"strip-ansi": "5.2"
}
}
Loading