Skip to content

Commit 864758b

Browse files
Misc bug fixes for the cli, live examples, and prop/css tables (#115)
* fix(docs tables): fix props/css tables no longer rendering * fix(LiveExample): add pf/react-drag-drop to live example scope * fix(cli): support removing imports with semicolons at line endings * fix(cli): ensure that files actually exist before converting to mdx * fix(cli): prevent mdx conversion race conditions by making it async * fix(cli): update unit test * fix(cli): use path.join instead of manual slash insertion for paths
1 parent e35b760 commit 864758b

File tree

13 files changed

+225
-88
lines changed

13 files changed

+225
-88
lines changed

cli/__tests__/convertToMDX.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jest.mock('fs/promises', () => ({
66
readFile: jest.fn(),
77
writeFile: jest.fn(),
88
unlink: jest.fn(),
9+
access: jest.fn().mockResolvedValue(undefined), // Mock access to always resolve (file exists)
910
}))
1011

1112
jest.mock('glob', () => ({

cli/cli.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import { buildPropsData } from './buildPropsData.js'
1313
import { hasFile } from './hasFile.js'
1414
import { convertToMDX } from './convertToMDX.js'
1515

16-
function updateContent(program: Command) {
16+
async function updateContent(program: Command) {
1717
const { verbose } = program.opts()
1818

1919
if (verbose) {
2020
console.log('Verbose mode enabled')
2121
}
2222

23-
createCollectionContent(
23+
await createCollectionContent(
2424
astroRoot,
2525
`${process.cwd()}/pf-docs.config.mjs`,
2626
verbose,
@@ -45,39 +45,58 @@ async function generateProps(program: Command, forceProps: boolean = false) {
4545
verbose,
4646
)
4747
}
48+
49+
async function transformMDContentToMDX() {
50+
const config = await getConfig(`${currentDir}/pf-docs.config.mjs`)
51+
if (!config) {
52+
console.error(
53+
'No config found, please run the `setup` command or manually create a pf-docs.config.mjs file',
54+
)
55+
return config
56+
}
57+
58+
if (config.content) {
59+
await Promise.all(
60+
config.content.map((contentObj) => convertToMDX(contentObj.pattern)),
61+
)
62+
}
63+
}
64+
4865
async function buildProject(): Promise<DocsConfig | undefined> {
49-
updateContent(program)
66+
await updateContent(program)
5067
await generateProps(program, true)
5168
const config = await getConfig(`${currentDir}/pf-docs.config.mjs`)
5269
if (!config) {
5370
console.error(
5471
'No config found, please run the `setup` command or manually create a pf-docs.config.mjs file',
5572
)
56-
return config;
73+
return config
5774
}
5875

5976
if (!config.outputDir) {
6077
console.error(
6178
"No outputDir found in config file, an output directory must be defined in your config file e.g. 'dist'",
6279
)
63-
return config;
80+
return config
6481
}
6582

83+
await transformMDContentToMDX()
84+
6685
build({ root: astroRoot, outDir: join(currentDir, config.outputDir) })
67-
68-
return config;
86+
87+
return config
6988
}
7089

7190
async function deploy() {
7291
const { verbose } = program.opts()
73-
92+
7493
if (verbose) {
7594
console.log('Starting Cloudflare deployment...')
7695
}
7796

7897
try {
7998
// First build the project
80-
const config = await buildProject();
99+
const config = await buildProject()
81100
if (config) {
82101
if (verbose) {
83102
console.log('Build complete, deploying to Cloudflare...')
@@ -86,12 +105,12 @@ async function deploy() {
86105
// Deploy using Wrangler
87106
const { execSync } = await import('child_process')
88107
const outputPath = join(currentDir, config.outputDir)
89-
108+
90109
execSync(`npx wrangler pages deploy ${outputPath}`, {
91110
stdio: 'inherit',
92-
cwd: currentDir
111+
cwd: currentDir,
93112
})
94-
113+
95114
console.log('Successfully deployed to Cloudflare Pages!')
96115
}
97116
} catch (error) {
@@ -143,7 +162,7 @@ program.command('init').action(async () => {
143162
})
144163

145164
program.command('start').action(async () => {
146-
updateContent(program)
165+
await updateContent(program)
147166

148167
// if a props file hasn't been generated yet, but the consumer has propsData, it will cause a runtime error so to
149168
// prevent that we're just creating a props file regardless of what they say if one doesn't exist yet
@@ -153,7 +172,7 @@ program.command('start').action(async () => {
153172
})
154173

155174
program.command('build').action(async () => {
156-
await buildProject();
175+
await buildProject()
157176
})
158177

159178
program.command('generate-props').action(async () => {
@@ -162,7 +181,7 @@ program.command('generate-props').action(async () => {
162181
})
163182

164183
program.command('serve').action(async () => {
165-
updateContent(program)
184+
await updateContent(program)
166185
preview({ root: astroRoot })
167186
})
168187

@@ -178,7 +197,7 @@ program
178197
})
179198

180199
program.command('deploy').action(async () => {
181-
await deploy()
200+
await deploy()
182201
})
183202

184203
program.parse(process.argv)

cli/convertToMDX.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFile, writeFile, unlink } from 'fs/promises'
1+
import { readFile, writeFile, unlink, access } from 'fs/promises'
22
import { glob } from 'glob'
33
import path from 'path'
44

@@ -46,7 +46,7 @@ function removeNoLiveTags(content: string): string {
4646

4747
function removeExistingImports(content: string): string {
4848
// Remove imports that don't end in .css
49-
const importRegex = /^import {?[\w\s,\n]*}? from ['"](?!.*\.css['"])[^'"]*['"]\n/gm
49+
const importRegex = /^import {?[\w\s,\n]*}? from ['"](?!.*\.css['"])[^'"]*['"];?\n/gm
5050
return content.replace(importRegex, '')
5151
}
5252

@@ -57,8 +57,15 @@ function convertCommentsToMDX(content: string): string {
5757
)
5858
}
5959

60+
async function fileExists(file: string): Promise<boolean> {
61+
return access(file).then(() => true).catch(() => false)
62+
}
63+
6064
async function processFile(file: string): Promise<void> {
61-
if (file.endsWith('.mdx')) {
65+
const exists = await fileExists(file)
66+
67+
// if the file is already an mdx file or doesn't exist we don't need to do anything
68+
if (file.endsWith('.mdx') || !exists) {
6269
return
6370
}
6471

cli/createCollectionContent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-console */
22
import { writeFile } from 'fs/promises'
3+
import { join } from 'path'
34
import { getConfig } from './getConfig.js'
45

56
export async function createCollectionContent(rootDir: string, configFile: string, verbose: boolean) {
@@ -14,7 +15,7 @@ export async function createCollectionContent(rootDir: string, configFile: strin
1415
return
1516
}
1617

17-
const contentFile = rootDir + 'src/content.ts'
18+
const contentFile = join(rootDir, 'src', 'content.ts')
1819

1920
try {
2021
await writeFile(

package-lock.json

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
"access": "public"
88
},
99
"scripts": {
10-
"dev": "astro dev",
11-
"start": "astro dev",
12-
"build": "astro check && astro build",
10+
"dev": "npm run start:cli",
11+
"start": "npm run dev",
12+
"start:cli": "npm run build:cli && node ./dist/cli/cli.js start",
13+
"start:astro": "astro dev",
14+
"build": "npm run build:cli && node ./dist/cli/cli.js build",
15+
"build:astro": "astro check && astro build",
1316
"build:cli": "tsc --build ./cli/tsconfig.json",
1417
"build:cli:watch": "tsc --build --watch ./cli/tsconfig.json",
1518
"build:props": "npm run build:cli && node ./dist/cli/cli.js generate-props",
@@ -52,6 +55,7 @@
5255
"@patternfly/patternfly": "^6.0.0",
5356
"@patternfly/react-code-editor": "^6.2.2",
5457
"@patternfly/react-core": "^6.0.0",
58+
"@patternfly/react-drag-drop": "^6.0.0",
5559
"@patternfly/react-icons": "^6.0.0",
5660
"@patternfly/react-styles": "^6.0.0",
5761
"@patternfly/react-table": "^6.0.0",

src/components/DocsTables.astro

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
22
import { Stack, StackItem } from '@patternfly/react-core'
3-
import PropsTables from './PropsTables.astro'
3+
import { PropsTables } from './PropsTables'
44
import CSSTable from './CSSTable.astro'
55
66
const { propComponents, cssPrefix } = Astro.props
77
8+
const { url } = Astro
9+
810
const hasTables = !!propComponents || !!cssPrefix
911
---
1012

@@ -14,12 +16,16 @@ const hasTables = !!propComponents || !!cssPrefix
1416
<Stack hasGutter>
1517
{propComponents && (
1618
<StackItem>
17-
<PropsTables propComponents={propComponents} server:defer />
19+
<PropsTables
20+
propComponents={propComponents}
21+
url={url}
22+
client:only="react"
23+
/>
1824
</StackItem>
1925
)}
2026
{cssPrefix && (
2127
<StackItem>
22-
<CSSTable cssPrefix={cssPrefix} server:defer />
28+
<CSSTable cssPrefix={cssPrefix} />
2329
</StackItem>
2430
)}
2531
</Stack>

src/components/LiveExample.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { convertToReactComponent } from '@patternfly/ast-helpers'
1010
import { ErrorBoundary } from 'react-error-boundary'
1111
import * as reactCoreModule from '@patternfly/react-core'
1212
import * as reactIconsModule from '@patternfly/react-icons'
13+
import * as reactDragDropModule from '@patternfly/react-drag-drop'
1314
import styles from '@patternfly/react-styles/css/components/_index'
1415
import * as reactTokensModule from '@patternfly/react-tokens'
1516
import { ExampleToolbar } from './ExampleToolbar'
@@ -33,6 +34,7 @@ function getLivePreview(editorCode: string) {
3334
const scope = {
3435
...reactCoreModule,
3536
...reactIconsModule,
37+
...reactDragDropModule,
3638
styles,
3739
...reactTokensModule,
3840
...{ useState, Fragment, useRef, useEffect, createRef, useReducer },

0 commit comments

Comments
 (0)