Skip to content

Commit

Permalink
Merge pull request #652 from Financial-Times/fix-custom-plugins
Browse files Browse the repository at this point in the history
Fix issues with custom plugins
  • Loading branch information
ivomurrell committed Sep 5, 2024
2 parents bfbdafe + 026048e commit bb0f5ee
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 0 additions & 2 deletions core/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
"endent": "^2.1.0",
"lodash": "^4.17.21",
"minimist": "^1.2.5",
"resolve-from": "^5.0.0",
"resolve-pkg": "^2.0.0",
"tslib": "^2.3.1",
"yaml": "^2.4.1",
"zod-validation-error": "^2.1.0"
Expand Down
14 changes: 12 additions & 2 deletions core/cli/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { styles as s } from '@dotcom-tool-kit/logger'
import { CURRENT_RC_FILE_VERSION, type Plugin } from '@dotcom-tool-kit/plugin'
import type { RawConfig, ValidPluginsConfig } from '@dotcom-tool-kit/config'
import { invalid, reduceValidated, valid, Validated } from '@dotcom-tool-kit/validated'
import resolvePkg from 'resolve-pkg'
import * as path from 'path'
import type { Logger } from 'winston'
import { loadToolKitRC } from './rc-file'
import { indentReasons } from './messages'
Expand All @@ -13,6 +13,16 @@ import { mergePluginOptions } from './plugin/merge-plugin-options'
import { mergeInits } from './plugin/merge-inits'
import { mergeTaskOptions } from './plugin/merge-task-options'

function resolveRoot(id: string, root: string): string {
const isPath = id.startsWith('.') || id.startsWith('/')
// resolve the package.json of a plugin as many plugins don't have valid
// entrypoints now that we're intending their tasks/hooks to be loaded via
// entrypoints defined in config
const modulePath = isPath ? id : path.join(id, 'package.json')
const resolvedPath = require.resolve(modulePath, { paths: [root] })
return path.dirname(resolvedPath)
}

export async function loadPlugin(
id: string,
config: RawConfig,
Expand All @@ -28,7 +38,7 @@ export async function loadPlugin(

// load plugin relative to the parent plugin
const root = parent ? parent.root : process.cwd()
const pluginRoot = isAppRoot ? root : resolvePkg(id, { cwd: root })
const pluginRoot = isAppRoot ? root : resolveRoot(id, root)
if (!pluginRoot) {
return invalid([`could not find path for name ${s.filepath(id)}`])
}
Expand Down
4 changes: 1 addition & 3 deletions core/cli/src/plugin/entry-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { styles as s } from '@dotcom-tool-kit/logger'
import type { Base } from '@dotcom-tool-kit/base'
import type { EntryPoint } from '@dotcom-tool-kit/plugin'
import { Validated, invalid } from '@dotcom-tool-kit/validated'
import resolveFrom from 'resolve-from'
import resolvePkg from 'resolve-pkg'
import { isPlainObject } from 'lodash'
import { indentReasons } from '../messages'

Expand All @@ -16,7 +14,7 @@ export async function importEntryPoint<T extends { name: string } & Omit<typeof
type: T,
entryPoint: EntryPoint
): Promise<Validated<T>> {
const resolvedPath = resolveFrom.silent(entryPoint.plugin.root, entryPoint.modulePath)
const resolvedPath = require.resolve(entryPoint.modulePath, { paths: [entryPoint.plugin.root] })

if (!resolvedPath) {
return invalid([
Expand Down
5 changes: 3 additions & 2 deletions core/cli/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const loadTasks = async (
return taskResult.flatMap<[string, Task]>((Task) => {
const taskSchema = TaskSchemas[taskId as keyof TaskOptions]
const configOptions = config.taskOptions[taskId]?.options ?? {}
const parsedOptions = taskSchema?.safeParse({ ...configOptions, ...options }) ?? {
const mergedOptions = { ...configOptions, ...options }
const parsedOptions = taskSchema?.safeParse(mergedOptions) ?? {
success: true,
data: {}
data: mergedOptions
}

if (parsedOptions.success) {
Expand Down
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb0f5ee

Please sign in to comment.