Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 54d727a

Browse files
committed
feat(packages/core): pass a PreloadRegistrar to plugin preloaders
Fixes #3189
1 parent 07aa3e3 commit 54d727a

File tree

18 files changed

+312
-25
lines changed

18 files changed

+312
-25
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2019 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* This file introduces a "test mmr mode-via-registration" command
19+
* that opens the sidecar with the MyResource resource type. We expect
20+
* that the modes and badges come from the mode registrations in
21+
* preload.ts.
22+
*
23+
*/
24+
25+
import { Commands, UI } from '@kui-shell/core'
26+
import { metadataWithNameOnly as metadata } from './metadata'
27+
28+
// exporting these for consumption in tests
29+
export const command = 'test mmr mode-via-registration'
30+
export { metadata }
31+
32+
// these are the modes we return to the REPL, not the modes we expect
33+
// to be displayed; those modes come from the mode registrations in
34+
// preload.ts
35+
const modes: UI.MultiModalMode[] = []
36+
37+
interface Options extends Commands.ParsedOptions {
38+
foo: boolean
39+
}
40+
41+
/**
42+
* e.g.
43+
*
44+
* test mmr mode-via-registration
45+
* test mmr mode-via-registration --foo
46+
*
47+
*/
48+
const doModes = (): ((args: Commands.Arguments<Options>) => UI.MultiModalResponse) => {
49+
return (args: Commands.Arguments<Options>) => Object.assign(metadata, { foo: !!args.parsedOptions.foo, modes })
50+
}
51+
52+
export default (commandTree: Commands.Registrar) => {
53+
commandTree.listen(`/${command.split(/ /).join('/')}`, doModes(), {
54+
usage: {
55+
docs: 'A test of MultiModalResponse mode'
56+
},
57+
flags: {
58+
boolean: ['foo']
59+
}
60+
})
61+
}

clients/test/plugins/plugin-test/src/lib/cmds/mmr-mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* This file introduces a "test mmr mode" command that opens the sidecar with
19-
* some texts modes.
19+
* some text modes.
2020
*
2121
*/
2222

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2019 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { BadgeParam, ModeParam } from '@kui-shell/test'
18+
import { Tab } from '@kui-shell/core/api/tab'
19+
import Models from '@kui-shell/core/api/models'
20+
import { BadgeRegistration, ModeRegistration } from '@kui-shell/core/api/registrars'
21+
22+
export interface MyResource extends Models.ResourceWithMetadata {
23+
foo: boolean
24+
}
25+
26+
function isMyResource(resource: Models.ResourceWithMetadata): resource is MyResource {
27+
return typeof (resource as MyResource).foo === 'boolean'
28+
}
29+
30+
export const mode1 = {
31+
when: isMyResource,
32+
mode: {
33+
mode: 'mode1',
34+
content: 'yo: this is mode1'
35+
}
36+
}
37+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
38+
const checkingModeType1: ModeRegistration<MyResource> = mode1
39+
40+
export const mode2 = {
41+
when: isMyResource,
42+
mode: {
43+
mode: 'mode2',
44+
content: () => ({
45+
content: 'this is mode2'
46+
})
47+
}
48+
}
49+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
50+
const checkingModeType2: ModeRegistration<MyResource> = mode2
51+
52+
export const mode3 = {
53+
when: isMyResource,
54+
mode: {
55+
mode: 'mode3',
56+
label: 'mode3 label',
57+
content: (_: Tab, resource: MyResource) =>
58+
Promise.resolve({
59+
// <-- just for fun, return a Promise, as this is supported by the API
60+
content: `# this is mode3\nvalue is ${resource.foo}`, // <-- here, we take a command line option and splice it into the view
61+
contentType: 'text/markdown'
62+
})
63+
}
64+
}
65+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66+
const checkingModeType3: ModeRegistration<MyResource> = mode3
67+
68+
export const badge1 = {
69+
when: isMyResource,
70+
badge: {
71+
title: 'badge1'
72+
}
73+
}
74+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
75+
const checkingBadgeType1: BadgeRegistration<MyResource> = badge1
76+
77+
export const badge2 = {
78+
when: isMyResource,
79+
badge: {
80+
title: 'badge2',
81+
css: 'red-background'
82+
}
83+
}
84+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
85+
const checkingBadgeType2: BadgeRegistration<MyResource> = badge2
86+
87+
// these are for the tests
88+
export const modesWeWillRegister: ModeParam[] = [mode1, mode2, mode3].map(_ => _.mode)
89+
export const badgesWeWillRegister: BadgeParam[] = [badge1, badge2].map(_ => _.badge)

