Repository navigation
feat(dev): support external app previews with a shared Bun runtime #78
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00bc647
feat(dev): preview external data apps with checkout dependencies
francoischalifour 1a63b15
feat(server): expose a reusable Bun single-file preview runtime
francoischalifour 9270ac9
refactor(preview): resolve dependencies from the runtime module
francoischalifour a809c51
fix(preview): make CI checks reliable and keep credentials private
francoischalifour 406aa6e
fix(bun): reject missing lakehouse credentials before startup
francoischalifour 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join, resolve, dirname } from 'node:path'; | ||
| import { | ||
| getTsconfig, | ||
| createPathsMatcher, | ||
| type TsConfigJson, | ||
| } from 'get-tsconfig'; | ||
| import manifest from '@/package.json'; | ||
| import { | ||
| previewLakehouseEnvironment, | ||
| ensurePackageBuild, | ||
| checkoutRoot, | ||
| } from '@/scripts/dev-resolve'; | ||
|
|
||
| type ImportPaths = Record<string, string[]>; | ||
|
|
||
| const usage = | ||
| 'Usage: bun run dev:dir <path>\nThe directory must contain a local data app with src/server.ts. Use `bun run dev:file <path>` for a single-file app.'; | ||
| const argument = process.argv[2]; | ||
| if (!argument) { | ||
| console.error(usage); | ||
| process.exit(1); | ||
| } | ||
| const directory = resolve(process.env.INIT_CWD ?? process.cwd(), argument); | ||
| const server = join(directory, 'src/server.ts'); | ||
| if (!existsSync(server)) { | ||
| console.error(`No local data app server at ${server}.\n${usage}`); | ||
| process.exit(1); | ||
| } | ||
| await ensurePackageBuild(); | ||
|
|
||
| const appConfig = readAppImportPaths(directory); | ||
| const checkoutPaths = checkoutImportPaths(); | ||
| const previewConfig = { | ||
| extends: appConfig.configPath, | ||
| compilerOptions: { | ||
| paths: { ...appConfig.paths, ...checkoutPaths }, | ||
| }, | ||
| } satisfies TsConfigJson; | ||
|
|
||
| const configDirectory = mkdtempSync(join(tmpdir(), 'data-app-dev-dir-')); | ||
| const tsconfig = join(configDirectory, 'tsconfig.json'); | ||
| writeFileSync(tsconfig, JSON.stringify(previewConfig)); | ||
|
|
||
| const { environment, target } = previewLakehouseEnvironment(); | ||
| console.log(target); | ||
| const child = Bun.spawn( | ||
| [ | ||
| process.execPath, | ||
| `--tsconfig-override=${tsconfig}`, | ||
| '--watch', | ||
| '--no-clear-screen', | ||
| 'src/server.ts', | ||
| ], | ||
| { | ||
| cwd: directory, | ||
| env: environment, | ||
| stdout: 'inherit', | ||
| stderr: 'inherit', | ||
| stdin: 'inherit', | ||
| } | ||
| ); | ||
| for (const signal of ['SIGINT', 'SIGTERM'] as const) | ||
| process.on(signal, () => child.kill(signal)); | ||
| const exitCode = await child.exited; | ||
| rmSync(configDirectory, { recursive: true, force: true }); | ||
| process.exit(exitCode); | ||
|
|
||
| function readAppImportPaths(directory: string): { | ||
| configPath: string | undefined; | ||
| paths: ImportPaths; | ||
| } { | ||
| const appTsconfig = getTsconfig(directory); | ||
| const matchPaths = appTsconfig ? createPathsMatcher(appTsconfig) : null; | ||
| const patterns = Object.keys( | ||
| appTsconfig?.config.compilerOptions?.paths ?? {} | ||
| ); | ||
| // Absolute targets preserve inherited aliases when the override lives elsewhere. | ||
| const paths = matchPaths | ||
| ? Object.fromEntries( | ||
| patterns.map(pattern => [pattern, matchPaths(pattern)]) | ||
| ) | ||
| : {}; | ||
| return { configPath: appTsconfig?.path, paths }; | ||
| } | ||
|
|
||
| function checkoutImportPaths(): ImportPaths { | ||
| const paths: ImportPaths = {}; | ||
| for (const [entry, target] of Object.entries(manifest.exports)) { | ||
| const subpath = entry === '.' ? '' : entry.slice(1); | ||
| const specifier = `@altertable/data-app${subpath}`; | ||
| const javascript = 'import' in target ? target.import : target.default; | ||
| paths[specifier] = [resolve(checkoutRoot, javascript)]; | ||
| } | ||
| for (const name of ['react', 'react-dom']) { | ||
| const entry = Bun.resolveSync(name, checkoutRoot); | ||
| paths[name] = [entry]; | ||
| paths[`${name}/*`] = [join(dirname(entry), '*')]; | ||
| } | ||
| return paths; | ||
| } | ||
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,36 @@ | ||
| import { existsSync, statSync } from 'node:fs'; | ||
| import { resolve } from 'node:path'; | ||
| import { | ||
| previewLakehouseEnvironment, | ||
| ensurePackageBuild, | ||
| } from '@/scripts/dev-resolve'; | ||
|
|
||
| const usage = | ||
| 'Usage: bun run dev:file <path>\nThe file must be a single-file data app such as examples/starter-data-app/index.tsx. Use `bun run dev:dir <path>` for a local app directory.'; | ||
| const argument = process.argv[2]; | ||
| if (!argument) { | ||
| console.error(usage); | ||
| process.exit(1); | ||
| } | ||
| const appFile = resolve(process.env.INIT_CWD ?? process.cwd(), argument); | ||
| if (!existsSync(appFile) || !statSync(appFile).isFile()) { | ||
| console.error(`No data app file at ${appFile}.\n${usage}`); | ||
| process.exit(1); | ||
| } | ||
| await ensurePackageBuild(); | ||
| const [{ serveAppFile }, { localLakehouse }] = await Promise.all([ | ||
| import('@altertable/data-app/server/bun/preview'), | ||
| import('@altertable/data-app/server/bun'), | ||
| ]); | ||
| const { environment, target } = previewLakehouseEnvironment(); | ||
| const preview = await serveAppFile({ | ||
| file: appFile, | ||
| port: Number(process.env.PORT ?? 25937), | ||
| lakehouse: localLakehouse(environment), | ||
| }); | ||
| console.log(`Data app ${appFile} running at ${preview.url}\n${target}`); | ||
|
francoischalifour marked this conversation as resolved.
Fixed
|
||
| for (const signal of ['SIGINT', 'SIGTERM'] as const) | ||
| process.on(signal, async () => { | ||
| await preview.stop(); | ||
| process.exit(0); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.