Skip to content

Commit

Permalink
refactor: use Yarn API directly
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Dec 22, 2023
1 parent fc45fdf commit cec543f
Show file tree
Hide file tree
Showing 5 changed files with 1,219 additions and 43 deletions.
2 changes: 2 additions & 0 deletions packages/agoric-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"@endo/patterns": "^1.0.1",
"@endo/promise-kit": "^1.0.1",
"@iarna/toml": "^2.2.3",
"@yarnpkg/cli": "3.7.0",
"@yarnpkg/core": "3.5.4",
"anylogger": "^0.21.0",
"chalk": "^5.2.0",
"commander": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/agoric-cli/scripts/get-sdk-package-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { basename } from 'path';
import { listWorkspaces } from '../src/lib/yarn.js';

const workspaces = listWorkspaces();
const workspaces = await listWorkspaces();

const packageNames = workspaces.map(w => w.name);

Expand Down
3 changes: 2 additions & 1 deletion packages/agoric-cli/src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default async function installMain(progname, rawArgs, powers, opts) {
const rimraf = file => pspawn('rm', ['-rf', file]);

async function getWorktreePackagePaths(cwd = '.', map = new Map()) {
for (const { name, location } of listWorkspaces()) {
const workspaces = await listWorkspaces();
for (const { name, location } of workspaces) {
map.set(name, path.resolve(cwd, location));
}
return map;
Expand Down
52 changes: 33 additions & 19 deletions packages/agoric-cli/src/lib/yarn.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
// @ts-check
import { execFileSync } from 'child_process';
import process from 'node:process';

import { getPluginConfiguration } from '@yarnpkg/cli';
import { Configuration, Project } from '@yarnpkg/core';

const pluginConf = getPluginConfiguration();

/**
* Omits the root
*
* @returns {Array<{ location: string, name: string }>}
* @returns {Promise<Array<{ location: string, name: string }>>} e.g. {"location":"packages/zoe","name":"@agoric/zoe"}
*/
export const listWorkspaces = () => {
const ndjson = execFileSync('yarn', ['workspaces', 'list', '--json'], {
stdio: ['ignore', 'pipe', 'inherit'],
shell: true,
encoding: 'utf-8',
});
return ndjson
.trim()
.split('\n')
.map(line => {
try {
return JSON.parse(line);
} catch (e) {
throw new Error(`Could not parse '${line}'`);
}
})
.filter(({ location }) => location !== '.');
export const listWorkspaces = async () => {
const cwd = process.cwd();
const configuration = await Configuration.find(
// @ts-expect-error not a PortablePath
cwd,
pluginConf,
);
const { project } = await Project.find(
configuration,
configuration.startingCwd,
);
const { workspacesByIdent } = project;
const records = [];
for (const entry of workspacesByIdent.entries()) {
const [_, workspace] = entry;
const { locator, relativeCwd } = workspace;
// omit root
if (relativeCwd === '.') {
continue;
}
records.push({
location: relativeCwd,
name: locator.scope ? `@${locator.scope}/${locator.name}` : locator.name,
});
}
return records;
};
Loading

0 comments on commit cec543f

Please sign in to comment.