Skip to content

Commit e5e316b

Browse files
fix(config): address bug preventing custom scope usage (#197)
* fix(config): address bug preventing custom scope usage * get rid of src/pf-docs.config.mjs symlink, add missing await * update tests
1 parent 16fc9aa commit e5e316b

File tree

7 files changed

+66
-55
lines changed

7 files changed

+66
-55
lines changed

cli/__tests__/symLinkConfig.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { symlink } from 'fs/promises'
1+
import { symlink, rm } from 'fs/promises'
22
import { symLinkConfig } from '../symLinkConfig'
3+
import { fileExists } from '../fileExists'
34

45
jest.mock('fs/promises')
6+
jest.mock('../fileExists')
57

68
// suppress console.log so that it doesn't clutter the test output
79
jest.spyOn(console, 'log').mockImplementation(() => {})
810

9-
it('should create a symlink successfully', async () => {
11+
it('should create a symlink successfully when one does not exist', async () => {
12+
;(fileExists as jest.Mock).mockResolvedValue(false)
1013
;(symlink as jest.Mock).mockResolvedValue(undefined)
1114

1215
await symLinkConfig('/astro', '/consumer')
@@ -17,11 +20,36 @@ it('should create a symlink successfully', async () => {
1720
)
1821
})
1922

23+
it('should not try to remove a file that does not exist', async () => {
24+
;(fileExists as jest.Mock).mockResolvedValue(false)
25+
;(symlink as jest.Mock).mockResolvedValue(undefined)
26+
27+
await symLinkConfig('/astro', '/consumer')
28+
29+
expect(rm).not.toHaveBeenCalled()
30+
})
31+
32+
it('should remove existing file before creating symlink', async () => {
33+
;(fileExists as jest.Mock).mockResolvedValue(true)
34+
;(rm as jest.Mock).mockResolvedValue(undefined)
35+
;(symlink as jest.Mock).mockResolvedValue(undefined)
36+
37+
await symLinkConfig('/astro', '/consumer')
38+
39+
expect(fileExists).toHaveBeenCalledWith('/astro/pf-docs.config.mjs')
40+
expect(rm).toHaveBeenCalledWith('/astro/pf-docs.config.mjs')
41+
expect(symlink).toHaveBeenCalledWith(
42+
'/consumer/pf-docs.config.mjs',
43+
'/astro/pf-docs.config.mjs',
44+
)
45+
})
46+
2047
it('should log an error if symlink creation fails', async () => {
2148
const consoleErrorSpy = jest
2249
.spyOn(console, 'error')
2350
.mockImplementation(() => {})
2451

52+
;(fileExists as jest.Mock).mockResolvedValue(false)
2553
const error = new Error('Symlink creation failed')
2654
;(symlink as jest.Mock).mockRejectedValue(error)
2755

@@ -36,6 +64,9 @@ it('should log an error if symlink creation fails', async () => {
3664
it('should log a success message after creating the symlink', async () => {
3765
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
3866

67+
;(fileExists as jest.Mock).mockResolvedValue(false)
68+
;(symlink as jest.Mock).mockResolvedValue(undefined)
69+
3970
await symLinkConfig('/astro', '/consumer')
4071

4172
expect(consoleLogSpy).toHaveBeenCalledWith(

cli/symLinkConfig.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
/* eslint-disable no-console */
2-
import { symlink } from 'fs/promises'
2+
import { symlink, rm } from 'fs/promises'
3+
import { join } from 'path'
4+
import { fileExists } from './fileExists.js'
35

46
export async function symLinkConfig(
57
astroRootDir: string,
68
consumerRootDir: string,
79
) {
810
const configFileName = '/pf-docs.config.mjs'
9-
const docsConfigFile = consumerRootDir + configFileName
11+
const defaultConfigFile = join(astroRootDir, configFileName)
12+
const consumerConfigFile = consumerRootDir + configFileName
13+
14+
if (await fileExists(defaultConfigFile)) {
15+
await rm(defaultConfigFile)
16+
}
1017

1118
try {
12-
await symlink(docsConfigFile, astroRootDir + configFileName)
19+
await symlink(consumerConfigFile, defaultConfigFile)
1320
} catch (e: any) {
1421
console.error(
15-
`Error creating symlink to ${docsConfigFile} in ${astroRootDir}`,
22+
`Error creating symlink to ${consumerConfigFile} in ${astroRootDir}`,
1623
e,
1724
)
1825
} finally {
19-
console.log(`Symlink to ${docsConfigFile} in ${astroRootDir} created`)
26+
console.log(`Symlink to ${consumerConfigFile} in ${astroRootDir} created`)
2027
}
2128
}

pf-docs.config.mjs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ export const config = {
1111
// example content entry for remote content, this would fetch all markdown files matching the glob in 'pattern'
1212
// from the specified npm package and serve them with a content identifier of 'react-component-docs':
1313
{
14-
packageName: "@patternfly/react-core",
15-
pattern: "**/components/**/*.md",
16-
name: "react-component-docs",
14+
packageName: '@patternfly/react-core',
15+
pattern: '**/components/**/*.md',
16+
name: 'react-component-docs',
17+
},
18+
{
19+
packageName: '@patternfly/patternfly',
20+
pattern: '*/**/*.md',
21+
name: 'core-docs',
22+
},
23+
{
24+
packageName: '@patternfly/quickstarts',
25+
pattern: '*/patternfly-docs/**/*.md',
26+
name: 'quickstarts-docs',
1727
},
18-
{ packageName: "@patternfly/patternfly", pattern: "*/**/*.md", name: "core-docs" },
19-
{ packageName: "@patternfly/quickstarts", pattern: "*/patternfly-docs/**/*.md", name: "quickstarts-docs" },
2028
],
2129
outputDir: './dist',
2230
propsGlobs: [
@@ -30,11 +38,6 @@ export const config = {
3038
],
3139
},
3240
],
33-
// Add custom scope items for LiveExample component
34-
// These will be available in your example code blocks
35-
// Example:
36-
// scope: {
37-
// MyCustomComponent: () => <div>Custom</div>,
38-
// myUtilFunction: (x) => x * 2,
39-
// },
41+
navSectionOrder: ['get-started', 'design-foundations'],
42+
scope: {},
4043
}

src/components/LiveExample.astro

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
import { LiveExample as LiveExampleBase } from './LiveExample'
3-
import { config } from '../pf-docs.config.mjs'
43
54
const { src, html } = Astro.props
65
---
76

8-
<LiveExampleBase src={src} html={html} customScope={config.scope} client:only="react" />
7+
<LiveExampleBase src={src} html={html} client:only="react" />

src/components/LiveExample.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import * as reactDragDropModule from '@patternfly/react-drag-drop'
1414
import styles from '@patternfly/react-styles/css/components/_index'
1515
import * as reactTokensModule from '@patternfly/react-tokens'
1616
import { ExampleToolbar } from './ExampleToolbar'
17+
import { config } from '../../pf-docs.config.mjs'
1718

1819
interface LiveExampleProps {
1920
src?: string
2021
html?: string
2122
isFullscreenPreview?: boolean
22-
customScope?: Record<string, any>
2323
}
2424

2525
function fallbackRender({ error }: any) {
@@ -31,15 +31,15 @@ function fallbackRender({ error }: any) {
3131
)
3232
}
3333

34-
function getLivePreview(editorCode: string, customScope?: Record<string, any>) {
34+
function getLivePreview(editorCode: string) {
3535
const scope = {
3636
...reactCoreModule,
3737
...reactIconsModule,
3838
...reactDragDropModule,
3939
styles,
4040
...reactTokensModule,
4141
...{ useState, Fragment, useRef, useEffect, createRef, useReducer },
42-
...customScope,
42+
...config.scope,
4343
}
4444
const { code: transformedCode } = convertToReactComponent(editorCode)
4545

@@ -60,7 +60,6 @@ export const LiveExample = ({
6060
src,
6161
html,
6262
isFullscreenPreview,
63-
customScope,
6463
}: LiveExampleProps) => {
6564
const inputCode = src || html || ''
6665
const [code, setCode] = useState(inputCode)
@@ -76,7 +75,7 @@ export const LiveExample = ({
7675
)
7776
lang = 'html'
7877
} else {
79-
livePreview = getLivePreview(code, customScope)
78+
livePreview = getLivePreview(code)
8079
lang = 'ts'
8180
}
8281

src/components/Navigation.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TextContentEntry } from './NavEntry.tsx'
66
77
import { content } from '../content'
88
9-
import { config } from '../pf-docs.config.mjs'
9+
import { config } from '../../pf-docs.config.mjs'
1010
1111
const collections = await Promise.all(
1212
content.map(

src/pf-docs.config.mjs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)