|
| 1 | +import { onLogAction } from "../../redux" |
| 2 | +import { |
| 3 | + Actions, |
| 4 | + LogLevels, |
| 5 | + ActivityLogLevels, |
| 6 | + ActivityTypes, |
| 7 | +} from "../../constants" |
| 8 | + |
| 9 | +import { createReporter } from "yurnalist" |
| 10 | +import ProgressBar from "progress" |
| 11 | +import chalk from "chalk" |
| 12 | +import { IUpdateActivity } from "../../redux/types" |
| 13 | + |
| 14 | +interface IYurnalistActivities { |
| 15 | + [activityId: string]: { |
| 16 | + text: string | undefined |
| 17 | + statusText: string | undefined |
| 18 | + update(payload: IUpdateActivity["payload"]): void |
| 19 | + end(): void |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export function initializeYurnalistLogger(): void { |
| 24 | + const activities: IYurnalistActivities = {} |
| 25 | + const yurnalist = createReporter({ emoji: true, verbose: true }) |
| 26 | + |
| 27 | + const levelToYurnalist = { |
| 28 | + [LogLevels.Log]: yurnalist.log.bind(yurnalist), |
| 29 | + [LogLevels.Warning]: yurnalist.warn.bind(yurnalist), |
| 30 | + [LogLevels.Error]: yurnalist.error.bind(yurnalist), |
| 31 | + [LogLevels.Info]: yurnalist.info.bind(yurnalist), |
| 32 | + [LogLevels.Success]: yurnalist.success.bind(yurnalist), |
| 33 | + [ActivityLogLevels.Success]: yurnalist.success.bind(yurnalist), |
| 34 | + [ActivityLogLevels.Failed]: (text: string): void => { |
| 35 | + yurnalist.log(`${chalk.red(`failed`)} ${text}`) |
| 36 | + }, |
| 37 | + [ActivityLogLevels.Interrupted]: (text: string): void => { |
| 38 | + yurnalist.log(`${chalk.gray(`not finished`)} ${text}`) |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + onLogAction(action => { |
| 43 | + switch (action.type) { |
| 44 | + case Actions.Log: { |
| 45 | + const yurnalistMethod = levelToYurnalist[action.payload.level] |
| 46 | + if (!yurnalistMethod) { |
| 47 | + process.stdout.write(`NO "${action.payload.level}" method\n`) |
| 48 | + } else { |
| 49 | + let message = action.payload.text |
| 50 | + if (action.payload.duration) { |
| 51 | + message += ` - ${action.payload.duration.toFixed(3)}s` |
| 52 | + } |
| 53 | + if (action.payload.statusText) { |
| 54 | + message += ` - ${action.payload.statusText}` |
| 55 | + } |
| 56 | + yurnalistMethod(message) |
| 57 | + } |
| 58 | + break |
| 59 | + } |
| 60 | + case Actions.StartActivity: { |
| 61 | + if (action.payload.type === ActivityTypes.Spinner) { |
| 62 | + const spinner = yurnalist.activity() |
| 63 | + spinner.tick(action.payload.text) |
| 64 | + |
| 65 | + const activity = { |
| 66 | + text: action.payload.text, |
| 67 | + statusText: action.payload.statusText, |
| 68 | + update(payload: any): void { |
| 69 | + // TODO: I'm not convinced that this is ever called with a text property. |
| 70 | + // From searching the codebase it appears that we do not ever assign a text |
| 71 | + // property during the IUpdateActivity action. |
| 72 | + if (payload.text) { |
| 73 | + activity.text = payload.text |
| 74 | + } |
| 75 | + if (payload.statusText) { |
| 76 | + activity.statusText = payload.statusText |
| 77 | + } |
| 78 | + |
| 79 | + let message = activity.text |
| 80 | + if (activity.statusText) { |
| 81 | + message += ` - ${activity.statusText}` |
| 82 | + } |
| 83 | + |
| 84 | + message += ` id:"${action.payload.id}"` |
| 85 | + |
| 86 | + spinner.tick(message) |
| 87 | + }, |
| 88 | + end(): void { |
| 89 | + spinner.end() |
| 90 | + }, |
| 91 | + } |
| 92 | + activities[action.payload.id] = activity |
| 93 | + } else if (action.payload.type === ActivityTypes.Progress) { |
| 94 | + const bar = new ProgressBar( |
| 95 | + ` [:bar] :current/:total :elapsed s :percent ${action.payload.text}`, |
| 96 | + { |
| 97 | + total: action.payload.total, |
| 98 | + curr: action.payload.current, |
| 99 | + width: 30, |
| 100 | + clear: true, |
| 101 | + } |
| 102 | + ) |
| 103 | + |
| 104 | + activities[action.payload.id] = { |
| 105 | + text: undefined, |
| 106 | + statusText: undefined, |
| 107 | + update(payload): void { |
| 108 | + if (payload.total) { |
| 109 | + bar.total = payload.total |
| 110 | + } |
| 111 | + if (payload.current) { |
| 112 | + bar.curr = payload.current |
| 113 | + } |
| 114 | + |
| 115 | + bar.tick(0) |
| 116 | + }, |
| 117 | + end(): void {}, |
| 118 | + } |
| 119 | + } |
| 120 | + break |
| 121 | + } |
| 122 | + case Actions.UpdateActivity: { |
| 123 | + const activity = activities[action.payload.id] |
| 124 | + if (activity) { |
| 125 | + activity.update(action.payload) |
| 126 | + } |
| 127 | + break |
| 128 | + } |
| 129 | + case Actions.EndActivity: { |
| 130 | + const activity = activities[action.payload.id] |
| 131 | + if (activity) { |
| 132 | + activity.end() |
| 133 | + delete activities[action.payload.id] |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + }) |
| 138 | +} |
0 commit comments