-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds mechanism to support managed npm install runs from within the ap…
…p builder (#381) * feat: add initial app specific PackageJsonContext, and small test interface in settings to exercise it * fix: make borders more consistent in the statusbar errors panel * fix: address incorrect useEffect dependencies * fix: remove forgotten trigger button from preview! * fix: address missing grow-0 shrink-0 on statusbar, it was getting squished in some cases * fix: remove bogus processMetadata.set line, this was an oversight * fix: remove unused variable
- Loading branch information
Showing
11 changed files
with
201 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,37 @@ | ||
import { Button } from '@srcbook/components/src/components/ui/button'; | ||
import { usePackageJson } from '../use-package-json'; | ||
|
||
export default function SettingsPanel() { | ||
return null; | ||
const { status, output, npmInstall } = usePackageJson(); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4 px-5 w-[360px]"> | ||
<div> | ||
<Button onClick={() => npmInstall()} disabled={status === 'installing'}> | ||
Run npm install | ||
</Button> | ||
</div> | ||
<div> | ||
<Button | ||
onClick={() => npmInstall(['uuid'])} | ||
variant="secondary" | ||
disabled={status === 'installing'} | ||
> | ||
Run npm install uuid | ||
</Button> | ||
</div> | ||
|
||
{status !== 'idle' ? ( | ||
<> | ||
<span> | ||
Status: <code>{status}</code> | ||
</span> | ||
<pre className="font-mono text-sm bg-tertiary p-2 overflow-auto rounded-md border"> | ||
{/* FIXME: disambiguate between stdout and stderr in here using n.type! */} | ||
{output.map((n) => n.data).join('\n')} | ||
</pre> | ||
</> | ||
) : null} | ||
</div> | ||
); | ||
} |
This file contains 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 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 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 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,82 @@ | ||
import React, { createContext, useCallback, useContext, useState } from 'react'; | ||
import { OutputType } from '@srcbook/components/src/types'; | ||
import { AppChannel } from '@/clients/websocket'; | ||
import { | ||
DependenciesInstallLogPayloadType, | ||
DependenciesInstallStatusPayloadType, | ||
} from '@srcbook/shared'; | ||
import { useLogs } from './use-logs'; | ||
|
||
type NpmInstallStatus = 'idle' | 'installing' | 'complete' | 'failed'; | ||
|
||
export interface PackageJsonContextValue { | ||
npmInstall: (packages?: string[]) => void; | ||
status: NpmInstallStatus; | ||
installing: boolean; | ||
failed: boolean; | ||
output: Array<OutputType>; | ||
} | ||
|
||
const PackageJsonContext = createContext<PackageJsonContextValue | undefined>(undefined); | ||
|
||
type ProviderPropsType = { | ||
channel: AppChannel; | ||
children: React.ReactNode; | ||
}; | ||
|
||
export function PackageJsonProvider({ channel, children }: ProviderPropsType) { | ||
const [status, setStatus] = useState<NpmInstallStatus>('idle'); | ||
const [output, setOutput] = useState<Array<OutputType>>([]); | ||
|
||
const { addError } = useLogs(); | ||
|
||
const npmInstall = useCallback( | ||
(packages?: Array<string>) => { | ||
// NOTE: caching of the log output is required here because socket events that call callback | ||
// functions in here hold on to old scope values | ||
let contents = ''; | ||
|
||
const logCallback = ({ log }: DependenciesInstallLogPayloadType) => { | ||
setOutput((old) => [...old, log]); | ||
contents += log.data; | ||
}; | ||
channel.on('dependencies:install:log', logCallback); | ||
|
||
const statusCallback = ({ status }: DependenciesInstallStatusPayloadType) => { | ||
channel.off('dependencies:install:log', logCallback); | ||
channel.off('dependencies:install:status', statusCallback); | ||
setStatus(status); | ||
|
||
if (status === 'failed') { | ||
addError({ type: 'npm_install_error', contents }); | ||
} | ||
}; | ||
channel.on('dependencies:install:status', statusCallback); | ||
|
||
setOutput([]); | ||
setStatus('installing'); | ||
channel.push('dependencies:install', { packages }); | ||
}, | ||
[channel, addError], | ||
); | ||
|
||
const context: PackageJsonContextValue = { | ||
npmInstall, | ||
status, | ||
installing: status === 'installing', | ||
failed: status === 'failed', | ||
output, | ||
}; | ||
|
||
return <PackageJsonContext.Provider value={context}>{children}</PackageJsonContext.Provider>; | ||
} | ||
|
||
export function usePackageJson() { | ||
const context = useContext(PackageJsonContext); | ||
|
||
if (!context) { | ||
throw new Error('usePackageJson must be used within a PackageJsonProvider'); | ||
} | ||
|
||
return context; | ||
} |
This file contains 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 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