Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add logging action and middleware #13269

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ jest.mock(`fs`, () => {
}
})
jest.mock(`recursive-readdir`, () => jest.fn())
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panic: jest.fn(),
}
})

const fs = require(`fs`)
const readdir = require(`recursive-readdir`)

const reporter = require(`gatsby-cli/lib/reporter`)

const {
OPTION_DEFAULT_HTML,
OPTION_DEFAULT_REDIRECT_TEMPLATE_PATH,
Expand All @@ -27,7 +20,7 @@ const createPagesParams = {
actions: {
createPage,
},
reporter,
reporter: { panic: jest.fn() },
}

const throwFileNotFoundErr = path => {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"react-error-overlay": "^3.0.0",
"react-hot-loader": "^4.6.2",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
"request": "^2.85.0",
"semver": "^5.6.0",
"shallow-compare": "^1.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ jest.mock(`fs`, () => {
readFileSync: jest.fn(),
}
})
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panic: jest.fn(),
}
})

const fs = require(`fs`)
const reporter = require(`gatsby-cli/lib/reporter`)
const resolveModuleExports = require(`../resolve-module-exports`)
let resolver

const { store } = require(`../../redux`)
const log = jest.fn()
store.dispatch({ type: `SET_LOGGER`, payload: log })
afterEach(() => log.mockClear())

describe(`Resolve module exports`, () => {
const MOCK_FILE_INFO = {
"/bad/file": `const exports.blah = () = }}}`,
Expand Down Expand Up @@ -130,7 +129,6 @@ describe(`Resolve module exports`, () => {
const existing = MOCK_FILE_INFO[file]
return existing
})
reporter.panic.mockClear()
})

it(`Returns empty array for file paths that don't exist`, () => {
Expand All @@ -146,10 +144,10 @@ describe(`Resolve module exports`, () => {
it(`Show meaningful error message for invalid JavaScript`, () => {
resolveModuleExports(`/bad/file`, { resolver })
expect(
reporter.panic.mock.calls.map(c =>
log.mock.calls.map(c =>
// Remove console colors + trim whitespace
// eslint-disable-next-line
c[0].replace(/\x1B[[(?);]{0,2}(;?\d)*./g, ``).trim()
c[0].message.replace(/\x1B[[(?);]{0,2}(;?\d)*./g, ``).trim()
)
).toMatchSnapshot()
})
Expand Down
31 changes: 19 additions & 12 deletions packages/gatsby/src/bootstrap/get-config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
const levenshtein = require(`fast-levenshtein`)
const fs = require(`fs-extra`)
const testRequireError = require(`../utils/test-require-error`).default
const report = require(`gatsby-cli/lib/reporter`)
const chalk = require(`chalk`)
const path = require(`path`)
const existsSync = require(`fs-exists-cached`).sync
const { store } = require(`../redux`)
const { actions } = require(`../redux/actions`)

const { dispatch } = store
const { log } = actions

function isNearMatch(
fileName: string,
Expand All @@ -32,20 +36,23 @@ module.exports = async function getConfigFile(
})
)
if (!testRequireError(configPath, err)) {
report.panic(
`We encountered an error while trying to load your site's ${configName}. Please fix the error and try again.`,
const message =
`We encountered an error while trying to load your site's ${configName}. ` +
`Please fix the error and try again.\n` +
err
)
dispatch(log({ message, type: `panic` }))
} else if (nearMatch) {
report.panic(
`It looks like you were trying to add the config file? Please rename "${chalk.bold(
nearMatch
)}" to "${chalk.bold(configName)}"`
)
const message =
`It looks like you were trying to add the config file? ` +
`Please rename "${chalk.bold(nearMatch)}" to ` +
`"${chalk.bold(configName)}".`
dispatch(log({ message, type: `panic` }))
} else if (existsSync(path.join(rootDir, `src`, configName))) {
report.panic(
`Your ${configName} file is in the wrong place. You've placed it in the src/ directory. It must instead be at the root of your site next to your package.json file.`
)
const message =
`Your ${configName} file is in the wrong place. You've placed it in the ` +
`src/ directory. It must instead be at the root of your site next to ` +
`your package.json file.`
dispatch(log({ message, type: `panic` }))
}
}

Expand Down
Loading