Skip to content

Commit

Permalink
fix(cli): always send the reports
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Sep 26, 2024
1 parent 4df780b commit 6672778
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-planets-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": patch
---

Always send the reports
16 changes: 8 additions & 8 deletions packages/cli/src/actions/BackupAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,17 +457,17 @@ export class BackupAction {
}),
);
}),
...createReportListTasks(l, {
hostname: this.config.hostname ?? hostname(),
action: "backup",
reports: this.config.reports || [],
verbose: this.options.verbose,
onMessage: (result, report) =>
this.dataFormat(result).format(report.format ?? "list"),
}),
];
},
}),
...createReportListTasks(l, {
hostname: this.config.hostname ?? hostname(),
action: "backup",
reports: this.config.reports || [],
verbose: this.options.verbose,
onMessage: (result, report) =>
this.dataFormat(result).format(report.format ?? "list"),
}),
])
.execAndParse(this.options.verbose);
}
Expand Down
17 changes: 9 additions & 8 deletions packages/cli/src/actions/CopyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export class CopyAction {
l.$task({
key: "snapshots",
data: { snapshots: [] },
exitOnError: false,
title: {
initial: "Fetch snapshots",
started: "Fetching snapshots",
Expand Down Expand Up @@ -370,17 +371,17 @@ export class CopyAction {
});
}),
),
...createReportListTasks(l, {
hostname: this.config.hostname ?? hostname(),
action: "copy",
reports: this.config.reports || [],
verbose: this.options.verbose,
onMessage: (result, report) =>
this.dataFormat(result).format(report.format ?? "list"),
}),
];
},
}),
...createReportListTasks(l, {
hostname: this.config.hostname ?? hostname(),
action: "copy",
reports: this.config.reports || [],
verbose: this.options.verbose,
onMessage: (result, report) =>
this.dataFormat(result).format(report.format ?? "list"),
}),
])
.execAndParse(this.options.verbose);
}
Expand Down
53 changes: 32 additions & 21 deletions packages/cli/src/utils/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ProcessOutput,
ListrTaskWrapper,
ListrTaskState,
ListrTaskFn,
} from "listr2";

export class List3Logger<
Expand Down Expand Up @@ -90,7 +91,6 @@ export class Listr3<T extends Listr3Context> extends Listr<
"simple"
> {
readonly resultMap: Record<string, Listr3TaskResult<T>> = {};
readonly resultList: Listr3TaskResult<T>[] = [];
readonly logger: List3Logger;
protected execTimer: Timer;
constructor(
Expand Down Expand Up @@ -133,6 +133,16 @@ export class Listr3<T extends Listr3Context> extends Listr<
private createResultIndex(key: keyof T, keyIndex?: KeyIndex): string {
return [key, ...this.serializeKeyIndex(keyIndex)].join(".");
}
get resultList() {
return this.tasks
.flatMap((task) => [task, ...(task.subtasks || [])])
.map((task) => {
const result = (task.task.task as any)["_result"];
if (!result)
throw new Error(`Task result is not defined: ${task.title}`);
return result;
});
}
result(key: keyof T, keyIndex?: KeyIndex): Listr3TaskResult<T> {
const index = this.createResultIndex(key, keyIndex);
const result = this.resultMap[index];
Expand All @@ -152,33 +162,34 @@ export class Listr3<T extends Listr3Context> extends Listr<
error: undefined,
data: item.data,
};
this.resultList.push(this.resultMap[index]);
const title =
typeof item.title === "string" ? { initial: item.title } : item.title;
const task: ListrTaskFn<any, any, any> = async (_, task) => {
const result = this.result(item.key, item.keyIndex);
if (title.started) task.title = title.started;
const timer = createTimer();
if (title)
try {
const runResult = await item.run(task, result.data as any);
if (title.completed) task.title = title.completed;
return Array.isArray(runResult)
? task.newListr(runResult)
: runResult;
} catch (error) {
result.error = error as Error;
if (title.failed) task.title = title.failed;
throw error;
} finally {
result.elapsed = timer.elapsed();
}
};
(task as any)["_result"] = this.resultMap[index];
return {
title: title.initial,
exitOnError: item.exitOnError,
enabled: item.enabled,
skip: item.skip,
task: async (_, task) => {
const result = this.result(item.key, item.keyIndex);
if (title.started) task.title = title.started;
const timer = createTimer();
if (title)
try {
const runResult = await item.run(task, result.data as any);
if (title.completed) task.title = title.completed;
return Array.isArray(runResult)
? task.newListr(runResult)
: runResult;
} catch (error) {
result.error = error as Error;
if (title.failed) task.title = title.failed;
throw error;
} finally {
result.elapsed = timer.elapsed();
}
},
task,
};
}
$tasks<K extends keyof T>(
Expand Down

0 comments on commit 6672778

Please sign in to comment.