From 89a2493bed01439301469f5306ce4aa3c68f0fad Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Tue, 1 Oct 2024 04:01:12 +0530 Subject: [PATCH 1/6] test: add ReplacementsSchema --- lib/data/replacements.json | 1 + tools/schemas/replacements-schema.json | 61 ++++++++++++++++++++++++++ tools/schemas/schema.ts | 44 ++++++++++++++++++- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 tools/schemas/replacements-schema.json diff --git a/lib/data/replacements.json b/lib/data/replacements.json index 9f650fa4773c44..e2f3336b10b2d1 100644 --- a/lib/data/replacements.json +++ b/lib/data/replacements.json @@ -1,4 +1,5 @@ { + "$schema": "../../tools/schemas/replacements-schema.json", "all": { "description": "Apply crowd-sourced package replacement rules.", "extends": [ diff --git a/tools/schemas/replacements-schema.json b/tools/schemas/replacements-schema.json new file mode 100644 index 00000000000000..ca585348c475e3 --- /dev/null +++ b/tools/schemas/replacements-schema.json @@ -0,0 +1,61 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "all": { + "type": "object", + "properties": { + "description": { "type": "string" }, + "extends": { + "type": "array", + "items": { "type": "string" } + }, + "ignoreDeps": { + "type": "array", + "items": { "type": "string" } + } + }, + "required": ["description", "extends"] + } + }, + "patternProperties": { + "^[a-zA-Z0-9-]+$": { + "type": "object", + "properties": { + "description": { "type": "string" }, + "packageRules": { + "type": "array", + "items": { + "type": "object", + "properties": { + "matchCurrentVersion": { "type": "string" }, + "matchDatasources": { + "type": "array", + "items": { "type": "string" } + }, + "matchPackageNames": { + "type": "array", + "items": { "type": "string" } + }, + "replacementName": { "type": "string" }, + "replacementVersion": { "type": "string" }, + "description": { "type": "string" }, + "replacementNameTemplate": { "type": "string" } + }, + "required": ["matchDatasources", "matchPackageNames"] + }, + "contains": { + "type": "object", + "oneOf": [ + { "required": ["replacementName"] }, + { "required": ["replacementNameTemplate"] } + ] + }, + "minItems": 1 + } + }, + "required": ["description", "packageRules"] + } + }, + "additionalProperties": false +} diff --git a/tools/schemas/schema.ts b/tools/schemas/schema.ts index b5361a2472824a..3c3610f71ae6e7 100644 --- a/tools/schemas/schema.ts +++ b/tools/schemas/schema.ts @@ -11,4 +11,46 @@ const MonorepoSchema = z.object({ patternGroups: UrlSchema, }); -export { MonorepoSchema }; +const PackageRuleSchema = z.object({ + matchCurrentVersion: z.string().optional(), + matchDatasources: z.array(z.string()), + matchPackageNames: z.array(z.string()), + replacementName: z.string().optional(), + replacementVersion: z.string().optional(), + description: z.string().optional(), + replacementNameTemplate: z.string().optional(), +}); + +const RuleSetSchema = z.object({ + description: z.string(), + packageRules: z + .array(PackageRuleSchema) + .min(1) + .refine( + (rules) => + rules.some( + (rule) => + rule.replacementName !== undefined || + rule.replacementNameTemplate !== undefined, + ), + { + message: + 'At least one package rule must have either replacementName or replacementNameTemplate', + }, + ), +}); + +const AllSchema = z.object({ + description: z.string(), + extends: z.array(z.string()), + ignoreDeps: z.array(z.string()).optional(), +}); + +const ReplacementsSchema = z + .object({ + $schema: z.string(), + all: AllSchema, + }) + .catchall(RuleSetSchema); + +export { MonorepoSchema, ReplacementsSchema }; From 56cfaa04b023a25b2b01a419b669313b9f3ec9b7 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Fri, 4 Oct 2024 04:03:44 +0530 Subject: [PATCH 2/6] fix type issue --- lib/config/presets/internal/replacements.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/config/presets/internal/replacements.ts b/lib/config/presets/internal/replacements.ts index 80b6d569c564c2..b717085e9af007 100644 --- a/lib/config/presets/internal/replacements.ts +++ b/lib/config/presets/internal/replacements.ts @@ -3,8 +3,10 @@ import type { Preset } from '../types'; import type { PresetTemplate, Replacement } from './auto-generate-replacements'; import { addPresets } from './auto-generate-replacements'; +const { $schema, ...tempPresets } = replacementGroups; + /* eslint sort-keys: ["error", "asc", {"caseSensitive": false, "natural": true}] */ -export const presets: Record = replacementGroups; +export const presets: Record = tempPresets; const muiReplacement: Replacement[] = [ [['@material-ui/codemod'], '@mui/codemod'], From c94bd44b0f6d0677ecceafdbbb06a7fab20543bc Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Sun, 6 Oct 2024 23:28:55 +0530 Subject: [PATCH 3/6] Update tools/schemas/replacements-schema.json Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> --- tools/schemas/replacements-schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/schemas/replacements-schema.json b/tools/schemas/replacements-schema.json index ca585348c475e3..7634f93c9e03ba 100644 --- a/tools/schemas/replacements-schema.json +++ b/tools/schemas/replacements-schema.json @@ -1,5 +1,5 @@ { - "$schema": "http://json-schema.org/draft-04/schema#", + "$schema": "https://json-schema.org/draft-04/schema#", "type": "object", "properties": { "all": { From b4dfb7027a2ed7000f6f203bc164196a4503c72b Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Sun, 6 Oct 2024 23:29:06 +0530 Subject: [PATCH 4/6] Update tools/schemas/schema.ts Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> --- tools/schemas/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/schemas/schema.ts b/tools/schemas/schema.ts index 3c3610f71ae6e7..9061757692cfa4 100644 --- a/tools/schemas/schema.ts +++ b/tools/schemas/schema.ts @@ -35,7 +35,7 @@ const RuleSetSchema = z.object({ ), { message: - 'At least one package rule must have either replacementName or replacementNameTemplate', + 'At least one package rule must use either the replacementName config option, or the replacementNameTemplate config option', }, ), }); From ed92426c87895a46e26d8043079f2a277fa92729 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Sun, 6 Oct 2024 23:31:06 +0530 Subject: [PATCH 5/6] refactor --- lib/config/presets/internal/replacements.ts | 6 +++--- tools/schemas/monorepo-schema.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/config/presets/internal/replacements.ts b/lib/config/presets/internal/replacements.ts index b717085e9af007..b2b91debd98331 100644 --- a/lib/config/presets/internal/replacements.ts +++ b/lib/config/presets/internal/replacements.ts @@ -1,12 +1,12 @@ -import replacementGroups from '../../../data/replacements.json'; +import replacementGroupsJson from '../../../data/replacements.json'; import type { Preset } from '../types'; import type { PresetTemplate, Replacement } from './auto-generate-replacements'; import { addPresets } from './auto-generate-replacements'; -const { $schema, ...tempPresets } = replacementGroups; +const { $schema, ...replacementPresets } = replacementGroupsJson; /* eslint sort-keys: ["error", "asc", {"caseSensitive": false, "natural": true}] */ -export const presets: Record = tempPresets; +export const presets: Record = replacementPresets; const muiReplacement: Replacement[] = [ [['@material-ui/codemod'], '@mui/codemod'], diff --git a/tools/schemas/monorepo-schema.json b/tools/schemas/monorepo-schema.json index 6bc1d6e7a9fabd..7effc4dce2c72f 100644 --- a/tools/schemas/monorepo-schema.json +++ b/tools/schemas/monorepo-schema.json @@ -1,5 +1,5 @@ { - "$schema": "http://json-schema.org/draft-04/schema#", + "$schema": "https://json-schema.org/draft-04/schema#", "type": "object", "properties": { "repoGroups": { From acf73ce4dce28cf0b1dcccb2276336b8e44c7519 Mon Sep 17 00:00:00 2001 From: Rahul Gautam Singh Date: Mon, 7 Oct 2024 22:23:24 +0530 Subject: [PATCH 6/6] use individual exports --- tools/schemas/schema.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/schemas/schema.ts b/tools/schemas/schema.ts index 9061757692cfa4..8609e3582e81dc 100644 --- a/tools/schemas/schema.ts +++ b/tools/schemas/schema.ts @@ -5,7 +5,7 @@ const UrlSchema = z.record( z.union([z.string(), z.array(z.string())]), ); -const MonorepoSchema = z.object({ +export const MonorepoSchema = z.object({ repoGroups: UrlSchema, orgGroups: UrlSchema, patternGroups: UrlSchema, @@ -46,11 +46,9 @@ const AllSchema = z.object({ ignoreDeps: z.array(z.string()).optional(), }); -const ReplacementsSchema = z +export const ReplacementsSchema = z .object({ $schema: z.string(), all: AllSchema, }) .catchall(RuleSetSchema); - -export { MonorepoSchema, ReplacementsSchema };