Rename to Grimoired, update domain to grimoi.red, add resource system
- Rename Grimaired -> Grimoired everywhere (title, nav, descriptions, token keys) - Update domain from skills.here.run.place to grimoi.red - Add Grimoired logo with description on homepage - Add accordion behavior for Quick install / Quick push sections - Add generic resource system (skills, agents, output-styles, rules) - Add resource registry, editor, search, and file manager components
This commit is contained in:
committed by
Alejandro Martinez
parent
aa477a553b
commit
17423fb3b9
373
src/lib/sync.ts
373
src/lib/sync.ts
@@ -1,19 +1,30 @@
|
||||
import { listResources, getResource } from './resources';
|
||||
import { listSkills } from './skills';
|
||||
import { type ResourceType, REGISTRY, RESOURCE_TYPES, getTypeConfig } from './registry';
|
||||
|
||||
export function isPowerShell(request: Request): boolean {
|
||||
const ua = request.headers.get('user-agent') || '';
|
||||
return /PowerShell/i.test(ua);
|
||||
}
|
||||
|
||||
// --- Push scripts (type-aware) ---
|
||||
|
||||
export async function buildPushScript(baseUrl: string, skillsDir: string): Promise<string> {
|
||||
return buildPushScriptForType(baseUrl, skillsDir, 'skills');
|
||||
}
|
||||
|
||||
export async function buildPushScriptForType(baseUrl: string, resourceDir: string, type: ResourceType): Promise<string> {
|
||||
const config = getTypeConfig(type);
|
||||
const lines = [
|
||||
'#!/usr/bin/env bash',
|
||||
'set -euo pipefail',
|
||||
'',
|
||||
`SKILLS_DIR="${skillsDir}"`,
|
||||
`RESOURCE_DIR="${resourceDir}"`,
|
||||
`BASE_URL="${baseUrl}"`,
|
||||
`RESOURCE_TYPE="${type}"`,
|
||||
`MAIN_FILE_NAME="${config.mainFileName}"`,
|
||||
'FILTER="${1:-}"',
|
||||
'TOKEN_FILE="$HOME/.claude/skills.here-token"',
|
||||
'TOKEN_FILE="$HOME/.claude/grimoired-token"',
|
||||
'',
|
||||
'# Get git author if available',
|
||||
'AUTHOR_NAME=$(git config user.name 2>/dev/null || echo "")',
|
||||
@@ -40,15 +51,15 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' echo " Token saved to $TOKEN_FILE"',
|
||||
' elif [ "$REGISTER_STATUS" = "409" ]; then',
|
||||
' echo " Email already registered. Place your token in $TOKEN_FILE"',
|
||||
' echo " Continuing without token (unprotected skills only)..."',
|
||||
' echo " Continuing without token (unprotected resources only)..."',
|
||||
' else',
|
||||
' echo " Registration failed ($REGISTER_STATUS): $REGISTER_BODY"',
|
||||
' echo " Continuing without token (unprotected skills only)..."',
|
||||
' echo " Continuing without token (unprotected resources only)..."',
|
||||
' fi',
|
||||
'fi',
|
||||
'',
|
||||
'if [ ! -d "$SKILLS_DIR" ]; then',
|
||||
' echo "No skills directory found at $SKILLS_DIR"',
|
||||
'if [ ! -d "$RESOURCE_DIR" ]; then',
|
||||
' echo "No directory found at $RESOURCE_DIR"',
|
||||
' exit 1',
|
||||
'fi',
|
||||
'',
|
||||
@@ -57,7 +68,7 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' AUTH_HEADER="Authorization: Bearer $TOKEN"',
|
||||
'fi',
|
||||
'',
|
||||
'push_skill() {',
|
||||
'push_file_resource() {',
|
||||
' local file="$1"',
|
||||
' local slug=$(basename "$file" .md)',
|
||||
' local content=$(cat "$file")',
|
||||
@@ -67,12 +78,6 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' content=$(echo "$content" | awk -v name="$AUTHOR_NAME" -v email="$AUTHOR_EMAIL" \'NR==1 && /^---$/{print; if (name) print "author: " name; print "author-email: " email; next} {print}\')',
|
||||
' fi',
|
||||
'',
|
||||
' # Build curl auth args',
|
||||
' local auth_args=""',
|
||||
' if [ -n "$AUTH_HEADER" ]; then',
|
||||
' auth_args="-H \\"$AUTH_HEADER\\""',
|
||||
' fi',
|
||||
'',
|
||||
' # Try PUT (update), fallback to POST (create)',
|
||||
' local response',
|
||||
' if [ -n "$TOKEN" ]; then',
|
||||
@@ -80,12 +85,12 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -H "Authorization: Bearer $TOKEN" \\',
|
||||
' -d "{\\"content\\": $(echo "$content" | jq -Rs .)}" \\',
|
||||
' "$BASE_URL/api/skills/$slug")',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE/$slug")',
|
||||
' else',
|
||||
' response=$(curl -sS -o /dev/null -w "%{http_code}" -X PUT \\',
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -d "{\\"content\\": $(echo "$content" | jq -Rs .)}" \\',
|
||||
' "$BASE_URL/api/skills/$slug")',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE/$slug")',
|
||||
' fi',
|
||||
'',
|
||||
' if [ "$response" = "403" ]; then',
|
||||
@@ -99,12 +104,12 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -H "Authorization: Bearer $TOKEN" \\',
|
||||
' -d "{\\"slug\\": \\"$slug\\", \\"content\\": $(echo "$content" | jq -Rs .)}" \\',
|
||||
' "$BASE_URL/api/skills")',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE")',
|
||||
' else',
|
||||
' local post_status=$(curl -sS -o /dev/null -w "%{http_code}" -X POST \\',
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -d "{\\"slug\\": \\"$slug\\", \\"content\\": $(echo "$content" | jq -Rs .)}" \\',
|
||||
' "$BASE_URL/api/skills")',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE")',
|
||||
' fi',
|
||||
' if [ "$post_status" = "403" ]; then',
|
||||
' echo " ✗ $slug (permission denied — token missing or invalid)"',
|
||||
@@ -115,25 +120,120 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' echo " ✓ $slug"',
|
||||
'}',
|
||||
'',
|
||||
'push_folder_resource() {',
|
||||
' local dir="$1"',
|
||||
' local slug=$(basename "$dir")',
|
||||
' local main_file="$dir/$MAIN_FILE_NAME"',
|
||||
'',
|
||||
' if [ ! -f "$main_file" ]; then',
|
||||
' echo " ✗ $slug (no $MAIN_FILE_NAME found)"',
|
||||
' return 1',
|
||||
' fi',
|
||||
'',
|
||||
' local content=$(cat "$main_file")',
|
||||
'',
|
||||
' # Inject author',
|
||||
' if [ -n "$AUTHOR_EMAIL" ] && ! echo "$content" | grep -q "^author-email:"; then',
|
||||
' content=$(echo "$content" | awk -v name="$AUTHOR_NAME" -v email="$AUTHOR_EMAIL" \'NR==1 && /^---$/{print; if (name) print "author: " name; print "author-email: " email; next} {print}\')',
|
||||
' fi',
|
||||
'',
|
||||
' # Create as folder format, try PUT first then POST',
|
||||
' local response',
|
||||
' if [ -n "$TOKEN" ]; then',
|
||||
' response=$(curl -sS -o /dev/null -w "%{http_code}" -X PUT \\',
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -H "Authorization: Bearer $TOKEN" \\',
|
||||
' -d "{\\"content\\": $(echo "$content" | jq -Rs .)}" \\',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE/$slug")',
|
||||
' else',
|
||||
' response=$(curl -sS -o /dev/null -w "%{http_code}" -X PUT \\',
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -d "{\\"content\\": $(echo "$content" | jq -Rs .)}" \\',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE/$slug")',
|
||||
' fi',
|
||||
'',
|
||||
' if [ "$response" = "403" ]; then',
|
||||
' echo " ✗ $slug (permission denied)"',
|
||||
' return 1',
|
||||
' fi',
|
||||
'',
|
||||
' if [ "$response" = "404" ]; then',
|
||||
' if [ -n "$TOKEN" ]; then',
|
||||
' curl -sS -o /dev/null -w "%{http_code}" -X POST \\',
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -H "Authorization: Bearer $TOKEN" \\',
|
||||
' -d "{\\"slug\\": \\"$slug\\", \\"content\\": $(echo "$content" | jq -Rs .), \\"format\\": \\"folder\\"}" \\',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE" > /dev/null',
|
||||
' else',
|
||||
' curl -sS -o /dev/null -w "%{http_code}" -X POST \\',
|
||||
' -H "Content-Type: application/json" \\',
|
||||
' -d "{\\"slug\\": \\"$slug\\", \\"content\\": $(echo "$content" | jq -Rs .), \\"format\\": \\"folder\\"}" \\',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE" > /dev/null',
|
||||
' fi',
|
||||
' fi',
|
||||
'',
|
||||
' # Upload sub-files',
|
||||
' local file_count=0',
|
||||
' for subdir in scripts references assets; do',
|
||||
' if [ -d "$dir/$subdir" ]; then',
|
||||
' find "$dir/$subdir" -type f | while read -r subfile; do',
|
||||
' local rel_path="${subfile#$dir/}"',
|
||||
' local auth_flag=""',
|
||||
' if [ -n "$TOKEN" ]; then',
|
||||
' auth_flag="-H \\"Authorization: Bearer $TOKEN\\""',
|
||||
' fi',
|
||||
' if [ -n "$TOKEN" ]; then',
|
||||
' curl -sS -X PUT \\',
|
||||
' -H "Authorization: Bearer $TOKEN" \\',
|
||||
' --data-binary "@$subfile" \\',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE/$slug/files/$rel_path" > /dev/null',
|
||||
' else',
|
||||
' curl -sS -X PUT \\',
|
||||
' --data-binary "@$subfile" \\',
|
||||
' "$BASE_URL/api/resources/$RESOURCE_TYPE/$slug/files/$rel_path" > /dev/null',
|
||||
' fi',
|
||||
' done',
|
||||
' fi',
|
||||
' done',
|
||||
'',
|
||||
' echo " ✓ $slug (folder)"',
|
||||
'}',
|
||||
'',
|
||||
'count=0',
|
||||
'failed=0',
|
||||
'if [ -n "$FILTER" ]; then',
|
||||
' # Push a specific skill',
|
||||
' file="$SKILLS_DIR/${FILTER%.md}.md"',
|
||||
' if [ ! -f "$file" ]; then',
|
||||
' echo "Skill not found: $file"',
|
||||
' # Check if filter matches a directory (folder resource)',
|
||||
' dir_path="$RESOURCE_DIR/${FILTER%.md}"',
|
||||
' dir_path="${dir_path%.md}"',
|
||||
' file_path="$RESOURCE_DIR/${FILTER%.md}.md"',
|
||||
' if [ -d "$dir_path" ] && [ -f "$dir_path/$MAIN_FILE_NAME" ]; then',
|
||||
' if push_folder_resource "$dir_path"; then count=1; else failed=1; fi',
|
||||
' elif [ -f "$file_path" ]; then',
|
||||
' if push_file_resource "$file_path"; then count=1; else failed=1; fi',
|
||||
' else',
|
||||
' echo "Resource not found: $FILTER"',
|
||||
' exit 1',
|
||||
' fi',
|
||||
' if push_skill "$file"; then',
|
||||
' count=1',
|
||||
' else',
|
||||
' failed=1',
|
||||
' fi',
|
||||
'else',
|
||||
' # Push all skills',
|
||||
' for file in "$SKILLS_DIR"/*.md; do',
|
||||
' # Push all .md files (simple format)',
|
||||
' for file in "$RESOURCE_DIR"/*.md; do',
|
||||
' [ -f "$file" ] || continue',
|
||||
' if push_skill "$file"; then',
|
||||
' slug_name=$(basename "$file" .md)',
|
||||
' # Skip if folder version exists',
|
||||
' if [ -d "$RESOURCE_DIR/$slug_name" ] && [ -f "$RESOURCE_DIR/$slug_name/$MAIN_FILE_NAME" ]; then',
|
||||
' continue',
|
||||
' fi',
|
||||
' if push_file_resource "$file"; then',
|
||||
' count=$((count + 1))',
|
||||
' else',
|
||||
' failed=$((failed + 1))',
|
||||
' fi',
|
||||
' done',
|
||||
' # Push all directories (folder format)',
|
||||
' for dir in "$RESOURCE_DIR"/*/; do',
|
||||
' [ -d "$dir" ] || continue',
|
||||
' [ -f "$dir/$MAIN_FILE_NAME" ] || continue',
|
||||
' if push_folder_resource "$dir"; then',
|
||||
' count=$((count + 1))',
|
||||
' else',
|
||||
' failed=$((failed + 1))',
|
||||
@@ -141,9 +241,9 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
' done',
|
||||
'fi',
|
||||
'',
|
||||
'echo "Pushed $count skill(s) to $BASE_URL"',
|
||||
`echo "Pushed $count resource(s) to $BASE_URL"`,
|
||||
'if [ "$failed" -gt 0 ]; then',
|
||||
' echo "$failed skill(s) failed (permission denied)"',
|
||||
' echo "$failed resource(s) failed (permission denied)"',
|
||||
'fi',
|
||||
'',
|
||||
];
|
||||
@@ -151,63 +251,120 @@ export async function buildPushScript(baseUrl: string, skillsDir: string): Promi
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// --- Sync scripts (type-aware) ---
|
||||
|
||||
export async function buildSyncScript(baseUrl: string, skillsDir: string): Promise<string> {
|
||||
const skills = await listSkills();
|
||||
return buildSyncScriptForType(baseUrl, skillsDir, 'skills');
|
||||
}
|
||||
|
||||
export async function buildSyncScriptForType(baseUrl: string, targetDir: string, type: ResourceType): Promise<string> {
|
||||
const resources = await listResources(type);
|
||||
const config = getTypeConfig(type);
|
||||
|
||||
const lines = [
|
||||
'#!/usr/bin/env bash',
|
||||
'set -euo pipefail',
|
||||
'',
|
||||
`SKILLS_DIR="${skillsDir}"`,
|
||||
'mkdir -p "$SKILLS_DIR"',
|
||||
`TARGET_DIR="${targetDir}"`,
|
||||
'mkdir -p "$TARGET_DIR"',
|
||||
'',
|
||||
];
|
||||
|
||||
if (skills.length === 0) {
|
||||
lines.push('echo "No skills available to sync."');
|
||||
if (resources.length === 0) {
|
||||
lines.push(`echo "No ${type} available to sync."`);
|
||||
} else {
|
||||
lines.push(`echo "Syncing ${skills.length} skill(s) from ${baseUrl}..."`);
|
||||
lines.push(`echo "Syncing ${resources.length} ${type} from ${baseUrl}..."`);
|
||||
lines.push('');
|
||||
|
||||
for (const skill of skills) {
|
||||
const skillUrl = `${baseUrl}/${skill.slug}`;
|
||||
lines.push(`curl -fsSL "${skillUrl}" -o "$SKILLS_DIR/${skill.slug}.md"`);
|
||||
lines.push(`echo " ✓ ${skill.name}"`);
|
||||
for (const r of resources) {
|
||||
if (r.format === 'folder') {
|
||||
// Fetch full resource to get file list
|
||||
const full = await getResource(type, r.slug);
|
||||
if (!full) continue;
|
||||
|
||||
lines.push(`mkdir -p "$TARGET_DIR/${r.slug}"`);
|
||||
lines.push(`curl -fsSL "${baseUrl}/${type}/${r.slug}" -o "$TARGET_DIR/${r.slug}/${config.mainFileName}"`);
|
||||
|
||||
for (const f of full.files) {
|
||||
const dir = f.relativePath.split('/').slice(0, -1).join('/');
|
||||
if (dir) {
|
||||
lines.push(`mkdir -p "$TARGET_DIR/${r.slug}/${dir}"`);
|
||||
}
|
||||
lines.push(`curl -fsSL "${baseUrl}/api/resources/${type}/${r.slug}/files/${f.relativePath}" -o "$TARGET_DIR/${r.slug}/${f.relativePath}"`);
|
||||
}
|
||||
|
||||
const scriptFiles = full.files.filter(f => f.relativePath.startsWith('scripts/'));
|
||||
for (const f of scriptFiles) {
|
||||
lines.push(`chmod +x "$TARGET_DIR/${r.slug}/${f.relativePath}"`);
|
||||
}
|
||||
|
||||
lines.push(`echo " ✓ ${r.name} (folder, ${full.files.length + 1} files)"`);
|
||||
} else {
|
||||
const resourceUrl = `${baseUrl}/${type}/${r.slug}`;
|
||||
lines.push(`curl -fsSL "${resourceUrl}" -o "$TARGET_DIR/${r.slug}.md"`);
|
||||
lines.push(`echo " ✓ ${r.name}"`);
|
||||
}
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
lines.push('echo "Done! Skills synced to $SKILLS_DIR"');
|
||||
lines.push('echo "Done! Synced to $TARGET_DIR"');
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// --- PowerShell variants ---
|
||||
|
||||
export async function buildSyncScriptPS(baseUrl: string, skillsDir: string): Promise<string> {
|
||||
const skills = await listSkills();
|
||||
return buildSyncScriptPSForType(baseUrl, skillsDir, 'skills');
|
||||
}
|
||||
|
||||
export async function buildSyncScriptPSForType(baseUrl: string, targetDir: string, type: ResourceType): Promise<string> {
|
||||
const resources = await listResources(type);
|
||||
const config = getTypeConfig(type);
|
||||
|
||||
const lines = [
|
||||
'$ErrorActionPreference = "Stop"',
|
||||
'',
|
||||
`$SkillsDir = "${skillsDir}"`,
|
||||
'New-Item -ItemType Directory -Force -Path $SkillsDir | Out-Null',
|
||||
`$TargetDir = "${targetDir}"`,
|
||||
'New-Item -ItemType Directory -Force -Path $TargetDir | Out-Null',
|
||||
'',
|
||||
];
|
||||
|
||||
if (skills.length === 0) {
|
||||
lines.push('Write-Host "No skills available to sync."');
|
||||
if (resources.length === 0) {
|
||||
lines.push(`Write-Host "No ${type} available to sync."`);
|
||||
} else {
|
||||
lines.push(`Write-Host "Syncing ${skills.length} skill(s) from ${baseUrl}..."`);
|
||||
lines.push(`Write-Host "Syncing ${resources.length} ${type} from ${baseUrl}..."`);
|
||||
lines.push('');
|
||||
|
||||
for (const skill of skills) {
|
||||
const skillUrl = `${baseUrl}/${skill.slug}`;
|
||||
lines.push(`Invoke-WebRequest -Uri "${skillUrl}" -OutFile (Join-Path $SkillsDir "${skill.slug}.md")`);
|
||||
lines.push(`Write-Host " ✓ ${skill.name}"`);
|
||||
for (const r of resources) {
|
||||
if (r.format === 'folder') {
|
||||
const full = await getResource(type, r.slug);
|
||||
if (!full) continue;
|
||||
|
||||
lines.push(`New-Item -ItemType Directory -Force -Path (Join-Path $TargetDir "${r.slug}") | Out-Null`);
|
||||
lines.push(`Invoke-WebRequest -Uri "${baseUrl}/${type}/${r.slug}" -OutFile (Join-Path $TargetDir "${r.slug}\\${config.mainFileName}")`);
|
||||
|
||||
for (const f of full.files) {
|
||||
const dir = f.relativePath.split('/').slice(0, -1).join('\\');
|
||||
if (dir) {
|
||||
lines.push(`New-Item -ItemType Directory -Force -Path (Join-Path $TargetDir "${r.slug}\\${dir}") | Out-Null`);
|
||||
}
|
||||
const winPath = f.relativePath.replace(/\//g, '\\');
|
||||
lines.push(`Invoke-WebRequest -Uri "${baseUrl}/api/resources/${type}/${r.slug}/files/${f.relativePath}" -OutFile (Join-Path $TargetDir "${r.slug}\\${winPath}")`);
|
||||
}
|
||||
|
||||
lines.push(`Write-Host " ✓ ${r.name} (folder, ${full.files.length + 1} files)"`);
|
||||
} else {
|
||||
const resourceUrl = `${baseUrl}/${type}/${r.slug}`;
|
||||
lines.push(`Invoke-WebRequest -Uri "${resourceUrl}" -OutFile (Join-Path $TargetDir "${r.slug}.md")`);
|
||||
lines.push(`Write-Host " ✓ ${r.name}"`);
|
||||
}
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
lines.push('Write-Host "Done! Skills synced to $SkillsDir"');
|
||||
lines.push('Write-Host "Done! Synced to $TargetDir"');
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
@@ -215,13 +372,20 @@ export async function buildSyncScriptPS(baseUrl: string, skillsDir: string): Pro
|
||||
}
|
||||
|
||||
export async function buildPushScriptPS(baseUrl: string, skillsDir: string): Promise<string> {
|
||||
return buildPushScriptPSForType(baseUrl, skillsDir, 'skills');
|
||||
}
|
||||
|
||||
export async function buildPushScriptPSForType(baseUrl: string, resourceDir: string, type: ResourceType): Promise<string> {
|
||||
const config = getTypeConfig(type);
|
||||
const lines = [
|
||||
'$ErrorActionPreference = "Stop"',
|
||||
'',
|
||||
`$SkillsDir = "${skillsDir}"`,
|
||||
`$ResourceDir = "${resourceDir}"`,
|
||||
`$BaseUrl = "${baseUrl}"`,
|
||||
`$ResourceType = "${type}"`,
|
||||
`$MainFileName = "${config.mainFileName}"`,
|
||||
'$Filter = if ($args.Count -gt 0) { $args[0] } else { "" }',
|
||||
'$TokenFile = Join-Path $HOME ".claude\\skills.here-token"',
|
||||
'$TokenFile = Join-Path $HOME ".claude\\grimoired-token"',
|
||||
'',
|
||||
'# Get git author if available',
|
||||
'$AuthorName = try { git config user.name 2>$null } catch { "" }',
|
||||
@@ -249,19 +413,19 @@ export async function buildPushScriptPS(baseUrl: string, skillsDir: string): Pro
|
||||
' } else {',
|
||||
' Write-Host " Registration failed: $_"',
|
||||
' }',
|
||||
' Write-Host " Continuing without token (unprotected skills only)..."',
|
||||
' Write-Host " Continuing without token (unprotected resources only)..."',
|
||||
' }',
|
||||
'}',
|
||||
'',
|
||||
'if (-not (Test-Path $SkillsDir)) {',
|
||||
' Write-Host "No skills directory found at $SkillsDir"',
|
||||
'if (-not (Test-Path $ResourceDir)) {',
|
||||
' Write-Host "No directory found at $ResourceDir"',
|
||||
' exit 1',
|
||||
'}',
|
||||
'',
|
||||
'$headers = @{ "Content-Type" = "application/json" }',
|
||||
'if ($Token) { $headers["Authorization"] = "Bearer $Token" }',
|
||||
'',
|
||||
'function Push-Skill($file) {',
|
||||
'function Push-FileResource($file) {',
|
||||
' $slug = [IO.Path]::GetFileNameWithoutExtension($file)',
|
||||
' $content = Get-Content $file -Raw',
|
||||
'',
|
||||
@@ -275,7 +439,7 @@ export async function buildPushScriptPS(baseUrl: string, skillsDir: string): Pro
|
||||
'',
|
||||
' $body = @{ content = $content } | ConvertTo-Json',
|
||||
' try {',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/skills/$slug" -Method PUT -Headers $headers -Body $body | Out-Null',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/resources/$ResourceType/$slug" -Method PUT -Headers $headers -Body $body | Out-Null',
|
||||
' Write-Host " ✓ $slug"',
|
||||
' return $true',
|
||||
' } catch {',
|
||||
@@ -287,7 +451,7 @@ export async function buildPushScriptPS(baseUrl: string, skillsDir: string): Pro
|
||||
' if ($code -eq 404) {',
|
||||
' $postBody = @{ slug = $slug; content = $content } | ConvertTo-Json',
|
||||
' try {',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/skills" -Method POST -Headers $headers -Body $postBody | Out-Null',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/resources/$ResourceType" -Method POST -Headers $headers -Body $postBody | Out-Null',
|
||||
' Write-Host " ✓ $slug"',
|
||||
' return $true',
|
||||
' } catch {',
|
||||
@@ -303,19 +467,92 @@ export async function buildPushScriptPS(baseUrl: string, skillsDir: string): Pro
|
||||
' return $true',
|
||||
'}',
|
||||
'',
|
||||
'function Push-FolderResource($dir) {',
|
||||
' $slug = Split-Path $dir -Leaf',
|
||||
' $mainFile = Join-Path $dir $MainFileName',
|
||||
' if (-not (Test-Path $mainFile)) {',
|
||||
' Write-Host " ✗ $slug (no $MainFileName found)"',
|
||||
' return $false',
|
||||
' }',
|
||||
' $content = Get-Content $mainFile -Raw',
|
||||
'',
|
||||
' if ($AuthorEmail -and $content -notmatch "(?m)^author-email:") {',
|
||||
' $inject = ""',
|
||||
' if ($AuthorName) { $inject += "author: $AuthorName`n" }',
|
||||
' $inject += "author-email: $AuthorEmail"',
|
||||
' $content = $content -replace "^---$", "---`n$inject" -replace "^---`n", "---`n"',
|
||||
' }',
|
||||
'',
|
||||
' $body = @{ content = $content } | ConvertTo-Json',
|
||||
' try {',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/resources/$ResourceType/$slug" -Method PUT -Headers $headers -Body $body | Out-Null',
|
||||
' } catch {',
|
||||
' $code = $_.Exception.Response.StatusCode.value__',
|
||||
' if ($code -eq 403) {',
|
||||
' Write-Host " ✗ $slug (permission denied)"',
|
||||
' return $false',
|
||||
' }',
|
||||
' if ($code -eq 404) {',
|
||||
' $postBody = @{ slug = $slug; content = $content; format = "folder" } | ConvertTo-Json',
|
||||
' try {',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/resources/$ResourceType" -Method POST -Headers $headers -Body $postBody | Out-Null',
|
||||
' } catch {',
|
||||
' $postCode = $_.Exception.Response.StatusCode.value__',
|
||||
' if ($postCode -eq 403) {',
|
||||
' Write-Host " ✗ $slug (permission denied)"',
|
||||
' return $false',
|
||||
' }',
|
||||
' }',
|
||||
' }',
|
||||
' }',
|
||||
'',
|
||||
' # Upload sub-files',
|
||||
' foreach ($subdir in @("scripts", "references", "assets")) {',
|
||||
' $subdirPath = Join-Path $dir $subdir',
|
||||
' if (Test-Path $subdirPath) {',
|
||||
' Get-ChildItem -Path $subdirPath -Recurse -File | ForEach-Object {',
|
||||
' $relPath = $_.FullName.Substring($dir.Length + 1).Replace("\\", "/")',
|
||||
' $fileHeaders = @{}',
|
||||
' if ($Token) { $fileHeaders["Authorization"] = "Bearer $Token" }',
|
||||
' $fileBytes = [IO.File]::ReadAllBytes($_.FullName)',
|
||||
' Invoke-WebRequest -Uri "$BaseUrl/api/resources/$ResourceType/$slug/files/$relPath" -Method PUT -Headers $fileHeaders -Body $fileBytes | Out-Null',
|
||||
' }',
|
||||
' }',
|
||||
' }',
|
||||
'',
|
||||
' Write-Host " ✓ $slug (folder)"',
|
||||
' return $true',
|
||||
'}',
|
||||
'',
|
||||
'$count = 0; $failed = 0',
|
||||
'if ($Filter) {',
|
||||
' $file = Join-Path $SkillsDir "$($Filter -replace \'\\.md$\',\'\').md"',
|
||||
' if (-not (Test-Path $file)) { Write-Host "Skill not found: $file"; exit 1 }',
|
||||
' if (Push-Skill $file) { $count++ } else { $failed++ }',
|
||||
' $dirPath = Join-Path $ResourceDir ($Filter -replace "\\.md$","")',
|
||||
' $filePath = Join-Path $ResourceDir "$($Filter -replace \'\\.md$\',\'\').md"',
|
||||
' if ((Test-Path $dirPath -PathType Container) -and (Test-Path (Join-Path $dirPath $MainFileName))) {',
|
||||
' if (Push-FolderResource $dirPath) { $count++ } else { $failed++ }',
|
||||
' } elseif (Test-Path $filePath) {',
|
||||
' if (Push-FileResource $filePath) { $count++ } else { $failed++ }',
|
||||
' } else {',
|
||||
' Write-Host "Resource not found: $Filter"; exit 1',
|
||||
' }',
|
||||
'} else {',
|
||||
' Get-ChildItem -Path $SkillsDir -Filter "*.md" | ForEach-Object {',
|
||||
' if (Push-Skill $_.FullName) { $count++ } else { $failed++ }',
|
||||
' # Push .md files (skip if folder version exists)',
|
||||
' Get-ChildItem -Path $ResourceDir -Filter "*.md" | ForEach-Object {',
|
||||
' $slugName = $_.BaseName',
|
||||
' $folderPath = Join-Path $ResourceDir $slugName',
|
||||
' if ((Test-Path $folderPath -PathType Container) -and (Test-Path (Join-Path $folderPath $MainFileName))) { return }',
|
||||
' if (Push-FileResource $_.FullName) { $count++ } else { $failed++ }',
|
||||
' }',
|
||||
' # Push directories',
|
||||
' Get-ChildItem -Path $ResourceDir -Directory | ForEach-Object {',
|
||||
' if (Test-Path (Join-Path $_.FullName $MainFileName)) {',
|
||||
' if (Push-FolderResource $_.FullName) { $count++ } else { $failed++ }',
|
||||
' }',
|
||||
' }',
|
||||
'}',
|
||||
'',
|
||||
'Write-Host "Pushed $count skill(s) to $BaseUrl"',
|
||||
'if ($failed -gt 0) { Write-Host "$failed skill(s) failed (permission denied)" }',
|
||||
'Write-Host "Pushed $count resource(s) to $BaseUrl"',
|
||||
'if ($failed -gt 0) { Write-Host "$failed resource(s) failed (permission denied)" }',
|
||||
'',
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user