Skip to content

Commit

Permalink
fix(scheduled-publishing): don't include it if it's the only plugin a…
Browse files Browse the repository at this point in the history
…vailable (#6530)

* fix(scheduled-publishing): don't include the tool if it's the plugin available

* chore(core): add tests for resolve default plugins
  • Loading branch information
pedrobonamin committed May 15, 2024
1 parent fcd543a commit caa5567
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
54 changes: 54 additions & 0 deletions packages/sanity/src/core/config/__tests__/resolveConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {firstValueFrom, lastValueFrom, of} from 'rxjs'
import {bufferTime} from 'rxjs/operators'

import {createMockAuthStore} from '../../store'
import {definePlugin} from '../definePlugin'
import {createSourceFromConfig, createWorkspaceFromConfig, resolveConfig} from '../resolveConfig'

describe('resolveConfig', () => {
Expand Down Expand Up @@ -136,6 +137,59 @@ describe('resolveConfig', () => {
},
])
})

it('includes all the default plugins', async () => {
const projectId = 'ppsg7ml5'
const dataset = 'production'
const client = createClient({
projectId,
apiVersion: '2021-06-07',
dataset,
useCdn: false,
})
const mockPlugin = definePlugin({name: 'sanity/mock-plugin'})
const [workspace] = await firstValueFrom(
resolveConfig({
name: 'default',
dataset,
projectId,
auth: createMockAuthStore({client, currentUser: null}),
plugins: [mockPlugin()],
}),
)
expect(workspace.__internal.options.plugins).toMatchObject([
{name: 'sanity/mock-plugin'},
{name: 'sanity/comments'},
{name: 'sanity/tasks'},
{name: 'sanity/scheduled-publishing'},
])
})

it('wont include scheduled publishing default plugin', async () => {
// Schedule publishing should not be included when none other plugin is included in the user config.
const projectId = 'ppsg7ml5'
const dataset = 'production'
const client = createClient({
projectId,
apiVersion: '2021-06-07',
dataset,
useCdn: false,
})
const [workspace] = await firstValueFrom(
resolveConfig({
name: 'default',
dataset,
projectId,
auth: createMockAuthStore({client, currentUser: null}),
plugins: [], // No plugins
}),
)

expect(workspace.__internal.options.plugins).toMatchObject([
{name: 'sanity/comments'},
{name: 'sanity/tasks'},
])
})
})

describe('createWorkspaceFromConfig', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/sanity/src/core/config/prepareConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export function prepareConfig(

const {unstable_sources: nestedSources = [], ...rootSource} = rawWorkspace
const sources = [rootSource as SourceOptions, ...nestedSources].map(({plugins, ...source}) => {
return {...source, plugins: [...(plugins ?? []), ...getDefaultPlugins(defaultPluginsOptions)]}
return {
...source,
plugins: [...(plugins ?? []), ...getDefaultPlugins(defaultPluginsOptions, plugins)],
}
})

const resolvedSources = sources.map((source): InternalSource => {
Expand Down
9 changes: 7 additions & 2 deletions packages/sanity/src/core/config/resolveDefaultPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import {SCHEDULED_PUBLISHING_NAME, scheduledPublishing} from '../scheduledPublis
import {tasks, TASKS_NAME} from '../tasks/plugin'
import {
type DefaultPluginsWorkspaceOptions,
type PluginOptions,
type SingleWorkspace,
type WorkspaceOptions,
} from './types'

const defaultPlugins = [comments(), tasks(), scheduledPublishing()]

export function getDefaultPlugins(options: DefaultPluginsWorkspaceOptions) {
export function getDefaultPlugins(
options: DefaultPluginsWorkspaceOptions,
plugins?: PluginOptions[],
) {
return defaultPlugins.filter((plugin) => {
if (plugin.name === SCHEDULED_PUBLISHING_NAME) {
return options.scheduledPublishing.enabled
// The scheduled publishing plugin is only included if other plugin is included by the user.
return options.scheduledPublishing.enabled && !!plugins?.length
}
if (plugin.name === TASKS_NAME) {
return options.tasks.enabled
Expand Down

0 comments on commit caa5567

Please sign in to comment.