Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions cli/__tests__/symLinkConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { symlink } from 'fs/promises'
import { symlink, rm } from 'fs/promises'
import { symLinkConfig } from '../symLinkConfig'
import { fileExists } from '../fileExists'

jest.mock('fs/promises')
jest.mock('../fileExists')

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

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

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

it('should not try to remove a file that does not exist', async () => {
;(fileExists as jest.Mock).mockResolvedValue(false)
;(symlink as jest.Mock).mockResolvedValue(undefined)

await symLinkConfig('/astro', '/consumer')

expect(rm).not.toHaveBeenCalled()
})

it('should remove existing file before creating symlink', async () => {
;(fileExists as jest.Mock).mockResolvedValue(true)
;(rm as jest.Mock).mockResolvedValue(undefined)
;(symlink as jest.Mock).mockResolvedValue(undefined)

await symLinkConfig('/astro', '/consumer')

expect(fileExists).toHaveBeenCalledWith('/astro/pf-docs.config.mjs')
expect(rm).toHaveBeenCalledWith('/astro/pf-docs.config.mjs')
expect(symlink).toHaveBeenCalledWith(
'/consumer/pf-docs.config.mjs',
'/astro/pf-docs.config.mjs',
)
})

it('should log an error if symlink creation fails', async () => {
const consoleErrorSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => {})

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

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

;(fileExists as jest.Mock).mockResolvedValue(false)
;(symlink as jest.Mock).mockResolvedValue(undefined)

await symLinkConfig('/astro', '/consumer')

expect(consoleLogSpy).toHaveBeenCalledWith(
Expand Down
17 changes: 12 additions & 5 deletions cli/symLinkConfig.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
/* eslint-disable no-console */
import { symlink } from 'fs/promises'
import { symlink, rm } from 'fs/promises'
import { join } from 'path'
import { fileExists } from './fileExists.js'

export async function symLinkConfig(
astroRootDir: string,
consumerRootDir: string,
) {
const configFileName = '/pf-docs.config.mjs'
const docsConfigFile = consumerRootDir + configFileName
const defaultConfigFile = join(astroRootDir, configFileName)
const consumerConfigFile = consumerRootDir + configFileName

if (await fileExists(defaultConfigFile)) {
await rm(defaultConfigFile)
}

try {
await symlink(docsConfigFile, astroRootDir + configFileName)
await symlink(consumerConfigFile, defaultConfigFile)
} catch (e: any) {
console.error(
`Error creating symlink to ${docsConfigFile} in ${astroRootDir}`,
`Error creating symlink to ${consumerConfigFile} in ${astroRootDir}`,
e,
)
} finally {
console.log(`Symlink to ${docsConfigFile} in ${astroRootDir} created`)
console.log(`Symlink to ${consumerConfigFile} in ${astroRootDir} created`)
}
}
27 changes: 15 additions & 12 deletions pf-docs.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ export const config = {
// example content entry for remote content, this would fetch all markdown files matching the glob in 'pattern'
// from the specified npm package and serve them with a content identifier of 'react-component-docs':
{
packageName: "@patternfly/react-core",
pattern: "**/components/**/*.md",
name: "react-component-docs",
packageName: '@patternfly/react-core',
pattern: '**/components/**/*.md',
name: 'react-component-docs',
},
{
packageName: '@patternfly/patternfly',
pattern: '*/**/*.md',
name: 'core-docs',
},
{
packageName: '@patternfly/quickstarts',
pattern: '*/patternfly-docs/**/*.md',
name: 'quickstarts-docs',
},
{ packageName: "@patternfly/patternfly", pattern: "*/**/*.md", name: "core-docs" },
{ packageName: "@patternfly/quickstarts", pattern: "*/patternfly-docs/**/*.md", name: "quickstarts-docs" },
],
outputDir: './dist',
propsGlobs: [
Expand All @@ -30,11 +38,6 @@ export const config = {
],
},
],
// Add custom scope items for LiveExample component
// These will be available in your example code blocks
// Example:
// scope: {
// MyCustomComponent: () => <div>Custom</div>,
// myUtilFunction: (x) => x * 2,
// },
navSectionOrder: ['get-started', 'design-foundations'],
scope: {},
}
3 changes: 1 addition & 2 deletions src/components/LiveExample.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
import { LiveExample as LiveExampleBase } from './LiveExample'
import { config } from '../pf-docs.config.mjs'

const { src, html } = Astro.props
---

<LiveExampleBase src={src} html={html} customScope={config.scope} client:only="react" />
<LiveExampleBase src={src} html={html} client:only="react" />
9 changes: 4 additions & 5 deletions src/components/LiveExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import * as reactDragDropModule from '@patternfly/react-drag-drop'
import styles from '@patternfly/react-styles/css/components/_index'
import * as reactTokensModule from '@patternfly/react-tokens'
import { ExampleToolbar } from './ExampleToolbar'
import { config } from '../../pf-docs.config.mjs'

interface LiveExampleProps {
src?: string
html?: string
isFullscreenPreview?: boolean
customScope?: Record<string, any>
}

function fallbackRender({ error }: any) {
Expand All @@ -31,15 +31,15 @@ function fallbackRender({ error }: any) {
)
}

function getLivePreview(editorCode: string, customScope?: Record<string, any>) {
function getLivePreview(editorCode: string) {
const scope = {
...reactCoreModule,
...reactIconsModule,
...reactDragDropModule,
styles,
...reactTokensModule,
...{ useState, Fragment, useRef, useEffect, createRef, useReducer },
...customScope,
...config.scope,
}
const { code: transformedCode } = convertToReactComponent(editorCode)

Expand All @@ -60,7 +60,6 @@ export const LiveExample = ({
src,
html,
isFullscreenPreview,
customScope,
}: LiveExampleProps) => {
const inputCode = src || html || ''
const [code, setCode] = useState(inputCode)
Expand All @@ -76,7 +75,7 @@ export const LiveExample = ({
)
lang = 'html'
} else {
livePreview = getLivePreview(code, customScope)
livePreview = getLivePreview(code)
lang = 'ts'
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Navigation.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TextContentEntry } from './NavEntry.tsx'

import { content } from '../content'

import { config } from '../pf-docs.config.mjs'
import { config } from '../../pf-docs.config.mjs'

const collections = await Promise.all(
content.map(
Expand Down
28 changes: 0 additions & 28 deletions src/pf-docs.config.mjs

This file was deleted.