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() {
|
2022-11-05 21:12:31 +11:00
|
|
|
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;
|
|
|
|
}
|