Skip to content

Commit

Permalink
chore(gatsby-cli): Migrate reporter to TypeScript (#22869)
Browse files Browse the repository at this point in the history
* chore(gatsby-cli): Migrate reporter to TypeScript

* Complete changes from Matts review

* fix tests

* fix enumeration of class
  • Loading branch information
blainekasten authored Apr 9, 2020
1 parent e691d76 commit d07c7b1
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 463 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@types/babel__code-frame": "^7.0.1",
"@types/bluebird": "^3.5.30",
"@types/cache-manager": "^2.10.2",
"@types/common-tags": "^1.8.0",
"@types/eslint": "^6.1.8",
"@types/express": "^4.17.3",
"@types/fast-levenshtein": "^0.0.1",
Expand All @@ -18,6 +19,8 @@
"@types/lodash": "^4.14.149",
"@types/node": "^12.12.30",
"@types/node-fetch": "^2.5.5",
"@types/semver": "^7.1.0",
"@types/signal-exit": "^3.0.0",
"@types/react": "^16.9.31",
"@types/stack-trace": "^0.0.29",
"@types/webpack": "^4.41.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ process.on(`unhandledRejection`, reason => {
reason = new Error(util.format(reason))
}

report.panic(`UNHANDLED REJECTION`, reason)
report.panic(`UNHANDLED REJECTION`, reason as Error)
})

process.on(`uncaughtException`, error => {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/reporter/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const reporter = require(`../index.js`)
const reporter = require(`../`)
const reporterActions = require(`../redux/actions`)

// TODO: report.error now DOES return something. Get rid of this spying mocking stuff
Expand Down
42 changes: 42 additions & 0 deletions packages/gatsby-cli/src/reporter/catch-exit-signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This module is used to catch if the user kills the gatsby process via cmd+c
* When this happens, there is some clean up logic we need to fire offf
*/
import signalExit from "signal-exit"
import { getStore } from "./redux"
import reporterActions from "./redux/actions"
import { ActivityStatuses } from "./constants"
import { reporter } from "./reporter"

const interruptActivities = (): void => {
const { activities } = getStore().getState().logs
Object.keys(activities).forEach(activityId => {
const activity = activities[activityId]
if (
activity.status === ActivityStatuses.InProgress ||
activity.status === ActivityStatuses.NotStarted
) {
reporter.completeActivity(activityId, ActivityStatuses.Interrupted)
}
})
}

export const prematureEnd = (): void => {
// hack so at least one activity is surely failed, so
// we are guaranteed to generate FAILED status
// if none of activity did explicitly fail
reporterActions.createPendingActivity({
id: `panic`,
status: ActivityStatuses.Failed,
})

interruptActivities()
}

export const catchExitSignals = (): void => {
signalExit((code, signal) => {
if (code !== 0 && signal !== `SIGINT` && signal !== `SIGTERM`)
prematureEnd()
else interruptActivities()
})
}
Loading

0 comments on commit d07c7b1

Please sign in to comment.