|
1 | | -import { dirname } from 'path'; |
2 | | -import type { SupportedFrameworks, SupportedRenderers } from './project_types'; |
| 1 | +import { dirname, join } from 'path'; |
| 2 | + |
| 3 | +// @ts-expect-error (has no typings) |
| 4 | +import downloadTarball from 'download-tarball'; |
| 5 | +import getNpmTarballUrl from 'get-npm-tarball-url'; |
| 6 | +import * as tempy from 'tempy'; |
| 7 | + |
3 | 8 | import { externalFrameworks } from './project_types'; |
| 9 | +import type { SupportedFrameworks, SupportedRenderers } from './project_types'; |
| 10 | +import type { JsPackageManager } from './js-package-manager'; |
| 11 | +import versions from './versions'; |
4 | 12 |
|
5 | 13 | export function getCliDir() { |
6 | 14 | return dirname(require.resolve('@storybook/cli/package.json')); |
7 | 15 | } |
8 | 16 |
|
9 | | -export function getRendererDir(renderer: SupportedFrameworks | SupportedRenderers) { |
| 17 | +const resolveUsingBranchInstall = async (packageManager: JsPackageManager, request: string) => { |
| 18 | + const tempDirectory = tempy.directory(); |
| 19 | + const name = request as keyof typeof versions; |
| 20 | + |
| 21 | + // FIXME: this might not be the right version for community packages |
| 22 | + const version = versions[name] || (await packageManager.latestVersion(request)); |
| 23 | + |
| 24 | + const url = getNpmTarballUrl(request, version); |
| 25 | + |
| 26 | + // this unzips the tarball into the temp directory |
| 27 | + await downloadTarball({ url, dir: tempDirectory }); |
| 28 | + |
| 29 | + return join(tempDirectory, 'package'); |
| 30 | +}; |
| 31 | + |
| 32 | +export async function getRendererDir( |
| 33 | + packageManager: JsPackageManager, |
| 34 | + renderer: SupportedFrameworks | SupportedRenderers |
| 35 | +) { |
10 | 36 | const externalFramework = externalFrameworks.find((framework) => framework.name === renderer); |
11 | 37 | const frameworkPackageName = |
12 | 38 | externalFramework?.renderer || externalFramework?.packageName || `@storybook/${renderer}`; |
13 | | - return dirname( |
14 | | - require.resolve(`${frameworkPackageName}/package.json`, { |
15 | | - paths: [process.cwd()], |
16 | | - }) |
17 | | - ); |
| 39 | + |
| 40 | + const packageJsonPath = `${frameworkPackageName}/package.json`; |
| 41 | + |
| 42 | + const errors: Error[] = []; |
| 43 | + |
| 44 | + try { |
| 45 | + return dirname( |
| 46 | + require.resolve(packageJsonPath, { |
| 47 | + paths: [process.cwd()], |
| 48 | + }) |
| 49 | + ); |
| 50 | + } catch (e) { |
| 51 | + errors.push(e); |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + return await resolveUsingBranchInstall(packageManager, frameworkPackageName); |
| 56 | + } catch (e) { |
| 57 | + errors.push(e); |
| 58 | + } |
| 59 | + |
| 60 | + throw new Error(`Cannot find ${packageJsonPath}, ${errors.map((e) => e.stack).join('\n\n')}`); |
18 | 61 | } |
0 commit comments