Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions scripts/lint-teams-json.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { readJsonFile } from '@metamask/utils/node';
import { getPluginConfiguration } from '@yarnpkg/cli';
import { Configuration, Project, structUtils } from '@yarnpkg/core';
import { ppath } from '@yarnpkg/fslib';
import execa from 'execa';
import path from 'path';

type Workspace = {
location: string;
name: string;
};

// Run the script immediately.
main().catch(console.error);

/**
Expand All @@ -16,17 +20,9 @@ main().catch(console.error);
async function main() {
const releaseableWorkspaces = await getPublicWorkspaces();
const releaseablePackageNames = releaseableWorkspaces.map((workspace) => {
const packageName = workspace.manifest.name;
if (packageName === null) {
throw new Error(
`${structUtils.stringifyIdent(
workspace.anchoredDescriptor,
)} has no name in its manifest`,
);
}
// The package names in teams.json omit the leading "@", so we do that here
// too in order to be consistent
return structUtils.stringifyIdent(packageName).slice(1);
return workspace.name.slice(1);
});

const teams = await readJsonFile<Record<string, string>>(
Expand All @@ -52,18 +48,19 @@ async function main() {
}

/**
* Uses the Yarn API to gather the Yarn workspaces inside of this project (the
* packages that are matched by the `workspaces` field inside of
* Uses the `yarn` executable to gather the Yarn workspaces inside of this
* project (the packages that are matched by the `workspaces` field inside of
* `package.json`).
*
* @returns The list of workspaces.
*/
async function getPublicWorkspaces() {
const cwd = ppath.resolve('..', ppath.cwd());
const configuration = await Configuration.find(cwd, getPluginConfiguration());
const { project } = await Project.find(configuration, cwd);
async function getPublicWorkspaces(): Promise<Workspace[]> {
const { stdout } = await execa('yarn', [
'workspaces',
'list',
'--json',
'--no-private',
]);

return project.workspaces.filter((workspace) => {
return !workspace.manifest.private;
});
return stdout.split('\n').map((line) => JSON.parse(line));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Trailing Newlines Break JSON Parsing

The getPublicWorkspaces function splits yarn workspaces list stdout by newlines and parses each line as JSON. Command-line tools often output trailing newlines or empty lines, which causes JSON.parse('') to throw a SyntaxError and crash the script. This is a regression in error handling.

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose so. We use this in other scripts though and it hasn't been a problem there, so I'm okay with this.

}
Loading