-
Notifications
You must be signed in to change notification settings - Fork 111
feat(nuxi): add --portless for dev server #1301
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
Draft
onmax
wants to merge
9
commits into
main
Choose a base branch
from
feat/nuxi-portless
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
214ab48
feat(nuxi): add --portless for dev server
onmax 03c427d
fix(nuxi): align portless with dev server host handling
onmax f97a340
Merge origin/main into feat/nuxi-portless
onmax 7d5e691
[autofix.ci] apply automated fixes
autofix-ci[bot] 26acd9f
fix(nuxi): remove portless alias on exit
onmax cbfbdeb
[autofix.ci] apply automated fixes
autofix-ci[bot] 16f9ce1
test(nuxi): cover forked portless restarts
onmax babcca4
refactor(nuxi): simplify portless lifecycle
onmax 8f87692
test(nuxi): cover portless restart and failure paths
onmax 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,116 @@ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { readFile } from 'node:fs/promises' | ||
| import { basename, join } from 'node:path' | ||
| import process from 'node:process' | ||
|
|
||
| import { x } from 'tinyexec' | ||
|
|
||
| const DEFAULT_PORTLESS_NAME = 'nuxt-app' | ||
|
|
||
| export async function ensurePortlessAvailable(cwd: string) { | ||
| try { | ||
| await runPortless(cwd, ['--version']) | ||
| } | ||
| catch (error) { | ||
| if (typeof error === 'object' && error && 'code' in error && error.code === 'ENOENT') { | ||
| throw new Error('Portless is required for `--portless`. Install it from https://portless.sh') | ||
| } | ||
|
|
||
| throw createPortlessError('check portless availability', error) | ||
| } | ||
| } | ||
|
|
||
| export async function resolvePortlessURL(cwd: string, name: string) { | ||
| try { | ||
| await runPortless(cwd, ['proxy', 'start']) | ||
| const result = await runPortless(cwd, ['get', '--no-worktree', name]) | ||
| return result.stdout.trim() | ||
| } | ||
| catch (error) { | ||
| throw createPortlessError('resolve the portless URL', error) | ||
| } | ||
| } | ||
|
|
||
| export async function registerPortlessAlias(cwd: string, name: string, port: number) { | ||
| try { | ||
| await runPortless(cwd, ['alias', name, `${port}`]) | ||
| } | ||
| catch (error) { | ||
| throw createPortlessError(`register the portless alias for port ${port}`, error) | ||
| } | ||
| } | ||
|
|
||
| export async function removePortlessAlias(cwd: string, name: string) { | ||
| try { | ||
| await runPortless(cwd, ['alias', '--remove', name]) | ||
| } | ||
| catch (error) { | ||
| throw createPortlessError(`remove the portless alias for ${name}`, error) | ||
| } | ||
| } | ||
|
|
||
| export function registerPortlessExitCleanup(cwd: string, name: string) { | ||
| let disposed = false | ||
|
|
||
| const cleanup = () => { | ||
| if (disposed) { | ||
| return | ||
| } | ||
|
|
||
| disposed = true | ||
| process.off('exit', cleanup) | ||
| runPortlessSync(cwd, ['alias', '--remove', name]) | ||
| } | ||
|
|
||
| process.on('exit', cleanup) | ||
|
|
||
| return () => { | ||
| disposed = true | ||
| process.off('exit', cleanup) | ||
| } | ||
| } | ||
|
|
||
| function createPortlessError(action: string, error: unknown) { | ||
| const message = typeof error === 'object' && error && 'stderr' in error && typeof error.stderr === 'string' && error.stderr.trim() | ||
| ? error.stderr.trim() | ||
| : error instanceof Error && error.message | ||
| ? error.message | ||
| : 'Unknown portless error' | ||
|
|
||
| return new Error(`Failed to ${action}: ${message}`) | ||
| } | ||
|
|
||
| export async function resolvePortlessName(cwd: string) { | ||
| const packageName = await readFile(join(cwd, 'package.json'), 'utf8') | ||
| .then(contents => JSON.parse(contents)) | ||
| .then(pkg => typeof pkg.name === 'string' ? pkg.name : undefined) | ||
| .catch(() => undefined) | ||
| return normalizePortlessName(packageName || basename(cwd)) | ||
| } | ||
|
|
||
| function normalizePortlessName(value: string) { | ||
| const normalizedValue = value | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9-]+/g, '-') | ||
| .replace(/-+/g, '-') | ||
| .replace(/^-+|-+$/g, '') | ||
|
|
||
| return normalizedValue || DEFAULT_PORTLESS_NAME | ||
| } | ||
|
|
||
| function runPortless(cwd: string, args: string[]) { | ||
| return x('portless', args, { | ||
| throwOnError: true, | ||
| nodeOptions: { | ||
| cwd, | ||
| stdio: 'pipe', | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| function runPortlessSync(cwd: string, args: string[]) { | ||
| spawnSync('portless', args, { | ||
| cwd, | ||
| stdio: 'ignore', | ||
| }) | ||
| } |
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.