Skip to content

Commit

Permalink
Simplify config
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 6, 2022
1 parent 08c1c26 commit ad12d5e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/config/load/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { findUp } from 'find-up'
import pLocate from 'p-locate'
import { pathExists } from 'path-exists'

import { CONFIG_PLUGIN_TYPE } from '../plugin/types.js'

import { CONFIG_FILENAMES } from './contents.js'
import { CONFIG_NPM_PREFIX } from './resolvers.js'

// The default values for `config` looks for `spyd.*` in the current or parent
// directories.
Expand Down Expand Up @@ -77,7 +76,7 @@ const testConfigPackageDir = async function (dir) {
}

const isConfigPackageDir = function (filename) {
return filename.startsWith(CONFIG_PLUGIN_TYPE.modulePrefix)
return filename.startsWith(CONFIG_NPM_PREFIX)
}

const findMatchingDir = async function (base, lookupDirs) {
Expand Down
4 changes: 2 additions & 2 deletions src/config/load/npx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { env } from 'process'

import { CONFIG_PLUGIN_TYPE } from '../plugin/types.js'
import { CONFIG_NPM_PREFIX } from './resolvers.js'

// In principle, users can use `npx` with the "npm" resolver by doing:
// npx --package=spyd-config-{name} spyd --config=spyd-config-{name} ...
Expand Down Expand Up @@ -43,5 +43,5 @@ const getNpxConfigs = function () {
}

const isSharedConfig = function (npxPackage) {
return npxPackage.startsWith(CONFIG_PLUGIN_TYPE.modulePrefix)
return npxPackage.startsWith(CONFIG_NPM_PREFIX)
}
7 changes: 4 additions & 3 deletions src/config/load/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getPluginPath } from '../plugin/import.js'
import { CONFIG_PLUGIN_TYPE } from '../plugin/types.js'

// The `config` can be:
// - a file path
Expand Down Expand Up @@ -27,11 +26,13 @@ export const useResolvers = async function (configOpt, base) {
// We do not use a shorter id like "npm:{name}" so users do not need two
// different ids: one for `npm install` and one for the `config` property.
const isNpmResolver = function (configOpt) {
return configOpt.startsWith(CONFIG_PLUGIN_TYPE.modulePrefix)
return configOpt.startsWith(CONFIG_NPM_PREFIX)
}

export const CONFIG_NPM_PREFIX = 'spyd-config-'

const resolveNpm = function (configOpt, base) {
return getPluginPath(configOpt, CONFIG_PLUGIN_TYPE.type, base)
return getPluginPath(configOpt, base)
}

// Additional resolvers.
Expand Down
12 changes: 3 additions & 9 deletions src/config/plugin/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const importPlugin = async function ({
const moduleName = `${modulePrefix}${moduleId}`
const pluginPath = safeGetPluginPath({
moduleName,
type,
selectPropName,
base: PLUGINS_IMPORT_BASE,
})
Expand All @@ -51,14 +50,9 @@ export const importPlugin = async function ({
// - This prevent the confusion (which could be malicious) created by the
// ambiguity
// TODO: use import.meta.resolve() when available
const safeGetPluginPath = function ({
moduleName,
type,
selectPropName,
base,
}) {
const safeGetPluginPath = function ({ moduleName, selectPropName, base }) {
try {
return getPluginPath(moduleName, type, base)
return getPluginPath(moduleName, base)
} catch (error) {
throw wrapError(
error,
Expand All @@ -68,7 +62,7 @@ const safeGetPluginPath = function ({
}
}

export const getPluginPath = function (moduleName, type, base) {
export const getPluginPath = function (moduleName, base) {
const { resolve } = createRequire(new URL(base, import.meta.url))

try {
Expand Down
7 changes: 0 additions & 7 deletions src/config/plugin/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ export const PLUGIN_TYPES = {
}

export const PLUGIN_TYPES_ARRAY = Object.values(PLUGIN_TYPES)

// Shared configurations are plugins when using the npm resolver.
// They are handled differently, i.e. require a separate type.
export const CONFIG_PLUGIN_TYPE = {
type: 'config',
modulePrefix: 'spyd-config-',
}
2 changes: 1 addition & 1 deletion src/runners/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { sharedProps } from './shared_props.js'
const pick = amongCommands(['dev', 'run'])

export const RUNNER_PLUGIN_TYPE = {
// Shown in error message
type: 'runner',
// Prefix of the npm package
modulePrefix: 'spyd-runner-',
Expand All @@ -28,6 +27,7 @@ export const RUNNER_PLUGIN_TYPE = {
builtins: BUILTIN_RUNNERS,
// Configuration property selecting the plugin
selectProp: {
// This `name` is also shown in error messages
name: 'runner',
pick,
default: DEFAULT_RUNNERS,
Expand Down

0 comments on commit ad12d5e

Please sign in to comment.