Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Add error reporter to path loaders to print syntax errors #115

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
25 changes: 16 additions & 9 deletions packages/cli/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,32 @@ const {
loadRawFromRoot,
loadFromRoot,
error,
exitWithError,
load,
} = require('@rescripts/utilities')

const errorReporter = (error) => {
if (error.code !== "ENOENT" && error.code !== "MODULE_NOT_FOUND") {
console.log(error);
}
}

const rootRescript = (() => {
const configKeyI = findIndex(
either(equals('--config'), equals('-c')),
process.argv,
)

const loaded =
configKeyI >= 0
? loadRawFromRoot(process.argv[configKeyI + 1]) ||
loadFromRoot(process.argv[configKeyI + 1]) ||
load(process.argv[configKeyI + 1])
(configKeyI >= 0
? loadRawFromRoot(process.argv[configKeyI + 1], errorReporter) ||
loadFromRoot(process.argv[configKeyI + 1], errorReporter) ||
load(process.argv[configKeyI + 1], errorReporter)
: loadFromPackageField('rescripts') ||
loadRawFromRoot('.rescriptsrc') ||
loadFromRoot('.rescriptsrc') ||
error(
"You're likely seeing this bug because you haven't defined a root rescript or your root rescript contains a syntactical error. If you're certain of otherwise, please file an issue.",
loadRawFromRoot('.rescriptsrc', errorReporter) ||
loadFromRoot('.rescriptsrc', errorReporter)) ||
exitWithError(
"Rescripts ran into an error. Either your root rescript isn't defined or it contains syntactical errors. If you're certain of otherwise, please file an issue.",
)

switch (type(loaded)) {
Expand Down Expand Up @@ -99,7 +106,7 @@ const gatherPipes = (scope, rescript = rootRescript) =>
}

default: {
error(
exitWithError(
'It seems that your Rescripts configuration is invalid. Please refer to the docs or post an issue if you believe this to be an internal bug. Thank you!',
)
return
Expand Down
5 changes: 5 additions & 0 deletions packages/utilities/errors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const error = message => {
console.error(message)
}

const exitWithError = message => {
error(message)
process.exit(1)
}

module.exports = {
error,
exitWithError,
}
9 changes: 3 additions & 6 deletions packages/utilities/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ const paths = {
),
}

const makeSafe = loader => p => {
const makeSafe = loader => (p, r) => {
try {
return loader(p)
} catch (e) {
r?.(e)
return null
}
}
Expand All @@ -86,11 +87,7 @@ const resolve = makeSafe(p => {
}
})

const createFromLoader = (loader, prefix) =>
pipe(
m => join(prefix, m),
loader,
)
const createFromLoader = (loader, prefix) => (m, r) => loader(join(prefix, m), r)

const {root, reactScriptsNodeModules} = paths
const loadFromRoot = createFromLoader(load, root)
Expand Down