Skip to content

Commit

Permalink
listr → listr2 (facebook#44716)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#44716

Move to listr2 which handle non-TTY environment, outputting to CircleCI logs in a useful way.  This gives our CI users more useful debugging information, but limits the output when running locally.

If you want more explicit output locally, do something like:

```
yarn run build | cat
```

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D57915369

fbshipit-source-id: ae9f87b0b9608f16ee035b791c5f7b81544c498c
  • Loading branch information
blakef authored and facebook-github-bot committed May 30, 2024
1 parent c67dfbb commit 32b5c96
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 212 deletions.
61 changes: 61 additions & 0 deletions flow-typed/npm/listr2_v8.x.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
*
* @flow
* @format
*/

declare module 'listr2' {
declare export type TaskResult<
ContextT = {__proto__: null},
ReturnT = mixed,
> =
| ReturnT
| Promise<ReturnT>
| rxjs$Observable<ReturnT>
| stream$Readable
| Listr<ContextT>;

declare type TaskFn<ContextT, ReturnT> = (
ctx: ContextT,
task: TaskInstance,
) => TaskResult<ContextT, ReturnT>;

declare type SkipResultSync = boolean | string;
declare type SkipResult = SkipResultSync | Promise<SkipResultSync>;
declare type SkipFn<ContextT> = (ctx: ContextT) => SkipResult;

declare type CustomRenderer = {...}; // TODO

declare interface TaskInstance {
title: string;
output: string;
skip(reason?: string): void;
}

declare export type TaskSpec<
ContextT = {__proto__: null},
ReturnT = mixed,
> = {
title: string,
task: TaskFn<ContextT, ReturnT>,
skip?: SkipFn<ContextT>,
};

declare export type Options = {
concurrent?: boolean | number,
exitOnError?: boolean,
renderer?: 'default' | 'verbose' | 'silent' | CustomRenderer,
nonTTYRenderer?: 'default' | 'verbose' | 'silent' | CustomRenderer,
};

declare export class Listr<ContextT> {
constructor<ReturnT>(
tasks: Array<TaskSpec<ContextT, ReturnT>>,
options?: Options,
): void;
add<ReturnT>(task: TaskSpec<ContextT, ReturnT>): this;
add<ReturnT>(tasks: $ReadOnlyArray<TaskSpec<ContextT, ReturnT>>): this;
run(ctx?: ContextT): Promise<ContextT>;
}
}
2 changes: 1 addition & 1 deletion packages/helloworld/cli.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {app, apple} from '@react-native/core-cli-utils';
import chalk from 'chalk';
import {program} from 'commander';
import {readFileSync} from 'fs';
import Listr from 'listr';
import {Listr} from 'listr2';
import path from 'path';

program.version(JSON.parse(readFileSync('./package.json', 'utf8')).version);
Expand Down
33 changes: 19 additions & 14 deletions packages/helloworld/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
import type {Task} from '@react-native/core-cli-utils';
import type {Result} from 'execa';
import type {ExecaPromise} from 'execa';
import type {TaskSpec} from 'listr';
import type {TaskResult, TaskSpec} from 'listr2';

import chalk from 'chalk';
import Listr from 'listr';
import {Listr} from 'listr2';
import {Observable} from 'rxjs';

export function trim(
line: string,
// $FlowFixMe[prop-missing]
maxLength: number = Math.min(process.stdout?.columns, 120),
): string {
if (process.stdout.isTTY == null) {
return line;
}
const flattened = line.replaceAll('\n', ' ').trim();
return flattened.length >= maxLength
? flattened.slice(0, maxLength - 3) + '...'
Expand All @@ -31,8 +34,8 @@ export function trim(

type ExecaPromiseMetaized = Promise<Result> & child_process$ChildProcess;

export function observe(result: ExecaPromiseMetaized): Observable<string> {
return new Observable(observer => {
export function observe(result: ExecaPromiseMetaized): TaskResult<string> {
const obs = new Observable<string>(observer => {
result.stderr.on('data', (data: Buffer) =>
data
.toString('utf8')
Expand Down Expand Up @@ -69,6 +72,9 @@ export function observe(result: ExecaPromiseMetaized): Observable<string> {
}
};
});

// $FlowFixMe
return obs;
}

type MixedTasks = Task<ExecaPromise> | Task<void>;
Expand All @@ -89,15 +95,14 @@ export function run(
}
ordered = ordered.sort((a, b) => a.order - b.order);

const spec: TaskSpec<void, Observable<string> | Promise<void> | void>[] =
ordered.map(task => ({
title: task.label,
task: () => {
const action = task.action();
if (action != null) {
return observe(action);
}
},
}));
const spec: TaskSpec<void>[] = ordered.map(task => ({
title: task.label,
task: () => {
const action = task.action();
if (action != null) {
return observe(action);
}
},
}));
return new Listr(spec).run();
}
2 changes: 1 addition & 1 deletion packages/helloworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"commander": "^12.0.0",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"listr": "^0.14.3",
"listr2": "^8.2.1",
"react-test-renderer": "18.3.1",
"rxjs": "^7.8.1"
},
Expand Down
Loading

0 comments on commit 32b5c96

Please sign in to comment.