-
-
Notifications
You must be signed in to change notification settings - Fork 433
feat: link install scripts to relevant file in code tab #975
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
3549718
fa4122b
244ae26
db689ea
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -114,3 +114,67 @@ export function extractInstallScriptsInfo( | |||||||||||||
| npxDependencies: extractNpxDependencies(scripts), | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Pattern to match scripts that are just `node <file-path>` | ||||||||||||||
| * Captures the file path (relative paths with alphanumeric chars, dots, hyphens, underscores, and slashes) | ||||||||||||||
| */ | ||||||||||||||
| const NODE_SCRIPT_PATTERN = /^node\s+([\w./-]+)$/ | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Get the file path for an install script link. | ||||||||||||||
| * - If the script is `node <file-path>`, returns that file path | ||||||||||||||
| * - Otherwise, returns 'package.json' | ||||||||||||||
| * | ||||||||||||||
| * @param scriptContent - The content of the script | ||||||||||||||
| * @returns The file path to link to in the code tab | ||||||||||||||
| */ | ||||||||||||||
| export function getInstallScriptFilePath(scriptContent: string): string { | ||||||||||||||
| const match = NODE_SCRIPT_PATTERN.exec(scriptContent) | ||||||||||||||
|
|
||||||||||||||
| if (match?.[1]) { | ||||||||||||||
| // Script is `node <file-path>`, link to that file | ||||||||||||||
| // Normalize path: strip leading ./ | ||||||||||||||
| const filePath = match[1].replace(/^\.\//, '') | ||||||||||||||
|
|
||||||||||||||
| // Fall back to package.json if path contains navigational elements (the client-side routing can't handle these well) | ||||||||||||||
| if (filePath.includes('../') || filePath.includes('./')) { | ||||||||||||||
| return 'package.json' | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return filePath | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Default: link to package.json | ||||||||||||||
| return 'package.json' | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Parse an install script into a prefix and a linkable file path. | ||||||||||||||
| * - If the script is `node <file-path>`, returns { prefix: 'node ', filePath: '<file-path>' } | ||||||||||||||
| * so only the file path portion can be rendered as a link. | ||||||||||||||
| * - Otherwise, returns null (the entire script content should link to package.json). | ||||||||||||||
| * | ||||||||||||||
| * @param scriptContent - The content of the script | ||||||||||||||
| * @returns Parsed parts, or null if no node file path was extracted | ||||||||||||||
| */ | ||||||||||||||
| export function parseNodeScript( | ||||||||||||||
| scriptContent: string, | ||||||||||||||
| ): { prefix: string; filePath: string } | null { | ||||||||||||||
| const match = NODE_SCRIPT_PATTERN.exec(scriptContent) | ||||||||||||||
|
|
||||||||||||||
| if (match?.[1]) { | ||||||||||||||
| const filePath = match[1].replace(/^\.\//, '') | ||||||||||||||
|
|
||||||||||||||
| // Fall back if path contains navigational elements | ||||||||||||||
| if (filePath.includes('../') || filePath.includes('./')) { | ||||||||||||||
| return null | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Reconstruct the prefix (everything before the captured file path) | ||||||||||||||
| const prefix = scriptContent.slice(0, match.index + match[0].indexOf(match[1])) | ||||||||||||||
| return { prefix, filePath } | ||||||||||||||
|
Comment on lines
+174
to
+176
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. Prefix extraction breaks when the file name is 🛠️ Suggested fix- const prefix = scriptContent.slice(0, match.index + match[0].indexOf(match[1]))
+ const prefix = scriptContent.slice(0, scriptContent.length - filePath.length)📝 Committable suggestion
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return null | ||||||||||||||
| } | ||||||||||||||
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.
Reject absolute paths to avoid broken code‑tab links.
Absolute paths (e.g.,
node /usr/bin/install.js) currently match and will be linked, even though they won’t exist in the package code. Treat absolute paths as non‑linkable and fall back topackage.json/null.🛠️ Suggested fix
Also applies to: 169-172