Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scheduled-publishing): don't include it if it's the only plugin available #6530

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
jordanl17 marked this conversation as resolved.
Show resolved Hide resolved
return options.scheduledPublishing.enabled && !!plugins?.length
}
if (plugin.name === TASKS_NAME) {
return options.tasks.enabled
Expand Down