Skip to content

Commit ba3e30a

Browse files
committed
chore(site): fix generating examples
1 parent 72676eb commit ba3e30a

File tree

8 files changed

+142
-346
lines changed

8 files changed

+142
-346
lines changed

pnpm-lock.yaml

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

website/scripts/examplesData.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Shared example metadata used by both generateExamples.mjs and generateReadme.mjs
1+
// Shared example metadata used by both generateExamples.mjs
22

33
export const EXAMPLE_METADATA = {
44
"ai-agent": {

website/scripts/generateExamples.mjs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import { execSync } from 'node:child_process';
44
import { readFileSync, writeFileSync, existsSync, mkdirSync, cpSync, rmSync } from 'node:fs';
5-
import { join } from 'node:path';
5+
import { join, dirname } from 'node:path';
6+
import { fileURLToPath } from 'node:url';
67
import { EXAMPLE_METADATA } from './examplesData.mjs';
78

8-
const REPO_URL = 'https://github.com/rivet-dev/rivetkit.git';
9-
const BRANCH = 'main';
10-
const TEMP_DIR = '/tmp/rivetkit-examples';
9+
const __dirname = dirname(fileURLToPath(import.meta.url));
10+
const EXAMPLES_DIR = join(__dirname, '../../examples');
1111
const TEMP_EXAMPLE_DIR = '/tmp/rivet-example-temp';
1212
const OUTPUT_DIR = './src/data/examples';
1313
const OUTPUT_FILE = 'examples.ts';
@@ -17,57 +17,78 @@ if (!existsSync(OUTPUT_DIR)) {
1717
mkdirSync(OUTPUT_DIR, { recursive: true });
1818
}
1919

20-
// Clone or update the repository
21-
function updateRepo() {
22-
if (existsSync(TEMP_DIR)) {
23-
console.log('Updating existing repository...');
24-
execSync('git clean -fd', { cwd: TEMP_DIR });
25-
execSync('git reset --hard', { cwd: TEMP_DIR });
26-
execSync('git fetch origin', { cwd: TEMP_DIR });
27-
execSync(`git checkout ${BRANCH}`, { cwd: TEMP_DIR });
28-
execSync(`git pull origin ${BRANCH}`, { cwd: TEMP_DIR });
29-
} else {
30-
console.log('Cloning repository...');
31-
execSync(`git clone -b ${BRANCH} ${REPO_URL} ${TEMP_DIR}`);
20+
// Verify examples directory exists
21+
function verifyExamplesDir() {
22+
if (!existsSync(EXAMPLES_DIR)) {
23+
throw new Error(`Examples directory not found at ${EXAMPLES_DIR}`);
3224
}
25+
console.log(`Using examples from: ${EXAMPLES_DIR}`);
3326
}
3427

35-
// Replace workspace dependencies with version numbers
28+
// Get latest version from npm
29+
function getLatestVersion(packageName) {
30+
try {
31+
const result = execSync(`npm view ${packageName} version`, { encoding: 'utf-8' });
32+
return result.trim();
33+
} catch (error) {
34+
console.warn(`Warning: Could not fetch version for ${packageName}, using ^0.9.1`);
35+
return '0.9.1';
36+
}
37+
}
38+
39+
// Cache for npm versions to avoid repeated lookups
40+
const versionCache = {};
41+
42+
// Replace workspace dependencies with latest npm versions
3643
function replaceWorkspaceDependencies(content) {
37-
return content
38-
.replace(/@rivetkit\/([^"]+)": "workspace:\*"/g, '@rivetkit/$1": "^0.9.1"')
39-
.replace(/"workspace:\*"/g, '"^0.9.1"');
44+
const packageJson = JSON.parse(content);
45+
46+
// Process both dependencies and devDependencies
47+
for (const depType of ['dependencies', 'devDependencies']) {
48+
if (packageJson[depType]) {
49+
for (const [pkgName, pkgVersion] of Object.entries(packageJson[depType])) {
50+
if (pkgVersion === 'workspace:*') {
51+
// Get version from cache or fetch from npm
52+
if (!versionCache[pkgName]) {
53+
console.log(`Fetching latest version for ${pkgName}...`);
54+
versionCache[pkgName] = getLatestVersion(pkgName);
55+
}
56+
packageJson[depType][pkgName] = `^${versionCache[pkgName]}`;
57+
}
58+
}
59+
}
60+
}
61+
62+
return JSON.stringify(packageJson, null, '\t');
4063
}
4164

4265
// Get only the examples defined in metadata
4366
function getExamplesToProcess() {
44-
const examplesDir = join(TEMP_DIR, 'examples');
45-
46-
if (!existsSync(examplesDir)) {
67+
if (!existsSync(EXAMPLES_DIR)) {
4768
throw new Error('Examples directory not found');
4869
}
49-
70+
5071
const definedExamples = Object.keys(EXAMPLE_METADATA);
5172
const availableExamples = [];
52-
73+
5374
// Check which defined examples actually exist in the repository
5475
for (const exampleName of definedExamples) {
55-
const examplePath = join(examplesDir, exampleName);
76+
const examplePath = join(EXAMPLES_DIR, exampleName);
5677
if (existsSync(examplePath)) {
5778
availableExamples.push(exampleName);
5879
} else {
5980
throw new Error(`Example defined in metadata but not found in repo: ${exampleName}`);
6081
}
6182
}
62-
83+
6384
console.log(`Processing ${availableExamples.length} examples: ${availableExamples.join(', ')}`);
6485
return availableExamples;
6586
}
6687

6788
// Copy example to temp folder, install dependencies, then process files
6889
function processExample(exampleName) {
69-
const exampleDir = join(TEMP_DIR, 'examples', exampleName);
70-
90+
const exampleDir = join(EXAMPLES_DIR, exampleName);
91+
7192
if (!existsSync(exampleDir)) {
7293
throw new Error(`Example directory not found: ${exampleName}`);
7394
}
@@ -95,14 +116,14 @@ function processExample(exampleName) {
95116
// Run npm install to generate lockfile
96117
console.log(`Running npm install for ${exampleName}...`);
97118
try {
98-
execSync('npm install', {
119+
execSync('npm install', {
99120
cwd: tempExampleDir,
100-
stdio: 'inherit'
121+
stdio: 'inherit'
101122
});
102123
} catch (error) {
103124
throw new Error(`npm install failed for ${exampleName}: ${error.message}`);
104125
}
105-
126+
106127
// Remove node_modules after npm install
107128
console.log(`Removing node_modules for ${exampleName}...`);
108129
const nodeModulesPath = join(tempExampleDir, 'node_modules');
@@ -166,15 +187,15 @@ function processExample(exampleName) {
166187
// Main function
167188
function main() {
168189
console.log('Generating examples...');
169-
170-
// Update the repository
171-
updateRepo();
172-
190+
191+
// Verify examples directory exists
192+
verifyExamplesDir();
193+
173194
// Ensure temp example directory exists
174195
if (!existsSync(TEMP_EXAMPLE_DIR)) {
175196
mkdirSync(TEMP_EXAMPLE_DIR, { recursive: true });
176197
}
177-
198+
178199
// Get examples to process (only those defined in metadata)
179200
const exampleNames = getExamplesToProcess();
180201

0 commit comments

Comments
 (0)