clients/test/plugins/plugin-test/src/plugin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ import mmrName from './lib/cmds/mmr-name'
2121
import mmrNamespace from './lib/cmds/mmr-namespace'
2222
import mmrKind from './lib/cmds/mmr-kind'
2323
import mmrMode from './lib/cmds/mmr-mode'
24+
import mmrModeViaRegistration from './lib/cmds/mmr-mode-via-registration'
2425

2526
export default async (commandTree: Commands.Registrar) => {
2627
// commands
27-
await Promise.all([sayHello(commandTree), mmrName(commandTree), mmrNamespace(commandTree), mmrKind(commandTree), mmrMode(commandTree)])
28+
await Promise.all([
29+
sayHello(commandTree),
30+
mmrName(commandTree),
31+
mmrNamespace(commandTree),
32+
mmrKind(commandTree),
33+
mmrMode(commandTree),
34+
mmrModeViaRegistration(commandTree)
35+
])
2836
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { isHeadless } from '@kui-shell/core/api/capabilities'
18+
import { PreloadRegistrar } from '@kui-shell/core/api/registrars'
19+
20+
import { mode1, mode2, mode3, badge1, badge2 } from './lib/modes'
21+
22+
export default async (registrar: PreloadRegistrar) => {
23+
if (!isHeadless()) {
24+
registrar.registerModes(mode1, mode2, mode3)
25+
registrar.registerBadges(badge1, badge2)
26+
}
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { TestMMR } from '@kui-shell/test'
18+
19+
import { command, metadata as _meta } from '../../lib/cmds/mmr-mode-via-registration'
20+
import { badgesWeWillRegister as badges, modesWeWillRegister as modes } from '../../lib/modes'
21+
22+
const { metadata } = _meta
23+
24+
const test = new TestMMR({
25+
metadata,
26+
command,
27+
modes
28+
})
29+
30+
test.badges({
31+
name: 'mmr-mode-via-registration',
32+
badges
33+
})
34+
35+
test.modes({
36+
name: 'mmr-mode-via-registration',
37+
windowButtons: true
38+
})

packages/core/src/api/registrars.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ export {
2121
registerModeWhen,
2222
registerSidecarMode as registerMode
2323
} from '../webapp/views/registrar/modes'
24+
2425
export { BadgeRegistration, registerSidecarBadge as registerBadge } from '../webapp/views/registrar/badges'
2526
export { Badge } from '../webapp/views/badge'
27+
28+
export { PreloadRegistration, PreloadRegistrar } from '../models/plugin'

packages/core/src/models/mmr/content-types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ export interface StringContent<ContentType = 'yaml' | 'text/markdown' | 'text/ht
4949
contentType?: ContentType
5050
}
5151

52-
export function isStringWithContentType(entity: Entity | SidecarMode): entity is StringContent {
53-
const string = entity as StringContent
54-
return string && string.content !== undefined && string.contentType !== undefined
52+
export function isStringWithOptionalContentType(entity: Entity | SidecarMode): entity is StringContent {
53+
const str = entity as StringContent
54+
return (
55+
str && typeof str.content === 'string' && (str.contentType === undefined || typeof str.contentType === 'string')
56+
)
5557
}
5658

5759
/**

packages/core/src/models/mmr/show.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
ScalarContent,
3030
isScalarContent,
3131
isCommandStringContent,
32-
isStringWithContentType,
32+
isStringWithOptionalContentType,
3333
isFunctionContent
3434
} from './content-types'
3535

@@ -105,7 +105,7 @@ async function renderContent<T extends MetadataBearing>(
105105
bearer: T,
106106
content: string | object
107107
): Promise<ScalarContent> {
108-
if (isStringWithContentType(content)) {
108+
if (isStringWithOptionalContentType(content)) {
109109
return content
110110
} else if (isTable(content) || isMultiTable(content)) {
111111
return {

packages/core/src/models/mmr/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export type DrilldownButton = Button & {
9696
}
9797

9898
export function isButton<T extends MetadataBearing>(mode: Button<T> | Content<T> | SidecarMode): mode is Button<T> {
99-
return (mode as Button).command !== undefined
99+
return mode !== undefined && (mode as Button).command !== undefined
100100
}
101101

102102
/**

0 commit comments

Comments
 (0)