-
Notifications
You must be signed in to change notification settings - Fork 5
Overall refactor to file structures and types, functions, constants usage #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
1367a54
4b02aea
d2f79b5
0c0360b
4f7372f
94bc8f8
da22044
a0c131d
e032632
f1d82c4
96e37e2
50234d1
a73b669
00201bb
e22397b
5b6e27e
a585773
c63da0f
f780e89
8ca2058
685753f
6415e63
ff95562
85d3f08
3b4b45a
1e4e43c
3085872
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "trailingComma": "all", | ||
| "tabWidth": 2, | ||
| "semi": true, | ||
| "singleQuote": false | ||
| } |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,8 @@ | |
| "build:dev": "builder build plugin --no-minify", | ||
| "clean": "rimraf bundles", | ||
| "test": "yarn build && vitest run", | ||
| "test:watch": "yarn build && vitest watch" | ||
| "test:watch": "yarn build && vitest watch", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "packageManager": "[email protected]" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * Error thrown when .yarnrc.yml#catalogs is invalid or missing | ||
| */ | ||
| export class CatalogConfigurationError extends Error { | ||
| constructor(message: string, public readonly code: string) { | ||
| super(message); | ||
| this.name = "CatalogConfigurationError"; | ||
| } | ||
|
|
||
| static FILE_NOT_FOUND = "FILE_NOT_FOUND"; | ||
| static INVALID_FORMAT = "INVALID_FORMAT"; | ||
| static INVALID_ALIAS = "INVALID_ALIAS"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export * from "./types"; | ||
| export * from "./errors"; | ||
| export * from "./reader"; | ||
|
|
||
| // Create a singleton instance of our configuration reader | ||
| import { CatalogConfigurationReader } from "./reader"; | ||
| export const configReader = new CatalogConfigurationReader(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { ValidationLevel } from "../types"; | ||
|
|
||
| declare module "@yarnpkg/core" { | ||
| interface ConfigurationValueMap { | ||
| catalogs?: CatalogsConfiguration; | ||
| } | ||
| } | ||
|
|
||
| export type ValidationConfig = | ||
| | ValidationLevel | ||
| | { [groupName: string]: ValidationLevel }; | ||
|
|
||
| /** | ||
| * Configuration structure for .yarnrc.yml#catalogs | ||
| */ | ||
| export interface CatalogsConfiguration { | ||
| options?: { | ||
| /** | ||
| * The default alias group to be used when no group is specified when adding a dependency | ||
| * - if list of alias groups, it will be used in order | ||
| * - if 'max', the most frequently used alias group will be used | ||
| */ | ||
| default?: string[] | "max"; | ||
| /** | ||
| * List of workspaces to ignore | ||
| */ | ||
| ignoredWorkspaces?: string[]; | ||
| /** | ||
| * Validation level for catalog usage | ||
| * - 'warn': Show warnings when catalog versions are not used (default) | ||
| * - 'strict': Throw errors when catalog versions are not used | ||
| * - 'off': Disable validation | ||
| * Can also be an object with group-specific settings: { [groupName]: 'warn' | 'strict' | 'off' } | ||
| */ | ||
| validation?: ValidationConfig; | ||
| }; | ||
| list?: { | ||
| [alias: string]: | ||
| | { | ||
| [packageName: string]: string; | ||
| } | ||
| | string; | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export const ROOT_ALIAS_GROUP = "root"; | ||
|
|
||
| export const CATALOG_PROTOCOL = "catalog:"; |
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { Descriptor, Workspace } from "@yarnpkg/core"; | ||
| import chalk from "chalk"; | ||
| import { configReader } from "./configuration"; | ||
| import { getCatalogProtocolUsability } from "./get-catalog-protocol-usability"; | ||
| import { CATALOG_PROTOCOL, ROOT_ALIAS_GROUP } from "./constants"; | ||
|
|
||
| export async function fallbackDefaultAliasGroup( | ||
| workspace: Workspace, | ||
| dependency: Descriptor, | ||
| ) { | ||
| if (dependency.range.startsWith(CATALOG_PROTOCOL)) { | ||
| if (await configReader.shouldIgnoreWorkspace(workspace)) { | ||
| throw new Error( | ||
| chalk.red( | ||
| `The workspace is ignored from the catalogs, but the dependency to add is using the catalog protocol. Consider removing the protocol.`, | ||
| ), | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const validationInfo = await getCatalogProtocolUsability( | ||
| workspace, | ||
| dependency, | ||
| ); | ||
|
|
||
| // If no applicable groups found, return early | ||
| if (!validationInfo) return; | ||
|
|
||
| const { validationLevel, applicableGroups } = validationInfo; | ||
|
|
||
| // If there's a default alias group, fallback to it | ||
| const defaultAliasGroups = await configReader.getDefaultAliasGroups( | ||
| workspace, | ||
| ); | ||
| if (defaultAliasGroups.length > 0) { | ||
| for (const aliasGroup of defaultAliasGroups) { | ||
| if (applicableGroups.includes(aliasGroup)) { | ||
| dependency.range = `${CATALOG_PROTOCOL}${aliasGroup}`; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If no default alias group is specified, show warning message | ||
| const aliasGroups = applicableGroups.map((groupName) => | ||
| groupName === ROOT_ALIAS_GROUP ? "" : groupName, | ||
| ); | ||
|
|
||
| const aliasGroupsText = | ||
| aliasGroups.filter((aliasGroup) => aliasGroup !== "").length > 0 | ||
| ? ` (${aliasGroups.join(", ")})` | ||
| : ""; | ||
|
|
||
| const message = `➤ ${dependency.name} is listed in the catalogs config${aliasGroupsText}, but it seems you're adding it without the catalog protocol. Consider running 'yarn add ${dependency.name}@${CATALOG_PROTOCOL}${aliasGroups[0]}' instead.`; | ||
| if (validationLevel === "strict") { | ||
| throw new Error(chalk.red(message)); | ||
| } else if (validationLevel === "warn") { | ||
| console.warn(chalk.yellow(message)); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.