-
Notifications
You must be signed in to change notification settings - Fork 2
fix: harden plugin loading and add pack test #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env bash | ||
| #MISE description="Install packed plugin into OpenCode cache (production-like test)" | ||
| set -euo pipefail | ||
|
|
||
| PLUGIN_LOCAL_FILE="${HOME}/.config/opencode/plugin/opencode-synced-local.ts" | ||
| CACHE_DIR="${HOME}/.cache/opencode" | ||
| PACKAGE_DIR="$(pwd)" | ||
| VERSION="$(node -p "require('./package.json').version")" | ||
|
|
||
| if [ -f "${PLUGIN_LOCAL_FILE}" ]; then | ||
| rm -f "${PLUGIN_LOCAL_FILE}" | ||
| fi | ||
|
|
||
| TARBALL="$(npm pack | tail -n 1)" | ||
|
|
||
| rm -rf "${CACHE_DIR}/node_modules/opencode-synced" | ||
|
|
||
| if [ -f "${CACHE_DIR}/package.json" ]; then | ||
| python - <<PY | ||
| import json, re | ||
| from pathlib import Path | ||
| version = "${VERSION}" | ||
| path = Path("${CACHE_DIR}") / "package.json" | ||
| text = path.read_text() | ||
| deps = dict(re.findall(r'\"([^\\\"]+)\"\\s*:\\s*\"([^\\\"]+)\"', text)) | ||
| deps["opencode-synced"] = version | ||
| path.write_text(json.dumps({"dependencies": deps}, indent=2) + "\\n") | ||
| PY | ||
| fi | ||
|
|
||
| (cd "${CACHE_DIR}" && npm install --no-save "${PACKAGE_DIR}/${TARBALL}") | ||
|
|
||
| echo "Installed ${TARBALL} (${VERSION}) into ${CACHE_DIR}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| ".": "0.4.1" | ||
| ".": "0.4.2" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,18 +81,29 @@ async function loadCommands(): Promise<ParsedCommand[]> { | |
| const commands: ParsedCommand[] = []; | ||
| const commandDir = path.join(getModuleDir(), 'command'); | ||
|
|
||
| try { | ||
| const stats = await fs.stat(commandDir); | ||
| if (!stats.isDirectory()) { | ||
| return commands; | ||
| } | ||
| } catch { | ||
| return commands; | ||
| } | ||
|
|
||
| const files = await scanMdFiles(commandDir); | ||
| for (const file of files) { | ||
| const content = await fs.readFile(file, 'utf-8'); | ||
| const { frontmatter, body } = parseFrontmatter(content); | ||
| const relativePath = path.relative(commandDir, file); | ||
| const name = relativePath.replace(/\.md$/, '').replace(/\//g, '-'); | ||
|
|
||
| commands.push({ | ||
| name, | ||
| frontmatter, | ||
| template: body, | ||
| }); | ||
| try { | ||
| const content = await fs.readFile(file, 'utf-8'); | ||
| const { frontmatter, body } = parseFrontmatter(content); | ||
| const relativePath = path.relative(commandDir, file); | ||
| const name = relativePath.replace(/\.md$/, '').replace(/\//g, '-'); | ||
|
|
||
| commands.push({ | ||
| name, | ||
| frontmatter, | ||
| template: body, | ||
| }); | ||
| } catch {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swallowing errors with an empty } catch (error) {
console.error(`[opencode-synced] Failed to load command from ${file}:`, error);
} |
||
| } | ||
|
|
||
| return commands; | ||
|
|
@@ -210,6 +221,9 @@ export const OpencodeConfigSync: Plugin = async (ctx) => { | |
| }; | ||
| }; | ||
|
|
||
| export const OpencodeSynced = OpencodeConfigSync; | ||
| export default OpencodeConfigSync; | ||
|
|
||
| function formatError(error: unknown): string { | ||
| if (error instanceof Error) return error.message; | ||
| return String(error); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using regular expressions to parse and modify a JSON file is brittle and can lead to unexpected behavior. This implementation overwrites the entire
package.jsonwith only thedependenciesfield, which would remove other important fields likename,version, orscripts. A much safer and more robust approach is to use Python's built-injsonmodule to load, modify, and then write back the JSON data. This preserves the original structure of the file while updating the necessary dependency.