Skip to content

Commit

Permalink
Add support for single char packages in yarn berry (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: Dieter Funk <[email protected]>
  • Loading branch information
VasuLief and Dieter Funk authored Oct 2, 2024
1 parent 8dfcf40 commit 7a5335c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/parseYarnDescriptor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* A regular expression for parsing a yarn berry descriptor.
*/
const PARSE_REGEX = /(^@?[^/]+?\/?[^@/]+?)@(?:.*:)*(.+)/;
const PARSE_REGEX = /(?<packageName>(^@?[^/]+?\/)?[^@/]+?)@(?:.*:)*(?<version>.+)/;

/**
* @typedef {Object} ParsedDescriptor
Expand Down Expand Up @@ -39,7 +39,8 @@ function parseYarnDescriptor(descriptor) {
if (!result) {
throw new Error(`Unable to parse descriptor: ${descriptor}`);
}
const [, packageName, version] = result;

const { packageName, version } = result.groups;
return { packageName, version };
}

Expand Down
7 changes: 7 additions & 0 deletions test/parse-yarn-descriptor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ describe('parse-yarn-descriptor', () => {
});
});

it('should parse a single char yarn berry descriptor with protocol', () => {
expect(parseYarnDescriptor('q@npm:1.0.0')).toEqual({
packageName: 'q',
version: '1.0.0',
});
});

it('should throw an error if the descriptor is invalid', () => {
expect(() => parseYarnDescriptor('invalid')).toThrow();
});
Expand Down

0 comments on commit 7a5335c

Please sign in to comment.