Skip to content

Commit 0a88331

Browse files
committed
feat(list): improve spacing of list command output (#30)
re #30
1 parent 71321e8 commit 0a88331

File tree

1 file changed

+90
-68
lines changed

1 file changed

+90
-68
lines changed

src/printers/list.ts

+90-68
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
import chalk from 'chalk';
2-
import { stripIndent } from 'common-tags';
3-
import title from 'title';
4-
import logSymbols from 'log-symbols';
5-
import columnify from 'columnify';
6-
import startCase from 'lodash.startcase';
7-
8-
import { shouldPrettyPrint, supportsEmoji } from './utils';
9-
import { ListConfig } from '../commands/list';
101
import {
11-
ListResult,
12-
FunctionVersion,
132
AssetVersion,
14-
VariableResource,
15-
EnvironmentResource,
16-
ServiceResource,
173
BuildResource,
4+
EnvironmentResource,
5+
FunctionVersion,
186
ListOptions,
7+
ListResult,
8+
ServiceResource,
9+
VariableResource,
1910
} from '@twilio-labs/serverless-api';
11+
import chalk from 'chalk';
12+
import columnify from 'columnify';
13+
import { stripIndent } from 'common-tags';
14+
import startCase from 'lodash.startcase';
15+
import logSymbols from 'log-symbols';
16+
import title from 'title';
17+
import size from 'window-size';
18+
import { ListConfig } from '../commands/list';
19+
import { shouldPrettyPrint } from './utils';
2020

2121
type KeyMaps = {
2222
[key in ListOptions]: string[];
2323
};
2424

25+
const LONG_LINE = '─'.repeat(size.width);
26+
27+
function basicIndent(str: string, repeat = 0) {
28+
return str
29+
.split('\n')
30+
.map(s => ' '.repeat(repeat) + s)
31+
.join('\n');
32+
}
33+
2534
const baseKeys: KeyMaps = {
2635
environments: [
2736
'sid',
@@ -103,6 +112,13 @@ function formatDate(dateStr: string) {
103112
return new Date(dateStr).toString();
104113
}
105114

115+
function sortByAccess(resA: CommonType, resB: CommonType) {
116+
if (resA.visibility === resB.visibility) {
117+
resA.path.localeCompare(resB.path);
118+
}
119+
return resA.visibility.localeCompare(resB.visibility);
120+
}
121+
106122
const headingTransform = (name: string) => {
107123
return chalk.cyan.bold(startCase(name).replace(/Sid$/g, 'SID'));
108124
};
@@ -170,26 +186,20 @@ export type FunctionOrAssetContent = {
170186
function prettyPrintFunctionsOrAssets(result: FunctionOrAssetContent) {
171187
const { entries, environmentSid } = result;
172188
const resourceString = entries
189+
.sort(sortByAccess)
173190
.map<string>((entry: CommonType, idx: number): string => {
174191
const symbol = idx + 1 === entries.length ? '└──' : '├──';
175-
let emoji = '';
176-
if (supportsEmoji) {
177-
emoji =
178-
entry.visibility === 'public'
179-
? '🌐'
180-
: entry.visibility === 'protected'
181-
? '🔒'
182-
: '🙈';
183-
}
192+
const suffix =
193+
entry.visibility === 'public'
194+
? ' '
195+
: chalk`{dim [Visibility {reset.bold ${entry.visibility}}]}`;
184196
return stripIndent(chalk`
185-
${symbol} ${emoji} ${entry.path} {dim [Visibility: ${entry.visibility}]}
186-
`);
197+
{dim │} {reset ${entry.path}} ${suffix}
198+
`);
187199
})
188200
.join('\n');
189201

190-
return stripIndent(chalk`
191-
│ {bold For Environment:} ${environmentSid}\n${resourceString}
192-
`);
202+
return resourceString;
193203
}
194204

195205
type VariablesContent = {
@@ -198,82 +208,94 @@ type VariablesContent = {
198208
};
199209

200210
function prettyPrintVariables(variables: VariablesContent) {
201-
const { entries, environmentSid } = variables;
211+
const { entries } = variables;
202212

203-
const variableString = entries
204-
.map((entry, idx) => {
205-
const symbol = idx + 1 === entries.length ? '└──' : '├──';
206-
return stripIndent(chalk`
207-
${symbol} {bold ${entry.key}:} ${entry.value}
208-
`);
209-
})
210-
.join('\n');
213+
const updatedRows = entries.map((entry: VariableResource) => {
214+
return {
215+
...entry,
216+
key: chalk`{dim │} {cyan ${entry.key}}:`,
217+
};
218+
});
211219

212-
return stripIndent(chalk`
213-
│ {bold For Environment:} ${environmentSid}\n${variableString}
214-
`);
220+
const renderedValues = columnify(updatedRows, {
221+
columns: ['key', 'value'],
222+
showHeaders: false,
223+
});
224+
225+
return renderedValues;
215226
}
216227

217228
function prettyPrintEnvironment(environment: EnvironmentResource): string {
218-
return stripIndent(chalk`
219-
${environment.unique_name} (${environment.domain_suffix})
220-
│ ├── {bold SID:} ${environment.sid}
221-
│ ├── {bold URL:} ${environment.domain_name}
222-
│ ├── {bold Active Build:} ${environment.build_sid}
223-
│ └── {bold Last Updated:} ${formatDate(environment.date_updated)}
224-
`);
229+
return basicIndent(
230+
stripIndent(chalk`
231+
{bold ${environment.domain_suffix}} {dim [${environment.sid}]}
232+
{dim │} {cyan URL: } {reset ${environment.domain_name}}
233+
{dim │} {cyan Unique Name: } {reset ${environment.unique_name}}
234+
{dim │} {cyan Active Build:} {reset ${environment.build_sid}}
235+
{dim │} {cyan Last Updated:} {reset ${formatDate(
236+
environment.date_updated
237+
)}}
238+
`)
239+
);
225240
}
226241

227242
function prettyPrintServices(service: ServiceResource): string {
228243
return stripIndent(chalk`
229-
230-
│ {cyan.bold ${service.unique_name}}
231-
│ ├── {bold SID: } ${service.sid}
232-
│ ├── {bold Created: } ${formatDate(service.date_created)}
233-
│ └── {bold Updated: } ${formatDate(service.date_updated)}
244+
{bold ${service.unique_name}}
245+
{dim │} {cyan SID: } ${service.sid}
246+
{dim │} {cyan Created: } {dim ${formatDate(service.date_created)}}
247+
{dim │} {cyan Updated: } {dim ${formatDate(service.date_updated)}}
234248
`);
235249
}
236250

237251
function prettyPrintBuilds(build: BuildResource): string {
238-
let status = chalk.yellow(build.status);
252+
let status = chalk.reset.yellow(build.status);
239253
if (build.status === 'completed') {
240-
status = chalk.green(`${logSymbols.success} ${build.status}`);
254+
status = chalk.reset.green(`${logSymbols.success} ${build.status}`);
241255
} else if (build.status === 'failed') {
242-
status = chalk.red(`${logSymbols.error} ${build.status}`);
256+
status = chalk.reset.red(`${logSymbols.error} ${build.status}`);
243257
}
244-
return stripIndent`
245-
${build.sid} (${status})
246-
│ └── ${chalk`{bold Date:}`} ${formatDate(build.date_updated)}
247-
`;
258+
return basicIndent(
259+
stripIndent(chalk`
260+
{bold ${build.sid}} {dim [${status}]}
261+
{dim │} {cyan Date:} {dim ${formatDate(build.date_updated)}}
262+
`)
263+
);
248264
}
249265

250266
function prettyPrintSection<T extends ListOptions>(
251267
sectionTitle: ListOptions,
252268
sectionContent: ListResult[T]
253269
): string {
254-
const sectionHeader = chalk.cyan.bold(`${title(sectionTitle)}: `);
270+
let sectionHeader = chalk.cyan.bold(`${title(sectionTitle)}:`);
255271
let content = '';
256272
if (sectionTitle === 'builds') {
257273
content = (sectionContent as BuildResource[])
258274
.map(prettyPrintBuilds)
259-
.join('\n');
275+
.join(`\n\n`);
260276
} else if (sectionTitle === 'environments') {
261277
content = (sectionContent as EnvironmentResource[])
262278
.map(prettyPrintEnvironment)
263-
.join('\n');
279+
.join('\n\n');
264280
} else if (sectionTitle === 'services') {
265281
content = (sectionContent as ServiceResource[])
266282
.map(prettyPrintServices)
267-
.join('\n');
283+
.join('\n\n');
268284
} else if (sectionTitle === 'variables') {
269-
content = prettyPrintVariables(sectionContent as VariablesContent);
285+
const data = sectionContent as VariablesContent;
286+
sectionHeader = chalk`{cyan.bold ${title(
287+
sectionTitle
288+
)}} {dim for environment ${data.environmentSid}}`;
289+
content = prettyPrintVariables(data);
270290
} else if (sectionTitle === 'functions' || sectionTitle === 'assets') {
271-
content = prettyPrintFunctionsOrAssets(
272-
(sectionContent as unknown) as FunctionOrAssetContent
273-
);
291+
const data = sectionContent as FunctionOrAssetContent;
292+
sectionHeader = chalk`{cyan.bold ${title(
293+
sectionTitle
294+
)}} {dim for environment ${data.environmentSid}}`;
295+
content = prettyPrintFunctionsOrAssets(data);
274296
}
275297
const output = stripIndent`
276-
${sectionHeader}\n${content}
298+
${sectionHeader}\n\n${content}\n\n${chalk.dim(LONG_LINE)}\n
277299
`;
278300
return output;
279301
}

0 commit comments

Comments
 (0)