storybook/scripts/utils/workspace.ts

27 lines
822 B
TypeScript
Raw Normal View History

2022-10-03 17:16:39 +11:00
import memoize from 'memoizerific';
2022-10-06 13:38:04 +11:00
import { resolve } from 'path';
2022-11-24 20:13:28 +01:00
import { execaCommand } from './exec';
2022-10-03 17:16:39 +11:00
export type Workspace = { name: string; location: string };
2022-10-06 13:38:04 +11:00
const codeDir = resolve(__dirname, '../../code');
2022-10-03 17:16:39 +11:00
async function getWorkspaces() {
const { stdout } = await execaCommand('yarn workspaces list --json', {
2022-10-06 13:38:04 +11:00
cwd: codeDir,
2022-10-03 17:16:39 +11:00
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;
}