mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-21 05:02:39 +08:00
27 lines
822 B
TypeScript
27 lines
822 B
TypeScript
import memoize from 'memoizerific';
|
|
import { resolve } from 'path';
|
|
import { execaCommand } from './exec';
|
|
|
|
export type Workspace = { name: string; location: string };
|
|
|
|
const codeDir = resolve(__dirname, '../../code');
|
|
|
|
async function getWorkspaces() {
|
|
const { stdout } = await execaCommand('yarn workspaces list --json', {
|
|
cwd: codeDir,
|
|
shell: true,
|
|
});
|
|
return JSON.parse(`[${stdout.split('\n').join(',')}]`) as Workspace[];
|
|
}
|
|
|
|
const getWorkspacesMemo = memoize(1)(getWorkspaces);
|
|
|
|
export async function workspacePath(type: string, packageName: string) {
|
|
const workspaces = await getWorkspacesMemo();
|
|
const workspace = workspaces.find((w) => w.name === packageName);
|
|
if (!workspace) {
|
|
throw new Error(`Unknown ${type} '${packageName}', not in yarn workspace!`);
|
|
}
|
|
return workspace.location;
|
|
}
|