Skip to content

Commit fa9df94

Browse files
committed
Enhance fetch-docs script with plugin and version filtering options
1 parent fa6e453 commit fa9df94

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,41 @@ npm run fetch-docs
5656
npm run fetch-docs:cache
5757
```
5858

59+
##### Filtering by Plugin and/or Version
60+
61+
Pass `--plugin` and `--version` flags (directly via `tsx`) or use the `FETCH_PLUGIN` / `FETCH_VERSION` environment variables (recommended on Windows, where `npm run -- ` does not forward args correctly).
62+
63+
**PowerShell (Windows) — use env vars:**
64+
```powershell
65+
# Single plugin (all versions)
66+
$env:FETCH_PLUGIN='simple-contact-form'; npm run fetch-docs
67+
68+
# Single plugin, single version
69+
$env:FETCH_PLUGIN='simple-contact-form'; $env:FETCH_VERSION='2.x'; npm run fetch-docs
70+
71+
# Multiple plugins (comma-separated)
72+
$env:FETCH_PLUGIN='filament-tree,filament-firewall'; npm run fetch-docs
73+
74+
# Combined with cache-only
75+
$env:FETCH_PLUGIN='simple-contact-form'; npm run fetch-docs:cache
76+
```
77+
78+
**bash / macOS / Linux — env vars also work:**
79+
```bash
80+
FETCH_PLUGIN=simple-contact-form npm run fetch-docs
81+
FETCH_PLUGIN=simple-contact-form FETCH_VERSION=2.x npm run fetch-docs
82+
FETCH_PLUGIN=filament-tree,filament-firewall npm run fetch-docs
83+
```
84+
85+
**Or call `tsx` directly (works everywhere):**
86+
```bash
87+
npx tsx scripts/fetch-docs.ts --plugin=simple-contact-form
88+
npx tsx scripts/fetch-docs.ts --plugin=simple-contact-form --version=2.x
89+
npx tsx scripts/fetch-docs.ts --plugin=filament-tree --plugin=filament-firewall
90+
```
91+
92+
> **Note:** `npm run fetch-docs -- --plugin=...` does **not** work on Windows because npm strips args after `--` on that platform. Use the env var or direct `tsx` approach instead.
93+
5994
#### Build & Deploy
6095

6196
Since GitHub Actions may hit API limits easily, we recommend building locally and pushing the static site.
@@ -349,8 +384,12 @@ npm run build:cache # Build using cached documentation (faster)
349384
npm run build:static # Build for static export (used by GitHub Actions)
350385

351386
# Documentation Management
352-
npm run fetch-docs # Fetch documentation from GitHub
353-
npm run fetch-docs:cache # Use cached docs, skip GitHub fetch
387+
npm run fetch-docs # Fetch all documentation from GitHub
388+
npm run fetch-docs:cache # Use cached docs, skip GitHub fetch
389+
$env:FETCH_PLUGIN='<id>'; npm run fetch-docs # PowerShell: single plugin
390+
$env:FETCH_PLUGIN='<id>'; $env:FETCH_VERSION='<ver>'; npm run fetch-docs # PowerShell: plugin + version
391+
FETCH_PLUGIN=<id> npm run fetch-docs # bash: single plugin
392+
npx tsx scripts/fetch-docs.ts --plugin=<id> --version=<ver> # Direct tsx (all platforms)
354393

355394
# Type Checking
356395
npm run types:check # Run TypeScript type checking

scripts/fetch-docs.ts

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,50 @@ const octokit = new Octokit({
1111
});
1212

1313
// Parse command line arguments
14-
// Usage: tsx scripts/fetch-docs.ts [--cache-only|--offline]
15-
// --cache-only: Skip fetching from GitHub and use only cached content
14+
// Usage: tsx scripts/fetch-docs.ts [--cache-only|--offline] [--plugin=<id>] [--version=<ver>]
15+
// --cache-only / --offline : Skip fetching from GitHub and use only cached content
16+
// --plugin=<id> : Only fetch docs for the plugin with this id (can repeat)
17+
// --version=<ver> : Only fetch docs for this version string (can repeat)
18+
//
19+
// Examples:
20+
// tsx scripts/fetch-docs.ts --plugin=filament-tree
21+
// tsx scripts/fetch-docs.ts --plugin=filament-tree --version=4.x
22+
// tsx scripts/fetch-docs.ts --plugin=filament-tree --plugin=filament-firewall
1623
const args = process.argv.slice(2);
1724
const cacheOnly = args.includes('--cache-only') || args.includes('--offline');
1825

19-
if (cacheOnly) {
20-
console.log('🔌 Running in cache-only mode. Will not fetch from GitHub.');
26+
// Collect repeated --plugin and --version values (supports both --plugin=id and --plugin id)
27+
function parseRepeatedArg(argName: string, argv: string[]): string[] {
28+
const values: string[] = [];
29+
const prefix = `--${argName}=`;
30+
for (let i = 0; i < argv.length; i++) {
31+
if (argv[i].startsWith(prefix)) {
32+
values.push(argv[i].slice(prefix.length));
33+
} else if (argv[i] === `--${argName}` && i + 1 < argv.length) {
34+
values.push(argv[++i]);
35+
}
36+
}
37+
return values;
38+
}
39+
40+
// CLI args take priority; fall back to env vars (FETCH_PLUGIN / FETCH_VERSION).
41+
// Env vars support comma-separated values: FETCH_PLUGIN=filament-tree,filament-firewall
42+
function splitEnvList(val: string | undefined): string[] {
43+
return val ? val.split(',').map(s => s.trim()).filter(Boolean) : [];
2144
}
2245

46+
const filterPlugins = parseRepeatedArg('plugin', args).length
47+
? parseRepeatedArg('plugin', args)
48+
: splitEnvList(process.env.FETCH_PLUGIN);
49+
50+
const filterVersions = parseRepeatedArg('version', args).length
51+
? parseRepeatedArg('version', args)
52+
: splitEnvList(process.env.FETCH_VERSION);
53+
54+
if (cacheOnly) console.log('🔌 Running in cache-only mode. Will not fetch from GitHub.');
55+
if (filterPlugins.length) console.log(`🔍 Filtering plugins: ${filterPlugins.join(', ')}`);
56+
if (filterVersions.length) console.log(`🔍 Filtering versions: ${filterVersions.join(', ')}`);
57+
2358
function getCachePath(owner: string, repo: string, ref: string, path: string): string {
2459
// Use a Safe path structure: .cache/raw/owner/repo/ref/path
2560
// Replce any potential invalid characters in ref just in case, though usually refs are file-safe or close to it.
@@ -374,7 +409,17 @@ async function fetchPluginDocs() {
374409
const rootMetaPages: string[] = [];
375410
const rootMetaTitles: Record<string, string> = {};
376411

377-
for (const plugin of config.plugins) {
412+
const plugins = filterPlugins.length
413+
? config.plugins.filter(p => filterPlugins.includes(p.id))
414+
: config.plugins;
415+
416+
if (filterPlugins.length && plugins.length === 0) {
417+
console.error(`❌ No plugins matched: ${filterPlugins.join(', ')}`);
418+
console.error(` Available ids: ${config.plugins.map(p => p.id).join(', ')}`);
419+
process.exit(1);
420+
}
421+
422+
for (const plugin of plugins) {
378423
const [owner, repoName] = plugin.repo.split('/');
379424

380425
const pluginDir = join('content', 'docs', plugin.id);
@@ -397,7 +442,16 @@ async function fetchPluginDocs() {
397442
};
398443
writeFileSync(join(pluginDir, 'meta.json'), JSON.stringify(rootMeta, null, 2));
399444

400-
for (const version of plugin.versions) {
445+
const versions = filterVersions.length
446+
? plugin.versions.filter(v => filterVersions.includes(v.version))
447+
: plugin.versions;
448+
449+
if (filterVersions.length && versions.length === 0) {
450+
console.warn(`⚠️ No versions matched for ${plugin.id}: ${filterVersions.join(', ')} — skipping plugin.`);
451+
continue;
452+
}
453+
454+
for (const version of versions) {
401455
const versionDir = join('content', 'docs', plugin.id, version.version);
402456
mkdirSync(versionDir, { recursive: true });
403457

0 commit comments

Comments
 (0)