-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ts migration/convert yurnalist logger (#24224)
* chore(gatsby-cli): Convert yurnalist logger to TypeScript * chore(gatsby-cli): Convert yurnalist logger to TypeScript * chore(gatsby-cli): Convert yurnalist logger to TypeScript * fix lint error * support tsx * fix tsx support * fix build process in cli
- Loading branch information
1 parent
97a3d0c
commit 342d835
Showing
6 changed files
with
155 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 0 additions & 125 deletions
125
packages/gatsby-cli/src/reporter/loggers/yurnalist/index.js
This file was deleted.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
packages/gatsby-cli/src/reporter/loggers/yurnalist/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { onLogAction } from "../../redux" | ||
import { | ||
Actions, | ||
LogLevels, | ||
ActivityLogLevels, | ||
ActivityTypes, | ||
} from "../../constants" | ||
|
||
import { createReporter } from "yurnalist" | ||
import ProgressBar from "progress" | ||
import chalk from "chalk" | ||
import { IUpdateActivity } from "../../redux/types" | ||
|
||
interface IYurnalistActivities { | ||
[activityId: string]: { | ||
text: string | undefined | ||
statusText: string | undefined | ||
update(payload: IUpdateActivity["payload"]): void | ||
end(): void | ||
} | ||
} | ||
|
||
export function initializeYurnalistLogger(): void { | ||
const activities: IYurnalistActivities = {} | ||
const yurnalist = createReporter({ emoji: true, verbose: true }) | ||
|
||
const levelToYurnalist = { | ||
[LogLevels.Log]: yurnalist.log.bind(yurnalist), | ||
[LogLevels.Warning]: yurnalist.warn.bind(yurnalist), | ||
[LogLevels.Error]: yurnalist.error.bind(yurnalist), | ||
[LogLevels.Info]: yurnalist.info.bind(yurnalist), | ||
[LogLevels.Success]: yurnalist.success.bind(yurnalist), | ||
[ActivityLogLevels.Success]: yurnalist.success.bind(yurnalist), | ||
[ActivityLogLevels.Failed]: (text: string): void => { | ||
yurnalist.log(`${chalk.red(`failed`)} ${text}`) | ||
}, | ||
[ActivityLogLevels.Interrupted]: (text: string): void => { | ||
yurnalist.log(`${chalk.gray(`not finished`)} ${text}`) | ||
}, | ||
} | ||
|
||
onLogAction(action => { | ||
switch (action.type) { | ||
case Actions.Log: { | ||
const yurnalistMethod = levelToYurnalist[action.payload.level] | ||
if (!yurnalistMethod) { | ||
process.stdout.write(`NO "${action.payload.level}" method\n`) | ||
} else { | ||
let message = action.payload.text | ||
if (action.payload.duration) { | ||
message += ` - ${action.payload.duration.toFixed(3)}s` | ||
} | ||
if (action.payload.statusText) { | ||
message += ` - ${action.payload.statusText}` | ||
} | ||
yurnalistMethod(message) | ||
} | ||
break | ||
} | ||
case Actions.StartActivity: { | ||
if (action.payload.type === ActivityTypes.Spinner) { | ||
const spinner = yurnalist.activity() | ||
spinner.tick(action.payload.text) | ||
|
||
const activity = { | ||
text: action.payload.text, | ||
statusText: action.payload.statusText, | ||
update(payload: any): void { | ||
// TODO: I'm not convinced that this is ever called with a text property. | ||
// From searching the codebase it appears that we do not ever assign a text | ||
// property during the IUpdateActivity action. | ||
if (payload.text) { | ||
activity.text = payload.text | ||
} | ||
if (payload.statusText) { | ||
activity.statusText = payload.statusText | ||
} | ||
|
||
let message = activity.text | ||
if (activity.statusText) { | ||
message += ` - ${activity.statusText}` | ||
} | ||
|
||
message += ` id:"${action.payload.id}"` | ||
|
||
spinner.tick(message) | ||
}, | ||
end(): void { | ||
spinner.end() | ||
}, | ||
} | ||
activities[action.payload.id] = activity | ||
} else if (action.payload.type === ActivityTypes.Progress) { | ||
const bar = new ProgressBar( | ||
` [:bar] :current/:total :elapsed s :percent ${action.payload.text}`, | ||
{ | ||
total: action.payload.total, | ||
curr: action.payload.current, | ||
width: 30, | ||
clear: true, | ||
} | ||
) | ||
|
||
activities[action.payload.id] = { | ||
text: undefined, | ||
statusText: undefined, | ||
update(payload): void { | ||
if (payload.total) { | ||
bar.total = payload.total | ||
} | ||
if (payload.current) { | ||
bar.curr = payload.current | ||
} | ||
|
||
bar.tick(0) | ||
}, | ||
end(): void {}, | ||
} | ||
} | ||
break | ||
} | ||
case Actions.UpdateActivity: { | ||
const activity = activities[action.payload.id] | ||
if (activity) { | ||
activity.update(action.payload) | ||
} | ||
break | ||
} | ||
case Actions.EndActivity: { | ||
const activity = activities[action.payload.id] | ||
if (activity) { | ||
activity.end() | ||
delete activities[action.payload.id] | ||
} | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters