Skip to content

Commit

Permalink
feat: list workspaces without yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Feb 18, 2025
1 parent 2d876b7 commit 191b45f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
35 changes: 10 additions & 25 deletions packages/agoric-cli/scripts/get-sdk-package-names.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
#! /usr/bin/env node
// @ts-check
/* eslint-env node */
import { spawn } from 'child_process';
import { execFileSync } from 'child_process';
import { basename } from 'path';
import { listWorkspaces } from '../src/lib/packageManager.js';

const ps = spawn('yarn', ['workspaces', '--silent', 'info'], {
stdio: ['ignore', 'pipe', 'inherit'],
shell: true,
});
const workspaces = listWorkspaces({ execFileSync });

// Get Buffers of output.
const chunks = [];
ps.stdout.on('data', data => chunks.push(data));
const packageNames = workspaces.map(w => w.name);

// Wait for the process to exit.
ps.on('close', code => {
if (code !== 0) {
throw Error(`yarn info exited with code ${code}`);
}

// Get the output.
const json = Buffer.concat(chunks).toString('utf8');
// process.stderr.write(json);

// Write the module.
const workspaces = Object.keys(JSON.parse(json)).sort();
process.stdout.write(`\
// Write the module.
process.stdout.write(`\
// DO NOT EDIT - automatically generated by ${basename(
new URL(import.meta.url).pathname,
)}
new URL(import.meta.url).pathname,
)}
// prettier-ignore
export default ${JSON.stringify(workspaces, null, 2)};
export default ${JSON.stringify(packageNames.sort(), null, 2)};
`);
});
14 changes: 3 additions & 11 deletions packages/agoric-cli/src/install.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-env node */
import path from 'path';
import chalk from 'chalk';
import { execFileSync } from 'child_process';
import { makePspawn } from './helpers.js';
import DEFAULT_SDK_PACKAGE_NAMES from './sdk-package-names.js';
import { listWorkspaces } from './lib/packageManager.js';

const REQUIRED_AGORIC_START_PACKAGES = [
'@agoric/solo',
Expand Down Expand Up @@ -30,17 +32,7 @@ export default async function installMain(progname, rawArgs, powers, opts) {
const rimraf = file => pspawn('rm', ['-rf', file]);

async function getWorktreePackagePaths(cwd = '.', map = new Map()) {
// run `yarn workspaces info` to get the list of directories to
// use, instead of a hard-coded list
const p = pspawn('yarn', ['workspaces', '--silent', 'info'], {
cwd,
stdio: ['inherit', 'pipe', 'inherit'],
});
const stdout = [];
p.childProcess.stdout?.on('data', out => stdout.push(out));
await p;
const d = JSON.parse(Buffer.concat(stdout).toString('utf-8'));
for (const [name, { location }] of Object.entries(d)) {
for (const { name, location } of listWorkspaces({ execFileSync })) {
map.set(name, path.resolve(cwd, location));
}
return map;
Expand Down
22 changes: 22 additions & 0 deletions packages/agoric-cli/src/lib/packageManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-check

/**
* @import { execFileSync } from 'child_process';
*/

/**
* Omits the root
*
* @param {{ execFileSync: execFileSync }} io
* @returns {Array<{ location: string, name: string }>}
*/
export const listWorkspaces = ({ execFileSync }) => {
const out = execFileSync('npm', ['query', '.workspace'], {
stdio: ['ignore', 'pipe', 'inherit'],
shell: true,
encoding: 'utf-8',
});
/** @type {Array<{ location: string, name: string, description: string }>} */
const result = JSON.parse(out);
return result.filter(({ location }) => location !== '.');
};
10 changes: 2 additions & 8 deletions scripts/check-untested-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
import fs from 'fs';
import path from 'path';
import { execFileSync } from 'child_process';
import { listWorkspaces } from '../packages/agoric-cli/src/lib/packageManager.js';

const parent = new URL('..', import.meta.url).pathname;
const yarnCmd = ['yarn', '--silent', 'workspaces', 'info'];
console.log('Getting', yarnCmd.join(' '));
const workspacesInfo = execFileSync(yarnCmd[0], yarnCmd.slice(1), {
cwd: parent,
encoding: 'utf8',
});
const workspacesInfoJson = JSON.parse(workspacesInfo);

const testYaml = path.resolve(
parent,
Expand All @@ -22,7 +16,7 @@ const testYamlContent = fs.readFileSync(testYaml, 'utf-8');

console.log('Searching for "cd <location> && yarn test"...');
let status = 0;
for (const [pkg, { location }] of Object.entries(workspacesInfoJson)) {
for (const { name: pkg, location } of listWorkspaces({ execFileSync })) {
const cmd = `cd ${location} && yarn \${{ steps.vars.outputs.test }}`;
if (!testYamlContent.includes(cmd)) {
console.error(`Cannot find ${location} (${pkg})`);
Expand Down

0 comments on commit 191b45f

Please sign in to comment.