-
Notifications
You must be signed in to change notification settings - Fork 83
feat(astro): add "Download lesson as zip" button #415
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1b5daef
feat(astro): add "Download lesson as zip" button
AriPerkkio 2843338
Merge remote-tracking branch 'upstream/main' into feat/download-zip
AriPerkkio 68e7ccf
fix: review
AriPerkkio 09c841f
test: update snapshots
AriPerkkio e942837
test: fix e2e settings
AriPerkkio 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
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
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
type: tutorial | ||
mainCommand: '' | ||
prepareCommands: [] | ||
downloadAsZip: true | ||
--- |
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,78 @@ | ||
/// <reference types="node" /> | ||
import { readdirSync, readFileSync, rmSync } from 'node:fs'; | ||
import type { Readable } from 'node:stream'; | ||
import { test, expect } from '@playwright/test'; | ||
import * as unzipper from 'unzipper'; | ||
import { theme } from '../../packages/theme/src/theme'; | ||
|
||
test('user can change theme', async ({ page }) => { | ||
await page.goto('/'); | ||
|
||
const heading = page.getByRole('heading', { level: 1 }); | ||
const html = page.locator('html'); | ||
|
||
// default light theme | ||
await expect(html).toHaveAttribute('data-theme', 'light'); | ||
await expect(heading).toHaveCSS('color', hexToRGB(theme.colors.gray[800])); | ||
|
||
await page.getByRole('navigation').getByRole('button', { name: 'Toggle Theme' }).click(); | ||
|
||
await expect(html).toHaveAttribute('data-theme', 'dark'); | ||
await expect(heading).toHaveCSS('color', hexToRGB(theme.colors.gray[200])); | ||
}); | ||
|
||
test('user can download project as zip', async ({ page }) => { | ||
await page.goto('/', { waitUntil: 'networkidle' }); | ||
|
||
const downloadPromise = page.waitForEvent('download'); | ||
await page.getByRole('navigation').getByRole('button', { name: 'Download lesson as zip-file' }).click(); | ||
|
||
const download = await downloadPromise; | ||
expect(download.suggestedFilename()).toBe('tests-file-tree-allow-edits-disabled.zip'); | ||
|
||
const stream = await download.createReadStream(); | ||
const files = await unzip(stream); | ||
|
||
expect(files).toMatchObject({ | ||
'./tutorial/file-on-template.js': "export default 'This file is present on template';\n", | ||
'./tutorial/first-level/file.js': "export default 'File in first level';\n", | ||
'./tutorial/first-level/second-level/file.js': "export default 'File in second level';\n", | ||
}); | ||
|
||
expect(files['./tutorial/index.mjs']).toMatch("import http from 'node:http'"); | ||
}); | ||
|
||
function hexToRGB(hex: string) { | ||
return `rgb(${parseInt(hex.slice(1, 3), 16)}, ${parseInt(hex.slice(3, 5), 16)}, ${parseInt(hex.slice(5, 7), 16)})`; | ||
} | ||
|
||
async function unzip(stream: Readable) { | ||
await stream.pipe(unzipper.Extract({ path: './downloads' })).promise(); | ||
|
||
const files = readDirectoryContents('./downloads'); | ||
rmSync('./downloads', { recursive: true }); | ||
|
||
return files.reduce( | ||
(all, current) => ({ | ||
...all, | ||
[current.name.replace('/downloads', '')]: current.content, | ||
}), | ||
{}, | ||
); | ||
} | ||
|
||
function readDirectoryContents(directory: string) { | ||
const files: { name: string; content: string }[] = []; | ||
|
||
for (const entry of readdirSync(directory, { withFileTypes: true })) { | ||
const name = `${directory}/${entry.name}`; | ||
|
||
if (entry.isFile()) { | ||
files.push({ name, content: readFileSync(name, 'utf-8') }); | ||
} else if (entry.isDirectory()) { | ||
files.push(...readDirectoryContents(name)); | ||
} | ||
} | ||
|
||
return files; | ||
} |
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,44 @@ | ||
import { tutorialStore, webcontainer as webcontainerPromise } from './webcontainer.js'; | ||
|
||
export function DownloadButton() { | ||
return ( | ||
<button | ||
title="Download lesson as zip-file" | ||
className="flex items-center text-2xl text-tk-elements-topBar-iconButton-iconColor hover:text-tk-elements-topBar-iconButton-iconColorHover transition-theme bg-tk-elements-topBar-iconButton-backgroundColor hover:bg-tk-elements-topBar-iconButton-backgroundColorHover p-1 rounded-md" | ||
onClick={onClick} | ||
> | ||
<div className="i-ph-download-simple" /> | ||
</button> | ||
); | ||
} | ||
|
||
async function onClick() { | ||
const lesson = tutorialStore.lesson; | ||
|
||
if (!lesson) { | ||
throw new Error('Missing lesson'); | ||
} | ||
Comment on lines
+18
to
+20
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. Oh I suppose we are throwing here because this can only happen in development if there's a bug? 👀 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. This is only to make Typescript happy 😄 |
||
|
||
const webcontainer = await webcontainerPromise; | ||
const data = await webcontainer.export('/home/tutorial', { format: 'zip', excludes: ['node_modules'] }); | ||
|
||
let filename = | ||
typeof lesson.data.downloadAsZip === 'object' | ||
? lesson.data.downloadAsZip.filename | ||
: [lesson.part?.id, lesson.chapter?.id, lesson.id].filter(Boolean).join('-'); | ||
|
||
if (!filename.endsWith('.zip')) { | ||
filename += '.zip'; | ||
} | ||
|
||
const link = document.createElement('a'); | ||
link.style.display = 'none'; | ||
link.download = filename; | ||
link.href = URL.createObjectURL(new Blob([data], { type: 'application/zip' })); | ||
|
||
document.body.appendChild(link); | ||
link.click(); | ||
|
||
document.body.removeChild(link); | ||
URL.revokeObjectURL(link.href); | ||
} |
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.