-
Notifications
You must be signed in to change notification settings - Fork 99
feat: cloudscape frontend #1606
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
base: feat/cdk-explorer
Are you sure you want to change the base?
Changes from all commits
978beac
051cc11
6337163
22fb2d6
cf3515c
11b4ae1
83fce7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * Builds the web explorer SPA into lib/web/static (typechecked via | ||
| * tsconfig.frontend.json, since esbuild only transpiles), then writes the same | ||
| * assets to lib/web/web-assets.generated.json so they ride the require() graph | ||
| * into the published CLI bundle (express.static paths are not bundled). | ||
| */ | ||
| import { execFileSync } from 'child_process'; | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import * as esbuild from 'esbuild'; | ||
|
|
||
| const packageRoot = path.resolve(__dirname, '..'); | ||
| const frontendDir = path.join(packageRoot, 'frontend'); | ||
| const outDir = path.join(packageRoot, 'lib', 'web', 'static'); | ||
| const embeddedAssetsFile = path.join(packageRoot, 'lib', 'web', 'web-assets.generated.json'); | ||
|
|
||
| async function main(): Promise<void> { | ||
| typecheck(); | ||
|
|
||
| fs.mkdirSync(outDir, { recursive: true }); | ||
|
|
||
| await esbuild.build({ | ||
| entryPoints: [path.join(frontendDir, 'index.tsx')], | ||
| bundle: true, | ||
| outfile: path.join(outDir, 'bundle.js'), | ||
| format: 'iife', | ||
| platform: 'browser', | ||
| target: 'es2020', | ||
| jsx: 'automatic', | ||
| loader: { '.css': 'css', '.svg': 'dataurl', '.png': 'dataurl' }, | ||
| sourcemap: true, | ||
| logLevel: 'info', | ||
| }); | ||
|
|
||
| fs.copyFileSync(path.join(frontendDir, 'index.html'), path.join(outDir, 'index.html')); | ||
| writeEmbeddedAssets(); | ||
| } | ||
|
|
||
| function writeEmbeddedAssets(): void { | ||
| const assets: Record<string, string> = { | ||
| 'index.html': fs.readFileSync(path.join(frontendDir, 'index.html'), 'utf-8'), | ||
| 'bundle.js': fs.readFileSync(path.join(outDir, 'bundle.js'), 'utf-8'), | ||
| 'bundle.css': fs.readFileSync(path.join(outDir, 'bundle.css'), 'utf-8'), | ||
| }; | ||
| fs.writeFileSync(embeddedAssetsFile, JSON.stringify(assets)); | ||
| } | ||
|
|
||
| function typecheck(): void { | ||
| execFileSync('tsc', ['--noEmit', '-p', path.join(packageRoot, 'tsconfig.frontend.json')], { | ||
| cwd: packageRoot, | ||
| stdio: 'inherit', | ||
| }); | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import Box from '@cloudscape-design/components/box'; | ||
| import Container from '@cloudscape-design/components/container'; | ||
| import ContentLayout from '@cloudscape-design/components/content-layout'; | ||
| import Grid from '@cloudscape-design/components/grid'; | ||
| import Header from '@cloudscape-design/components/header'; | ||
| import SpaceBetween from '@cloudscape-design/components/space-between'; | ||
| import * as React from 'react'; | ||
| import { FilePane } from './components/FilePane'; | ||
|
|
||
| /** Web explorer shell: Resource Tree (left), two file panes, Violations (bottom). Tree/violations are placeholders until the cloud-assembly reader is wired in. */ | ||
| export function App(): JSX.Element { | ||
|
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. Is there a way to make this more flexible in the future so customers can resize/rearrange their panels?
Author
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. TL; DR: Kind of, but the UI looks way worse. I played around with this for a few hours yesterday. In the next PR, I have code that makes the construct tree resizable horizontally. In my RFC, i had re-arrangable panes as a stretch goal. With cloudscape I've actually been finding that pretty hard, so my plan was to leave it as a stretch goal for now, lmk if you think its important enough to devote time to now. |
||
| return ( | ||
| <ContentLayout | ||
| header={ | ||
| <Header variant="h1" description="last updated: —"> | ||
| CDK Web Explorer | ||
| </Header> | ||
| } | ||
| > | ||
| <SpaceBetween size="l"> | ||
| <Grid gridDefinition={[{ colspan: 3 }, { colspan: 9 }]}> | ||
| <Container header={<Header variant="h2">Resource Tree</Header>}> | ||
| <Box color="text-status-inactive"> | ||
| Construct tree appears here once the cloud-assembly reader is wired in. | ||
| </Box> | ||
| </Container> | ||
| <Grid gridDefinition={[{ colspan: 6 }, { colspan: 6 }]}> | ||
| <FilePane title="file 1" /> | ||
| <FilePane title="file 2" /> | ||
| </Grid> | ||
| </Grid> | ||
| <Container header={<Header variant="h2">Violations</Header>}> | ||
| <Box color="text-status-inactive"> | ||
| Policy-validation violations appear here once the cloud-assembly reader is wired in. | ||
| </Box> | ||
| </Container> | ||
| </SpaceBetween> | ||
| </ContentLayout> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { DirEntry, FilesResponse, FileResponse } from '../lib/web/protocol'; | ||
|
|
||
| export type { DirEntry, FilesResponse, FileResponse }; | ||
|
|
||
| async function getJson<T>(url: string): Promise<T> { | ||
| const res = await fetch(url); | ||
| if (!res.ok) { | ||
| const body = await res.json().catch(() => ({})); | ||
| throw new Error((body as { error?: string }).error ?? `request failed: ${res.status}`); | ||
| } | ||
| return res.json() as Promise<T>; | ||
| } | ||
|
|
||
| export const api = { | ||
| listFiles: (dir = ''): Promise<FilesResponse> => getJson(`/api/files?dir=${encodeURIComponent(dir)}`), | ||
| readFile: (filePath: string): Promise<FileResponse> => getJson(`/api/file?path=${encodeURIComponent(filePath)}`), | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import Box from '@cloudscape-design/components/box'; | ||
| import Button from '@cloudscape-design/components/button'; | ||
| import Container from '@cloudscape-design/components/container'; | ||
| import Header from '@cloudscape-design/components/header'; | ||
| import SpaceBetween from '@cloudscape-design/components/space-between'; | ||
| import * as React from 'react'; | ||
| import { api, type DirEntry } from '../api'; | ||
|
|
||
| interface FilePaneProps { | ||
| /** Heading shown in the pane header (e.g. "file 1"). */ | ||
| readonly title: string; | ||
| } | ||
|
|
||
| /** | ||
| * A self-contained code pane with a server-backed file picker. Browses | ||
| * directories under the app root via /api/files and shows file contents via | ||
| * /api/file. Rendered once per code pane (center and right). | ||
| */ | ||
| export function FilePane({ title }: FilePaneProps): JSX.Element { | ||
| const [picking, setPicking] = React.useState(false); | ||
| const [dir, setDir] = React.useState(''); | ||
| const [entries, setEntries] = React.useState<readonly DirEntry[]>([]); | ||
| const [filePath, setFilePath] = React.useState<string | undefined>(); | ||
| const [content, setContent] = React.useState(''); | ||
| const [error, setError] = React.useState<string | undefined>(); | ||
|
|
||
| const browse = React.useCallback(async (nextDir: string) => { | ||
| try { | ||
| const res = await api.listFiles(nextDir); | ||
| setDir(res.dir); | ||
| setEntries(res.entries); | ||
| setError(undefined); | ||
| } catch (err) { | ||
| setError(err instanceof Error ? err.message : String(err)); | ||
| } | ||
| }, []); | ||
|
|
||
| const openPicker = React.useCallback(() => { | ||
| setPicking(true); | ||
| void browse(''); | ||
| }, [browse]); | ||
|
|
||
| const choose = React.useCallback(async (entry: DirEntry) => { | ||
| if (entry.type === 'dir') { | ||
| void browse(entry.path); | ||
| return; | ||
| } | ||
| try { | ||
| const res = await api.readFile(entry.path); | ||
| setFilePath(res.path); | ||
| setContent(res.content); | ||
| setPicking(false); | ||
| setError(undefined); | ||
| } catch (err) { | ||
| setError(err instanceof Error ? err.message : String(err)); | ||
| } | ||
| }, [browse]); | ||
|
|
||
| return ( | ||
| <Container | ||
| header={ | ||
| <Header variant="h2" actions={<Button iconName="folder-open" onClick={openPicker}>Open file…</Button>}> | ||
| {filePath ?? title} | ||
| </Header> | ||
| } | ||
| > | ||
| <SpaceBetween size="s"> | ||
| {error && <Box color="text-status-error">{error}</Box>} | ||
| {picking ? ( | ||
| <FileBrowser dir={dir} entries={entries} onChoose={choose} onUp={() => browse(parentOf(dir))} /> | ||
| ) : ( | ||
| <pre style={CODE_STYLE}>{content || 'No file selected.'}</pre> | ||
| )} | ||
| </SpaceBetween> | ||
| </Container> | ||
| ); | ||
| } | ||
|
|
||
| const CODE_STYLE: React.CSSProperties = { | ||
| margin: 0, | ||
| maxHeight: '60vh', | ||
| overflow: 'auto', | ||
| fontFamily: 'Monaco, Menlo, "Courier New", monospace', | ||
| fontSize: '12px', | ||
| whiteSpace: 'pre', | ||
| }; | ||
|
|
||
| function FileBrowser(props: { | ||
| readonly dir: string; | ||
| readonly entries: readonly DirEntry[]; | ||
| readonly onChoose: (entry: DirEntry) => void; | ||
| readonly onUp: () => void; | ||
| }): JSX.Element { | ||
| return ( | ||
| <SpaceBetween size="xxs"> | ||
| <Box variant="code">/{props.dir}</Box> | ||
| {props.dir !== '' && <Button variant="inline-link" iconName="folder" onClick={props.onUp}>../</Button>} | ||
| {props.entries.map((entry) => ( | ||
| <Button | ||
| key={entry.path} | ||
| variant="inline-link" | ||
| iconName={entry.type === 'dir' ? 'folder' : 'file'} | ||
| onClick={() => props.onChoose(entry)} | ||
| > | ||
| {entry.type === 'dir' ? `${entry.name}/` : entry.name} | ||
| </Button> | ||
| ))} | ||
| </SpaceBetween> | ||
| ); | ||
| } | ||
|
|
||
| function parentOf(dir: string): string { | ||
| const idx = dir.lastIndexOf('/'); | ||
| return idx === -1 ? '' : dir.slice(0, idx); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>CDK Web Explorer</title> | ||
| <link rel="stylesheet" href="./bundle.css" /> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script src="./bundle.js"></script> | ||
| </body> | ||
| </html> |
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.
Can you explain a bit why you are doing this unique path to bundling the code for the web explorer? Is it because of the frontend code?
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.
yep, it's driven entirely by the frontend, and there are two separate problems this handles:
First, the frontend is browser code, not server code, so it can't go through the normal per-file
tsccompile. esbuild bundlesfrontend/index.tsxinto a singlebundle.js/bundle.css. esbuild only transpiles, so we typecheck the frontend separately withtsc --noEmit -p tsconfig.frontend.jsonSecond, getting those assets into the published CLI, which is the more complicated part. The aws-cdk CLI ships as one bundle that only includes what's reachable through the require() graph. Assets served off disk via
express.static/sendFileare invisible to the bundler and get dropped from the npm tarball, so the explorer would 404 in a real install even though it works from source. To avoid that, the script writes the built assets into lib/web/web-assets.generated.json, andweb-assets.tsdoes a staticrequire('./web-assets.generated.json').That require carries the bytes into the CLI bundle, and the server serves them from memory rather than disk.