Skip to content

Commit

Permalink
Improve package.json load
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Oct 30, 2022
1 parent 3065e41 commit d2a4efb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/config/normalize/lib/error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import modernErrors from 'modern-errors'
import modernErrorsBugs from 'modern-errors-bugs'

import { packageJson } from '../../../utils/package.js'
import { bugs } from '../../../utils/package.js'

export const AnyError = modernErrors([modernErrorsBugs])

export const UnknownError = AnyError.subclass('UnknownError', {
bugs: packageJson.bugs.url,
})
export const UnknownError = AnyError.subclass('UnknownError', { bugs })

// Invalid `inputs`
export const InputError = AnyError.subclass('InputError')
Expand Down
12 changes: 5 additions & 7 deletions src/config/plugin/lib/error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import modernErrors from 'modern-errors'
import modernErrorsBugs from 'modern-errors-bugs'

import { packageJson } from '../../../utils/package.js'
import { bugs } from '../../../utils/package.js'

export const AnyError = modernErrors([modernErrorsBugs])

export const UnknownError = AnyError.subclass('UnknownError', {
bugs: packageJson.bugs.url,
})
export const UnknownError = AnyError.subclass('UnknownError', { bugs })

// Error from the library's user, who defines available plugin types
export const UserError = AnyError.subclass('UserError')
Expand All @@ -20,8 +18,8 @@ export const ConsumerError = AnyError.subclass('ConsumerError')

// When `options.bugs` is defined, user errors report it.
// When `plugin.bugs` is defined, plugin errors report it.
export const addErrorBugs = function (error, bugs) {
return typeof bugs === 'string' && bugs !== ''
? new AnyError('', { cause: error, bugs })
export const addErrorBugs = function (error, bugsOpt) {
return typeof bugsOpt === 'string' && bugsOpt !== ''
? new AnyError('', { cause: error, bugs: bugsOpt })
: error
}
2 changes: 2 additions & 0 deletions src/config/plugin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { normalizeReporters } from '../../report/config/main.js'
import { REPORTER_PLUGIN_TYPE } from '../../report/reporters/plugin/main.js'
import { RUNNER_PLUGIN_TYPE } from '../../runners/plugin/main.js'
import { cleanObject } from '../../utils/clean.js'
import { bugs } from '../../utils/package.js'
import { PREFIX } from '../normalize/main.js'

import { getPlugins } from './lib/main.js'
Expand Down Expand Up @@ -50,6 +51,7 @@ const normalizePluginConfigs = async function ({
context,
cwd,
prefix: PREFIX,
bugs,
})
const pluginInfosA = pluginInfos.map(normalizePluginInfo)
return { [name]: pluginInfosA }
Expand Down
4 changes: 2 additions & 2 deletions src/error/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import modernErrorsBugs from 'modern-errors-bugs'
import modernErrorsCli from 'modern-errors-cli'
import modernErrorsStack from 'modern-errors-stack'

import { packageJson } from '../utils/package.js'
import { bugs } from '../utils/package.js'

export const AnyError = modernErrors([
modernErrorsBugs,
Expand All @@ -12,7 +12,7 @@ export const AnyError = modernErrors([
])

export const UnknownError = AnyError.subclass('UnknownError', {
bugs: packageJson.bugs.url,
bugs,
cli: { exitCode: 1, header: 'red bold', icon: 'cross' },
})

Expand Down
6 changes: 2 additions & 4 deletions src/runners/common/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import modernErrors from 'modern-errors'
import modernErrorsBugs from 'modern-errors-bugs'
import modernErrorsSerialize from 'modern-errors-serialize'

import { packageJson } from '../../utils/package.js'
import { bugs } from '../../utils/package.js'

export const AnyError = modernErrors([modernErrorsBugs, modernErrorsSerialize])

export const UnknownError = AnyError.subclass('UnknownError', {
bugs: packageJson.bugs.url,
})
export const UnknownError = AnyError.subclass('UnknownError', { bugs })

// Could not JSON-stringify IPC payload
export const IpcSerializationError = AnyError.subclass('IpcSerializationError')
Expand Down
23 changes: 6 additions & 17 deletions src/runners/node/config/version.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { dirname } from 'path'
import { version as currentNodeVersion } from 'process'
import { fileURLToPath } from 'url'

import { readPackageUp } from 'read-pkg-up'
import semver from 'semver'

import { normalizeNumberString } from '../../../config/normalize/transform.js'
import { nodeVersion } from '../../../utils/package.js'
import { AnyError } from '../../common/error.js'

// Normalize and validate the Node.js version
// Normalize and validate the Node.js version.
// We can only allow Node versions that are valid with the runner's code.
const transformVersion = async function (version) {
const [versionInfo, allowedVersions] = await Promise.all([
normalizeVersion(version),
getAllowedVersions(),
])
const versionInfo = await normalizeVersion(version)

if (!semver.satisfies(versionInfo.version, allowedVersions)) {
throw new Error(`must be ${allowedVersions}`)
if (!semver.satisfies(versionInfo.version, nodeVersion)) {
throw new Error(`must be ${nodeVersion}`)
}

return versionInfo.version
Expand All @@ -38,13 +34,6 @@ const normalizeVersion = async function (version) {
}
}

// We can only allow Node versions that are valid with the runner's code
const getAllowedVersions = async function () {
const cwd = dirname(fileURLToPath(import.meta.url))
const { packageJson } = await readPackageUp({ cwd })
return packageJson.engines.node
}

export const versionRule = {
name: 'version',
schema: {
Expand Down
13 changes: 10 additions & 3 deletions src/utils/package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { createRequire } from 'module'

// TODO: replace with a JSON import after dropping support for Node <16.14.0
export const packageJson = createRequire(import.meta.url)(
'../../../package.json',
)
const {
bugs: { url },
engines: { node },
} = createRequire(import.meta.url)('../../../package.json')

// Bugs URL
export const bugs = url

// Node.js version
export const nodeVersion = node

0 comments on commit d2a4efb

Please sign in to comment.