storybook/scripts/utils/workspace.ts
Tom Coleman d3fa0bef43 Use execa 6.x in scripts
We need to use an obtuse method to import it as it is ESM only and `ts-node` doesn't play well with that.

See https://github.com/TypeStrong/ts-node/discussions/1290
2022-11-05 21:12:31 +11:00

27 lines
822 B
TypeScript

import { execaCommand } from './exec';
import memoize from 'memoizerific';
import { resolve } from 'path';
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;
}