Skip to content

Commit

Permalink
Merge pull request #48 from e2b-dev/generate-api-reference-for-code-i…
Browse files Browse the repository at this point in the history
…nterpreter-sdk-e2b-1235

Generate SDK reference
  • Loading branch information
0div authored Nov 27, 2024
2 parents 8ea8003 + a154d0a commit c74f82a
Show file tree
Hide file tree
Showing 19 changed files with 3,721 additions and 45 deletions.
11 changes: 11 additions & 0 deletions .github/scripts/is_new_sdk_ref.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -euo pipefail

# This script checks for diffs in the js/ and python/ directory.
# If there are diffs, it means we need to generate new SDK references.
if git diff --name-only HEAD^ | grep -q '^js/\|^python/'; then
echo "true"
else
echo "false"
fi
8 changes: 8 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ jobs:
run: pnpm run version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Generate SDK reference
id: sdk-ref
run: pnpm run --recursive generate-ref

- name: Show docs file structure
run: tree ./sdk-reference

- name: Release new versions
uses: changesets/action@v1
Expand All @@ -314,6 +321,7 @@ jobs:
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add ./sdk-reference
git commit -am "[skip ci] Release new versions" || exit 0
git push
env:
Expand Down
6 changes: 5 additions & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@e2b/code-interpreter",
"version": "1.0.4",
"packageManager": "pnpm@8.7.6",
"description": "E2B Code Interpreter - Stateful code execution",
"homepage": "https://e2b.dev",
"license": "MIT",
Expand Down Expand Up @@ -32,7 +33,8 @@
"update-deps": "ncu -u && pnpm i",
"example": "npx tsx example.mts",
"test:bun": "bun test tests/runtimes/bun --env-file=.env",
"test:deno": "deno test tests/runtimes/deno/ --allow-net --allow-read --allow-env --unstable-sloppy-imports --trace-leaks"
"test:deno": "deno test tests/runtimes/deno/ --allow-net --allow-read --allow-env --unstable-sloppy-imports --trace-leaks",
"generate-ref": "./scripts/generate_sdk_ref.sh"
},
"devDependencies": {
"@types/node": "^18.18.6",
Expand All @@ -41,6 +43,8 @@
"knip": "^5.25.1",
"npm-check-updates": "^16.14.20",
"tsup": "^8.1.0",
"typedoc": "^0.26.8",
"typedoc-plugin-markdown": "^4.2.7",
"typescript": "^5.5.3",
"vitest": "^2.0.1"
},
Expand Down
64 changes: 64 additions & 0 deletions js/scripts/CustomMarkdownTheme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { MarkdownTheme, MarkdownPageEvent } = require('typedoc-plugin-markdown')

function load(app) {
// Listen to the render event
app.renderer.on(MarkdownPageEvent.END, (page) => {
// Remove Markdown links from the document contents
page.contents = removeMarkdownLinks(
removeFirstNLines(
convertH5toH3(removeLinesWithConditions(page.contents)),
6
)
)
})
}

// this is a hacky way to make methods in the js-sdk sdk reference look more prominent
function convertH5toH3(text) {
return text.replace(/^##### (.*)$/gm, '### $1')
}

// Function to remove Markdown-style links
function removeMarkdownLinks(text) {
// Regular expression to match Markdown links [text](url)
return text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1') // Replace with just the link text
}

function removeFirstNLines(text, n, condition) {
// Split the text into lines, then join back excluding the first four lines
return text.split('\n').slice(n).join('\n')
}

// Function to remove lines based on conditions
function removeLinesWithConditions(text) {
const lines = text.split('\n')
const filteredLines = []

for (let i = 0; i < lines.length; i++) {
// Check if the current line starts with "#### Extends" or "###### Overrides"
if (
lines[i].startsWith('#### Extends') ||
lines[i].startsWith('###### Overrides') ||
lines[i].startsWith('###### Inherited from')
) {
// If it does, skip this line and the next three lines
i += 3 // Skip this line and the next three
continue
}

if (lines[i].startsWith('##### new')) {
// avoid promoting constructors
i += 1
continue
}

// If not removed, add the line to filteredLines
filteredLines.push(convertH5toH3(lines[i]))
}

// Join the filtered lines back into a single string
return filteredLines.join('\n')
}

// Export the load function
module.exports = { load }
35 changes: 35 additions & 0 deletions js/scripts/generate_sdk_ref.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

set -euo pipefail

# This script generates the Code Interpreter JS SDK reference markdown files
# Run it in the `js/` directory

# generate raw SDK reference markdown files
npx typedoc

PKG_VERSION="v$(node -p "require('./package.json').version")"
ROUTES_DIR="../sdk-reference/code-interpreter-js-sdk/${PKG_VERSION}"
mkdir -p "${ROUTES_DIR}"

rm -rf sdk_ref/README.md

# Flatten the sdk_ref directory by moving all nested files to the root level and remove empty subdirectories
find sdk_ref -mindepth 2 -type f | while read -r file; do
mv "$file" sdk_ref/
done
find sdk_ref -type d -empty -delete

# Transfrom top level MD files into folders of the same name with page.mdx inside
find sdk_ref -maxdepth 1 -type f -name "*.md" | while read -r file; do
# Extract the filename without extension
filename=$(basename "$file" .md)
# Create the directory of the same name in sdk_ref
mkdir -p "sdk_ref/${filename}"
# Move the file inside the newly created directory
mv "$file" "sdk_ref/${filename}/page.mdx"
done

cp -r sdk_ref/* "${ROUTES_DIR}"

rm -rf sdk_ref
31 changes: 31 additions & 0 deletions js/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"out": "sdk_ref",
"plugin": ["typedoc-plugin-markdown", "./scripts/CustomMarkdownTheme.js"],
"exclude": ["**/*.spec.ts"],
"entryPoints": [
"src/index.ts",
"src/charts.ts",
"src/consts.ts",
"src/messaging.ts",
"src/sandbox.ts"
],
"excludeExternals": true,
"excludePrivate": true,
"excludeProtected": true,
"navigation": {
"includeGroups": false,
"includeCategories": false
},
"outputFileStrategy": "modules",
"readme": "none",
"disableSources": true,
// typedoc-plugin-markdown options
"classPropertiesFormat": "table",
"typeDeclarationFormat": "table",
"enumMembersFormat": "table",
"parametersFormat": "table",
"expandParameters": true,
"useCodeBlocks": true,
"hidePageTitle": true,
"hideBreadcrumbs": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"changeset": "^0.2.6"
},
"engines": {
"pnpm": ">=9.0.0 <10"
"pnpm": ">=9.0.0 <10"
}
}
Loading

0 comments on commit c74f82a

Please sign in to comment.