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
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
- name: Npm Install
run: npm install
- name: Build dist
Expand All @@ -29,5 +29,5 @@ jobs:
- name: Release to NPM
env:
DEBUG: semantic-release:*
run: npx semantic-release@19.0.5 --branches main
run: npx semantic-release --branches main
#
15 changes: 14 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ export default defineConfig({
noExternal: ["@patternfly/*", "react-dropzone"],
external: ["fs", "node:fs", "node:path", "path", "fs/promises"]
},
environments: {
ssr: {
resolve: {
noExternal: ["@patternfly/*", "react-dropzone"],
},
},
prerender: {
resolve: {
noExternal: ["@patternfly/*", "react-dropzone"],
},
},
},
server: {
fs: {
allow: ['./']
}
},
},
adapter: cloudflare({
imageService: 'compile'
imageService: 'compile',
prerenderEnvironment: 'node',
})
});
160 changes: 150 additions & 10 deletions cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './buildPropsData.js'
import { hasFile } from './hasFile.js'
import { convertToMDX } from './convertToMDX.js'
import { mkdir, copyFile } from 'fs/promises'
import { mkdir, copyFile, readFile } from 'fs/promises'
import { fileURLToPath } from 'url'

const currentDir = process.cwd()
Expand Down Expand Up @@ -58,6 +58,10 @@ const absoluteOutputDir = resolve(currentDir, config.outputDir)

await mkdir(absoluteOutputDir, { recursive: true })

