-
-
Notifications
You must be signed in to change notification settings - Fork 473
feat: add package download button #1586
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
Merged
Merged
Changes from 30 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
5d3bea5
feat: Add download button component and install size types
Adebesin-Cell 6a28f59
fix: Update function to use correct translation function
Adebesin-Cell cf35100
feat: Add tarball URLs for dependencies
Adebesin-Cell 91e2933
style: Add tarball URLs to test dependencies
Adebesin-Cell 7a6e29e
feat(Button, PackageDownloadButton): Add async download functionality
Adebesin-Cell 14912af
fix: address CodeRabbit review feedback
Adebesin-Cell e4b803a
fix: installSize prop type mismatch (undefined → null)
Adebesin-Cell 16e9dd7
fix: wrap DownloadButton in ClientOnly to prevent hydration mismatch
Adebesin-Cell 6ecfc4d
fix: add download button skeleton to package loading state
Adebesin-Cell 60aac4a
Merge branch 'main' into feat/download-button
Adebesin-Cell f393057
fix: remove useId() and ClientOnly from DownloadButton
Adebesin-Cell e98977e
fix: add missing composable imports to package page
Adebesin-Cell 70f9d59
fix: make DownloadButton SSR-safe with useId and CSS reduced motion
Adebesin-Cell c8e7774
[autofix.ci] apply automated fixes
autofix-ci[bot] 36461d7
fix: resolve hydration mismatch on package page
Adebesin-Cell 401e44f
fix: add tarballUrl to dependency-resolver test expectation
Adebesin-Cell 989dfa3
[autofix.ci] apply automated fixes
autofix-ci[bot] cef21bb
Update app/components/Package/DownloadButton.vue
Adebesin-Cell ba7281d
refactor: replace installSize prop with dependencies in DownloadButton
Adebesin-Cell 1c7a680
fix: add missing useI18n import in DownloadButton
Adebesin-Cell b8c7570
fix: use $t auto-import instead of useI18n in DownloadButton
Adebesin-Cell cf724bd
fix: use static i18n keys in DownloadButton for static analysis
Adebesin-Cell b0de596
[autofix.ci] apply automated fixes
autofix-ci[bot] b1ee806
feat: show loading state for dependencies download option
Adebesin-Cell a91db2b
fix: hide dependencies option when package has no deps
Adebesin-Cell 8f9ac17
feat: make dependencies download script cross-platform using Node.js
Adebesin-Cell eaa2de2
fix: correct dependencies download label from .sh to .js
Adebesin-Cell 19eb93c
Merge branch 'main' into feat/download-button
Adebesin-Cell eb8c0da
fix: use Intl.Segmenter for avatar first character extraction
Adebesin-Cell c76c15c
Revert "fix: use Intl.Segmenter for avatar first character extraction"
Adebesin-Cell 5e0882f
fix: remove download dependencies script option from download button
Adebesin-Cell f48b384
Merge branch 'main' into feat/download-button
Adebesin-Cell 992b9ea
[autofix.ci] apply automated fixes
autofix-ci[bot] 1b7487a
Merge branch 'main' into feat/download-button
ghostdevv b56aa2e
refactor: simplify button and css
ghostdevv 52a9dcb
refactor: simplify
ghostdevv 02d6bb1
Merge remote-tracking branch 'origin/main' into feat/download-button
ghostdevv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,311 @@ | ||
| <script setup lang="ts"> | ||
| import type { SlimPackumentVersion, DependencySize } from '#shared/types' | ||
| import { onClickOutside, useEventListener, useMediaQuery } from '@vueuse/core' | ||
|
|
||
| const props = withDefaults( | ||
| defineProps<{ | ||
| packageName: string | ||
| version: SlimPackumentVersion | ||
| dependencies: DependencySize[] | null | ||
| dependenciesLoading?: boolean | ||
| size?: 'small' | 'medium' | ||
| }>(), | ||
| { | ||
| dependenciesLoading: false, | ||
| size: 'medium', | ||
| }, | ||
| ) | ||
|
|
||
| const triggerRef = useTemplateRef('triggerRef') | ||
| const listRef = useTemplateRef('listRef') | ||
| const isOpen = shallowRef(false) | ||
| const highlightedIndex = shallowRef(-1) | ||
| const dropdownPosition = shallowRef<{ top: number; left: number } | null>(null) | ||
|
|
||
| const menuId = 'download-menu' | ||
| const menuItems = computed(() => { | ||
| const items: { id: string; icon: string; disabled: boolean }[] = [ | ||
| { id: 'package', icon: 'i-lucide:package', disabled: false }, | ||
| ] | ||
| if (props.dependenciesLoading) { | ||
| items.push({ | ||
| id: 'dependencies', | ||
| icon: 'i-lucide:loader-circle', | ||
| disabled: true, | ||
| }) | ||
| } else if (props.dependencies?.length) { | ||
| items.push({ | ||
| id: 'dependencies', | ||
| icon: 'i-lucide:list-tree', | ||
| disabled: false, | ||
| }) | ||
| } | ||
| return items | ||
| }) | ||
|
|
||
| const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)') | ||
|
|
||
| function getDropdownStyle(): Record<string, string> { | ||
| if (!dropdownPosition.value) return {} | ||
| return { | ||
| top: `${dropdownPosition.value.top}px`, | ||
| left: `${dropdownPosition.value.left}px`, | ||
| } | ||
| } | ||
|
|
||
| function toggle() { | ||
| if (isOpen.value) { | ||
| close() | ||
| } else { | ||
| const rect = triggerRef.value?.$el?.getBoundingClientRect() | ||
| if (rect) { | ||
| dropdownPosition.value = { | ||
| top: rect.bottom + 4, | ||
| left: rect.left, | ||
| } | ||
| } | ||
| isOpen.value = true | ||
| highlightedIndex.value = 0 | ||
| } | ||
| } | ||
|
|
||
| function close() { | ||
| isOpen.value = false | ||
| highlightedIndex.value = -1 | ||
| } | ||
|
|
||
| onClickOutside(listRef, close, { ignore: [triggerRef] }) | ||
|
|
||
| function handleKeydown(event: KeyboardEvent) { | ||
| if (!isOpen.value) { | ||
| if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') { | ||
| event.preventDefault() | ||
| toggle() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| switch (event.key) { | ||
| case 'ArrowDown': | ||
| event.preventDefault() | ||
| highlightedIndex.value = (highlightedIndex.value + 1) % menuItems.value.length | ||
| break | ||
| case 'ArrowUp': | ||
| event.preventDefault() | ||
| highlightedIndex.value = | ||
| highlightedIndex.value <= 0 ? menuItems.value.length - 1 : highlightedIndex.value - 1 | ||
| break | ||
| case 'Enter': | ||
| case ' ': | ||
| event.preventDefault() | ||
| handleAction(menuItems.value[highlightedIndex.value]) | ||
| break | ||
| case 'Escape': | ||
| event.preventDefault() | ||
| close() | ||
| triggerRef.value?.$el?.focus() | ||
| break | ||
| case 'Tab': | ||
| close() | ||
| break | ||
| } | ||
| } | ||
|
|
||
| function handleAction(item: (typeof menuItems.value)[number] | undefined) { | ||
| if (!item || item.disabled) return | ||
| if (item.id === 'package') { | ||
| downloadPackage() | ||
| } else if (item.id === 'dependencies') { | ||
| downloadDependenciesScript() | ||
| } | ||
| close() | ||
| } | ||
|
|
||
| async function downloadPackage() { | ||
| const tarballUrl = props.version.dist.tarball | ||
| if (!tarballUrl) return | ||
|
|
||
| try { | ||
| const response = await fetch(tarballUrl) | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch tarball (${response.status})`) | ||
| } | ||
| const blob = await response.blob() | ||
| const url = URL.createObjectURL(blob) | ||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.download = `${props.packageName.replace(/\//g, '__')}-${props.version.version}.tgz` | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| URL.revokeObjectURL(url) | ||
| } catch { | ||
| // Fallback to direct link for non-CORS or other issues, though download attribute may be ignored | ||
| const link = document.createElement('a') | ||
| link.href = tarballUrl | ||
| link.download = `${props.packageName.replace(/\//g, '__')}-${props.version.version}.tgz` | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| } | ||
| } | ||
|
|
||
| function downloadDependenciesScript() { | ||
| if (!props.dependencies?.length) return | ||
|
|
||
| const tarballs: { name: string; version: string; url: string }[] = [] | ||
|
|
||
| const rootTarball = props.version.dist.tarball | ||
| if (rootTarball) { | ||
| tarballs.push({ name: props.packageName, version: props.version.version, url: rootTarball }) | ||
| } | ||
|
|
||
| props.dependencies.forEach(dep => { | ||
| if (!dep.tarballUrl) return | ||
| tarballs.push({ name: dep.name, version: dep.version, url: dep.tarballUrl }) | ||
| }) | ||
|
|
||
| const sanitize = (name: string) => name.replace(/\//g, '__') | ||
|
|
||
| // Node.js script — works on all platforms | ||
| const lines = [ | ||
| '#!/usr/bin/env node', | ||
| `// Download dependencies for ${props.packageName}@${props.version.version}`, | ||
| '// Run: node <filename>', | ||
| '', | ||
| "const { mkdirSync, createWriteStream } = require('fs');", | ||
| "const https = require('https');", | ||
| "const http = require('http');", | ||
| "const { basename } = require('path');", | ||
| '', | ||
| "const dir = 'tarballs';", | ||
| 'mkdirSync(dir, { recursive: true });', | ||
| '', | ||
| 'const tarballs = [', | ||
| ...tarballs.map(t => ` { name: '${t.name}', version: '${t.version}', url: '${t.url}' },`), | ||
| '];', | ||
| '', | ||
| 'function download(url, dest) {', | ||
| ' return new Promise((resolve, reject) => {', | ||
| " const client = url.startsWith('https') ? https : http;", | ||
| ' client.get(url, (res) => {', | ||
| ' if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {', | ||
| ' return download(res.headers.location, dest).then(resolve, reject);', | ||
| ' }', | ||
| ' if (res.statusCode !== 200) {', | ||
| ' return reject(new Error(`HTTP ${res.statusCode} for ${url}`));', | ||
| ' }', | ||
| ' const file = createWriteStream(dest);', | ||
| " res.pipe(file).on('finish', () => file.close(resolve));", | ||
| ' }).on("error", reject);', | ||
| ' });', | ||
| '}', | ||
| '', | ||
| '(async () => {', | ||
| ' for (const t of tarballs) {', | ||
| " const filename = `${t.name.replace(/\\//g, '__')}-${t.version}.tgz`;", | ||
| ' const dest = `${dir}/${filename}`;', | ||
| ' console.log(`Downloading ${t.name}@${t.version}...`);', | ||
| ' await download(t.url, dest);', | ||
| ' }', | ||
| ' console.log(`Done! ${tarballs.length} tarball(s) saved to ${dir}/`);', | ||
| '})();', | ||
| ] | ||
|
|
||
| const blob = new Blob([lines.join('\n')], { type: 'application/javascript' }) | ||
| const url = URL.createObjectURL(blob) | ||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.download = `download-${sanitize(props.packageName)}-deps.js` | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| URL.revokeObjectURL(url) | ||
| } | ||
|
|
||
| useEventListener('scroll', () => isOpen.value && close(), { passive: true }) | ||
|
|
||
| defineOptions({ | ||
| inheritAttrs: false, | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <ButtonBase | ||
| ref="triggerRef" | ||
| v-bind="$attrs" | ||
| type="button" | ||
| :variant="size === 'small' ? 'subtle' : 'secondary'" | ||
| :size | ||
| classicon="i-lucide:download" | ||
| :aria-expanded="isOpen" | ||
| aria-haspopup="menu" | ||
| :aria-controls="menuId" | ||
| @click="toggle" | ||
| @keydown="handleKeydown" | ||
| > | ||
| {{ $t('package.download.button') }} | ||
| <span | ||
| class="i-lucide:chevron-down ms-1" | ||
| :class="[ | ||
| size === 'small' ? 'w-3 h-3' : 'w-3.5 h-3.5', | ||
| { 'rotate-180': isOpen }, | ||
| prefersReducedMotion ? '' : 'transition-transform duration-200', | ||
| ]" | ||
| aria-hidden="true" | ||
| /> | ||
| </ButtonBase> | ||
|
|
||
| <Teleport to="body"> | ||
| <Transition | ||
| :enter-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-150'" | ||
| :enter-from-class="prefersReducedMotion ? '' : 'opacity-0'" | ||
| enter-to-class="opacity-100" | ||
| :leave-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-100'" | ||
| leave-from-class="opacity-100" | ||
| :leave-to-class="prefersReducedMotion ? '' : 'opacity-0'" | ||
| > | ||
| <ul | ||
| v-if="isOpen" | ||
| :id="menuId" | ||
| ref="listRef" | ||
| role="menu" | ||
| :aria-label="$t('package.download.button')" | ||
| :style="getDropdownStyle()" | ||
| class="fixed bg-bg-subtle border border-border rounded-md shadow-lg z-50" | ||
| @keydown="handleKeydown" | ||
| > | ||
| <li | ||
| v-for="(item, index) in menuItems" | ||
| :key="item.id" | ||
| role="menuitem" | ||
| :aria-disabled="item.disabled || undefined" | ||
| class="flex items-center gap-2 px-3 py-1.5 text-sm transition-colors duration-150" | ||
| :class="[ | ||
| item.disabled | ||
| ? 'cursor-default text-fg-muted/50' | ||
| : highlightedIndex === index | ||
| ? 'cursor-pointer bg-bg-elevated text-fg' | ||
| : 'cursor-pointer text-fg-muted hover:bg-bg-elevated hover:text-fg', | ||
| ]" | ||
| @click="handleAction(item)" | ||
| @mouseenter="highlightedIndex = index" | ||
| > | ||
| <span | ||
| :class="[ | ||
| item.icon, | ||
| { 'animate-spin': item.id === 'dependencies' && dependenciesLoading }, | ||
| ]" | ||
| class="w-4 h-4" | ||
| aria-hidden="true" | ||
| /> | ||
| {{ | ||
| item.id === 'package' | ||
| ? $t('package.download.package') | ||
| : $t('package.download.dependencies') | ||
| }} | ||
| </li> | ||
| </ul> | ||
| </Transition> | ||
| </Teleport> | ||
| </template> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm having a hard time imagining the value of this feature. What use cases are you thinking of?
My suggestion would be to punt on this part for this PR and revisit this in a follow-up. Does that seem ok?
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.
That’s fair, I’m happy to revisit later.
The main use case I had in mind is allowing users to download dependency tarballs directly so they can inspect or audit them before installing. It’s not something everyone would use, but it can be helpful in cases where people want more visibility into what’s being pulled in. It aligns with the original author of the feature as well #1528 (comment)
But we can talk about it if we want to implement it, I'm happy to work on that and make it cross-platform as well 👍