/**
* Generates the Astro content collections that back the docs site from the
* markdown sources configured in pf-docs.config.mjs.
*/
async function updateContent(program: Command) {
const { verbose } = program.opts()
if (!config) {
Expand All @@ -77,6 +81,10 @@ async function updateContent(program: Command) {
)
}

/**
* Generates the props data (props.json) for documented React components,
* either because the --props flag was passed or because a caller forced it.
*/
async function generateProps(program: Command, forceProps: boolean = false) {
const { verbose, props } = program.opts()
const { repoRoot } = config
Expand All @@ -93,6 +101,10 @@ async function generateProps(program: Command, forceProps: boolean = false) {
await buildPropsData(rootDir, `${currentDir}/pf-docs.config.mjs`, verbose)
}

/**
* Converts the configured Markdown content to MDX ahead of the Astro build
* so it can be loaded by the content collections.
*/
async function transformMDContentToMDX(verbose = false) {
if (!config.content) {
return
Expand Down Expand Up @@ -123,6 +135,11 @@ async function transformMDContentToMDX(verbose = false) {
)
}

/**
* Runs the full docs build: content collections, MDX conversion, props data,
* the Astro build, and copies the emitted API index to the root output
* directory for publication with the package.
*/
async function buildProject(program: Command): Promise<DocsConfig | undefined> {
const { verbose, apiOnly } = program.opts()

Expand Down Expand Up @@ -161,13 +178,108 @@ async function buildProject(program: Command): Promise<DocsConfig | undefined> {
outDir: docsOutputDir,
})
await copyFile(
join(docsOutputDir, 'apiIndex.json'),
join(docsOutputDir, 'client', 'apiIndex.json'),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
join(absoluteOutputDir, 'apiIndex.json'),
)

return config
}

/**
* Sanitizes an npm package name (e.g. "@scope/package") into a valid
* Cloudflare Workers name.
*/
const toWorkerName = (name: string) =>
name.replace(/^@/, '').toLowerCase().replace(/[^a-z0-9-]/g, '-')

/**
* Resolves the Workers name to deploy under. A consumer that customizes the
* wrangler configuration has that name merged into the generated config, and
* it wins. Otherwise the generated config carries a doc-core name (from the
* wrangler config bundled with this package, or the adapter's package-name
* default), which every consumer would share, so fall back to the consumer's
* own package name to keep each deployment on its own worker.
*/
async function getDeployName(serverDir: string): Promise<string | undefined> {
// When the CLI runs from the doc-core repo itself, its wrangler
// configuration is the explicit source of truth for the worker name.
if (resolve(currentDir) === resolve(astroRoot)) {
return undefined
}

const generated = JSON.parse(
await readFile(join(serverDir, 'wrangler.json'), 'utf8'),
)

// Names that indicate no consumer-provided wrangler configuration was
// merged into the generated config: doc-core's own configured worker name
// and the adapter's package-name default used when no config exists.
const docCoreNames = new Set<string>()
try {
const docCoreConfig = JSON.parse(
await readFile(join(astroRoot, 'wrangler.jsonc'), 'utf8'),
)
if (docCoreConfig.name) {
docCoreNames.add(docCoreConfig.name)
}
} catch {
// doc-core wrangler config unreadable
}
try {
docCoreNames.add(
toWorkerName(
JSON.parse(await readFile(join(astroRoot, 'package.json'), 'utf8'))
.name,
),
)
} catch {
// doc-core package.json unreadable
}

if (generated.name && !docCoreNames.has(generated.name)) {
return undefined
}

try {
return toWorkerName(
JSON.parse(await readFile(join(currentDir, 'package.json'), 'utf8'))
.name,
)
} catch {
return undefined
}
}

/**
* Resolves the JavaScript entrypoint of the locally installed Wrangler so it
* can be launched with the current Node executable, which works on every
* platform without relying on bin shims or a shell. Returns undefined when
* Wrangler cannot be resolved from this install.
*/
async function getWranglerEntry(): Promise<string | undefined> {
try {
const pkgPath = fileURLToPath(
import.meta.resolve('wrangler/package.json'),
)
const pkg = JSON.parse(await readFile(pkgPath, 'utf8'))
const bin = typeof pkg.bin === 'string' ? pkg.bin : pkg.bin?.wrangler
if (bin) {
return join(dirname(pkgPath), bin)
}
} catch {
// Wrangler not resolvable from this install
}
return undefined
}

/**
* Deploys the built docs to Cloudflare Workers.
*
* The Cloudflare adapter generates a self-contained wrangler.json next to
* the built worker (merged with any consumer wrangler settings). Wrangler is
* pointed at it explicitly, since running from the consumer root without a
* redirect would leave Wrangler unable to discover the configuration.
*/
async function deploy(program: Command) {
const { verbose, dryRun } = program.opts()

Expand All @@ -180,16 +292,44 @@ async function deploy(program: Command) {
return
}

try {
// Deploy using Wrangler
const { execSync } = await import('child_process')
const serverDir = join(absoluteOutputDir, 'docs', 'server')
const generatedConfig = join(serverDir, 'wrangler.json')

execSync(`wrangler pages deploy`, {
stdio: 'inherit',
cwd: currentDir,
})
if (!(await hasFile(generatedConfig))) {
console.error(
'No build output found, please run the `build` command before deploying',
)
process.exit(1)
}

console.log('Successfully deployed to Cloudflare Pages!')
try {
// Deploy using Wrangler. The JavaScript entrypoint is launched with the
// current Node executable because bin shims cannot be spawned without a
// shell on Windows, while a shell would split arguments containing
// spaces. The argument array preserves those paths.
const { execFileSync } = await import('child_process')

const workerName = await getDeployName(serverDir)
const deployArgs = ['deploy', '--config', generatedConfig]
if (workerName) {
deployArgs.push('--name', workerName)
}

const wranglerEntry = await getWranglerEntry()
if (wranglerEntry) {
execFileSync(process.execPath, [wranglerEntry, ...deployArgs], {
stdio: 'inherit',
cwd: currentDir,
})
} else {
// Fall back to the wrangler bin on PATH
execFileSync('wrangler', deployArgs, {
stdio: 'inherit',
cwd: currentDir,
})
}

console.log('Successfully deployed to Cloudflare Workers!')
} catch (error) {
console.error('Deployment failed:', error)
process.exit(1)
Expand Down
Loading
Loading