repopo 0.7.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/CHANGELOG.md +217 -6
  2. package/THIRD-PARTY-LICENSES.txt +152 -5
  3. package/esm/adapters/fluidFramework.d.ts +103 -0
  4. package/esm/adapters/fluidFramework.d.ts.map +1 -0
  5. package/esm/adapters/fluidFramework.js +124 -0
  6. package/esm/adapters/fluidFramework.js.map +1 -0
  7. package/esm/baseCommand.d.ts +1 -0
  8. package/esm/baseCommand.d.ts.map +1 -1
  9. package/esm/baseCommand.js +2 -0
  10. package/esm/baseCommand.js.map +1 -1
  11. package/esm/commands/check.d.ts.map +1 -1
  12. package/esm/commands/check.js +76 -50
  13. package/esm/commands/check.js.map +1 -1
  14. package/esm/commands/list.d.ts.map +1 -1
  15. package/esm/commands/list.js +3 -1
  16. package/esm/commands/list.js.map +1 -1
  17. package/esm/generators.d.ts +30 -2
  18. package/esm/generators.d.ts.map +1 -1
  19. package/esm/generators.js +31 -1
  20. package/esm/generators.js.map +1 -1
  21. package/esm/index.d.ts +14 -3
  22. package/esm/index.d.ts.map +1 -1
  23. package/esm/index.js +13 -2
  24. package/esm/index.js.map +1 -1
  25. package/esm/makePolicy.d.ts +31 -2
  26. package/esm/makePolicy.d.ts.map +1 -1
  27. package/esm/makePolicy.js +27 -13
  28. package/esm/makePolicy.js.map +1 -1
  29. package/esm/perf.d.ts +6 -2
  30. package/esm/perf.d.ts.map +1 -1
  31. package/esm/perf.js +5 -4
  32. package/esm/perf.js.map +1 -1
  33. package/esm/policies/HtmlFileHeaders.d.ts.map +1 -1
  34. package/esm/policies/HtmlFileHeaders.js +11 -7
  35. package/esm/policies/HtmlFileHeaders.js.map +1 -1
  36. package/esm/policies/JsTsFileHeaders.d.ts.map +1 -1
  37. package/esm/policies/JsTsFileHeaders.js +11 -7
  38. package/esm/policies/JsTsFileHeaders.js.map +1 -1
  39. package/esm/policies/LicenseFileExists.d.ts.map +1 -1
  40. package/esm/policies/LicenseFileExists.js +22 -16
  41. package/esm/policies/LicenseFileExists.js.map +1 -1
  42. package/esm/policies/NoJsFileExtensions.d.ts.map +1 -1
  43. package/esm/policies/NoJsFileExtensions.js +16 -9
  44. package/esm/policies/NoJsFileExtensions.js.map +1 -1
  45. package/esm/policies/NoLargeBinaryFiles.d.ts.map +1 -1
  46. package/esm/policies/NoLargeBinaryFiles.js +35 -29
  47. package/esm/policies/NoLargeBinaryFiles.js.map +1 -1
  48. package/esm/policies/NoPrivateWorkspaceDependencies.d.ts +37 -0
  49. package/esm/policies/NoPrivateWorkspaceDependencies.d.ts.map +1 -0
  50. package/esm/policies/NoPrivateWorkspaceDependencies.js +145 -0
  51. package/esm/policies/NoPrivateWorkspaceDependencies.js.map +1 -0
  52. package/esm/policies/PackageAllowedScopes.d.ts +88 -0
  53. package/esm/policies/PackageAllowedScopes.d.ts.map +1 -0
  54. package/esm/policies/PackageAllowedScopes.js +118 -0
  55. package/esm/policies/PackageAllowedScopes.js.map +1 -0
  56. package/esm/policies/PackageEsmType.d.ts +100 -0
  57. package/esm/policies/PackageEsmType.d.ts.map +1 -0
  58. package/esm/policies/PackageEsmType.js +261 -0
  59. package/esm/policies/PackageEsmType.js.map +1 -0
  60. package/esm/policies/PackageFolderName.d.ts +97 -0
  61. package/esm/policies/PackageFolderName.d.ts.map +1 -0
  62. package/esm/policies/PackageFolderName.js +108 -0
  63. package/esm/policies/PackageFolderName.js.map +1 -0
  64. package/esm/policies/PackageJsonProperties.d.ts.map +1 -1
  65. package/esm/policies/PackageJsonProperties.js +36 -30
  66. package/esm/policies/PackageJsonProperties.js.map +1 -1
  67. package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts.map +1 -1
  68. package/esm/policies/PackageJsonRepoDirectoryProperty.js +41 -37
  69. package/esm/policies/PackageJsonRepoDirectoryProperty.js.map +1 -1
  70. package/esm/policies/PackageJsonSorted.d.ts.map +1 -1
  71. package/esm/policies/PackageJsonSorted.js +40 -28
  72. package/esm/policies/PackageJsonSorted.js.map +1 -1
  73. package/esm/policies/PackageLicense.d.ts +61 -0
  74. package/esm/policies/PackageLicense.d.ts.map +1 -0
  75. package/esm/policies/PackageLicense.js +135 -0
  76. package/esm/policies/PackageLicense.js.map +1 -0
  77. package/esm/policies/PackagePrivateField.d.ts +124 -0
  78. package/esm/policies/PackagePrivateField.d.ts.map +1 -0
  79. package/esm/policies/PackagePrivateField.js +159 -0
  80. package/esm/policies/PackagePrivateField.js.map +1 -0
  81. package/esm/policies/PackageReadme.d.ts +77 -0
  82. package/esm/policies/PackageReadme.d.ts.map +1 -0
  83. package/esm/policies/PackageReadme.js +213 -0
  84. package/esm/policies/PackageReadme.js.map +1 -0
  85. package/esm/policies/PackageScripts.d.ts +180 -2
  86. package/esm/policies/PackageScripts.d.ts.map +1 -1
  87. package/esm/policies/PackageScripts.js +358 -34
  88. package/esm/policies/PackageScripts.js.map +1 -1
  89. package/esm/policies/PackageTestScripts.d.ts +100 -0
  90. package/esm/policies/PackageTestScripts.d.ts.map +1 -0
  91. package/esm/policies/PackageTestScripts.js +155 -0
  92. package/esm/policies/PackageTestScripts.js.map +1 -0
  93. package/esm/policies/RequiredGitignorePatterns.d.ts.map +1 -1
  94. package/esm/policies/RequiredGitignorePatterns.js +30 -19
  95. package/esm/policies/RequiredGitignorePatterns.js.map +1 -1
  96. package/esm/policies.d.ts +9 -1
  97. package/esm/policies.d.ts.map +1 -1
  98. package/esm/policies.js +10 -1
  99. package/esm/policies.js.map +1 -1
  100. package/esm/policy.d.ts +37 -8
  101. package/esm/policy.d.ts.map +1 -1
  102. package/esm/policy.js +18 -9
  103. package/esm/policy.js.map +1 -1
  104. package/esm/policyDefiners/defineFileHeaderPolicy.d.ts +43 -1
  105. package/esm/policyDefiners/defineFileHeaderPolicy.d.ts.map +1 -1
  106. package/esm/policyDefiners/defineFileHeaderPolicy.js +18 -5
  107. package/esm/policyDefiners/defineFileHeaderPolicy.js.map +1 -1
  108. package/esm/policyDefiners/definePackagePolicy.d.ts +43 -2
  109. package/esm/policyDefiners/definePackagePolicy.d.ts.map +1 -1
  110. package/esm/policyDefiners/definePackagePolicy.js +41 -5
  111. package/esm/policyDefiners/definePackagePolicy.js.map +1 -1
  112. package/esm/tsdoc-metadata.json +1 -1
  113. package/esm/utils/indentation.d.ts +73 -0
  114. package/esm/utils/indentation.d.ts.map +1 -0
  115. package/esm/utils/indentation.js +97 -0
  116. package/esm/utils/indentation.js.map +1 -0
  117. package/esm/utils/packageName.d.ts +71 -0
  118. package/esm/utils/packageName.d.ts.map +1 -0
  119. package/esm/utils/packageName.js +79 -0
  120. package/esm/utils/packageName.js.map +1 -0
  121. package/oclif.manifest.json +1 -1
  122. package/package.json +33 -16
@@ -0,0 +1,261 @@
1
+ import jsonfile from "jsonfile";
2
+ import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
3
+ import { detectIndentation } from "../utils/indentation.js";
4
+ const { writeFile: writeJson } = jsonfile;
5
+ /**
6
+ * Detect module type from the exports field.
7
+ */
8
+ function detectTypeFromExports(exports) {
9
+ if (exports === undefined) {
10
+ return undefined;
11
+ }
12
+ const exportsStr = JSON.stringify(exports);
13
+ const hasEsm = exportsStr.includes(".mjs") || exportsStr.includes('"import"');
14
+ const hasCjs = exportsStr.includes(".cjs") || exportsStr.includes('"require"');
15
+ // Dual format packages have both ESM and CJS exports
16
+ if (hasEsm && hasCjs) {
17
+ return {
18
+ type: "module",
19
+ isDualFormat: true,
20
+ reason: "Package has both ESM and CommonJS exports (dual format). Defaulting to ESM.",
21
+ };
22
+ }
23
+ if (hasEsm) {
24
+ return {
25
+ type: "module",
26
+ isDualFormat: false,
27
+ reason: "Detected ESM from exports field (.mjs or import condition)",
28
+ };
29
+ }
30
+ if (hasCjs) {
31
+ return {
32
+ type: "commonjs",
33
+ isDualFormat: false,
34
+ reason: "Detected CommonJS from exports field (.cjs or require condition)",
35
+ };
36
+ }
37
+ return undefined;
38
+ }
39
+ /**
40
+ * Detect module type from the main field.
41
+ */
42
+ function detectTypeFromMain(main) {
43
+ if (typeof main !== "string") {
44
+ return undefined;
45
+ }
46
+ if (main.endsWith(".mjs")) {
47
+ return {
48
+ type: "module",
49
+ isDualFormat: false,
50
+ reason: "Detected ESM from main field (.mjs extension)",
51
+ };
52
+ }
53
+ if (main.endsWith(".cjs")) {
54
+ return {
55
+ type: "commonjs",
56
+ isDualFormat: false,
57
+ reason: "Detected CommonJS from main field (.cjs extension)",
58
+ };
59
+ }
60
+ return undefined;
61
+ }
62
+ /**
63
+ * Detect the expected type field from the package's exports or main field.
64
+ *
65
+ * @param json - The package.json content
66
+ * @returns Detection result with type, dual-format indicator, and reason
67
+ */
68
+ function detectTypeFromPackage(json) {
69
+ // Check exports field for type hints
70
+ const exportsResult = detectTypeFromExports(json.exports);
71
+ if (exportsResult !== undefined) {
72
+ return exportsResult;
73
+ }
74
+ // Check main field
75
+ const mainResult = detectTypeFromMain(json.main);
76
+ if (mainResult !== undefined) {
77
+ return mainResult;
78
+ }
79
+ // Check module field (ESM entry point)
80
+ if (json.module !== undefined) {
81
+ return {
82
+ type: "module",
83
+ isDualFormat: false,
84
+ reason: "Detected ESM from module field presence",
85
+ };
86
+ }
87
+ return {
88
+ type: undefined,
89
+ isDualFormat: false,
90
+ reason: "Could not detect module type from exports, main, or module fields",
91
+ };
92
+ }
93
+ /**
94
+ * Check if a package matches an exclusion pattern.
95
+ *
96
+ * @param packageName - The package name
97
+ * @param excludePackages - List of patterns to exclude
98
+ * @returns true if the package should be excluded
99
+ */
100
+ function isExcluded(packageName, excludePackages) {
101
+ if (excludePackages === undefined || excludePackages.length === 0) {
102
+ return false;
103
+ }
104
+ for (const pattern of excludePackages) {
105
+ // Scope matching
106
+ if (pattern.startsWith("@") && packageName.startsWith(`${pattern}/`)) {
107
+ return true;
108
+ }
109
+ // Exact match
110
+ if (packageName === pattern) {
111
+ return true;
112
+ }
113
+ }
114
+ return false;
115
+ }
116
+ /**
117
+ * Check if the package should be skipped from validation.
118
+ */
119
+ function shouldSkipPackage(packageName, config) {
120
+ // Skip packages without a name
121
+ if (packageName === undefined) {
122
+ return true;
123
+ }
124
+ // Skip the root package
125
+ if (packageName === "root") {
126
+ return true;
127
+ }
128
+ // Check exclusions
129
+ return isExcluded(packageName, config.excludePackages);
130
+ }
131
+ /**
132
+ * Determine the expected type and reason from the config and package.
133
+ */
134
+ function determineExpectedType(json, config) {
135
+ if (config.requiredType !== undefined) {
136
+ return {
137
+ expectedType: config.requiredType,
138
+ detectionReason: `Required type: ${config.requiredType}`,
139
+ };
140
+ }
141
+ if (config.detectFromExports === true) {
142
+ const detection = detectTypeFromPackage(json);
143
+ return {
144
+ expectedType: detection.type,
145
+ detectionReason: detection.reason,
146
+ };
147
+ }
148
+ return { expectedType: undefined, detectionReason: undefined };
149
+ }
150
+ /**
151
+ * Build the error message for a type mismatch.
152
+ */
153
+ function buildErrorMessage(packageName, currentType, expectedType) {
154
+ if (currentType === undefined) {
155
+ return `Package "${packageName}" is missing the "type" field. Expected: "type": "${expectedType}"`;
156
+ }
157
+ return `Package "${packageName}" has "type": "${currentType}" but expected "type": "${expectedType}"`;
158
+ }
159
+ /**
160
+ * Apply the auto-fix for the type field.
161
+ */
162
+ async function applyFix(json, file, expectedType, failResult) {
163
+ const fixResult = {
164
+ ...failResult,
165
+ resolved: false,
166
+ };
167
+ try {
168
+ json.type = expectedType;
169
+ const indent = await detectIndentation(file);
170
+ await writeJson(file, json, { spaces: indent });
171
+ fixResult.resolved = true;
172
+ }
173
+ catch {
174
+ fixResult.resolved = false;
175
+ fixResult.errorMessages = [`Failed to update ${file}`];
176
+ }
177
+ return fixResult;
178
+ }
179
+ /**
180
+ * A policy that ensures the `type` field in package.json is correctly set.
181
+ *
182
+ * This policy helps maintain consistency in how packages declare their module format,
183
+ * which is essential for proper module resolution in Node.js and bundlers.
184
+ *
185
+ * @remarks
186
+ *
187
+ * The policy can operate in two modes:
188
+ *
189
+ * 1. **Required Type**: Set `requiredType` to enforce a specific type for all packages
190
+ * 2. **Detection**: Set `detectFromExports: true` to detect the expected type from
191
+ * the package's exports or main field
192
+ *
193
+ * When `detectFromExports` is enabled, the policy looks for:
194
+ * - `.mjs` extensions or `"import"` conditions → expects `"module"`
195
+ * - `.cjs` extensions or `"require"` conditions → expects `"commonjs"`
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * import { makePolicy } from "repopo";
200
+ * import { PackageEsmType } from "repopo/policies";
201
+ *
202
+ * // Require all packages to be ESM
203
+ * makePolicy(PackageEsmType, { requiredType: "module" })
204
+ *
205
+ * // Or detect based on exports
206
+ * makePolicy(PackageEsmType, { detectFromExports: true })
207
+ * ```
208
+ *
209
+ * @alpha
210
+ */
211
+ export const PackageEsmType = definePackagePolicy({
212
+ name: "PackageEsmType",
213
+ description: "Ensures the type field in package.json correctly indicates ESM or CommonJS module format.",
214
+ handler: async (json, { file, config, resolve }) => {
215
+ // If no config provided, skip validation
216
+ if (config === undefined) {
217
+ return true;
218
+ }
219
+ const packageName = json.name;
220
+ // Check if package should be skipped
221
+ if (shouldSkipPackage(packageName, config)) {
222
+ return true;
223
+ }
224
+ // Determine the expected type
225
+ const { expectedType, detectionReason } = determineExpectedType(json, config);
226
+ // Handle case where type cannot be detected
227
+ if (expectedType === undefined) {
228
+ if (config.onDetectionFailure === "fail") {
229
+ return {
230
+ name: "PackageEsmType",
231
+ file,
232
+ autoFixable: false,
233
+ errorMessages: [
234
+ `Package "${packageName}": ${detectionReason ?? "Could not detect module type"}. Set "type" field explicitly or use "requiredType" config.`,
235
+ ],
236
+ };
237
+ }
238
+ // "skip" and "warn" both pass validation
239
+ return true;
240
+ }
241
+ // Check if current type matches expected
242
+ if (json.type === expectedType) {
243
+ return true;
244
+ }
245
+ // Build failure result
246
+ const failResult = {
247
+ name: PackageEsmType.name,
248
+ file,
249
+ autoFixable: true,
250
+ // packageName is guaranteed to be defined here due to shouldSkipPackage check
251
+ errorMessages: [
252
+ buildErrorMessage(packageName, json.type, expectedType),
253
+ ],
254
+ };
255
+ if (resolve) {
256
+ return applyFix(json, file, expectedType, failResult);
257
+ }
258
+ return failResult;
259
+ },
260
+ });
261
+ //# sourceMappingURL=PackageEsmType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PackageEsmType.js","sourceRoot":"","sources":["../../src/policies/PackageEsmType.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAGhC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;AA6F1C;;GAEG;AACH,SAAS,qBAAqB,CAC7B,OAA+B;IAE/B,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAC3C,MAAM,MAAM,GAAG,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAC9E,MAAM,MAAM,GACX,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IAEjE,qDAAqD;IACrD,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;QACtB,OAAO;YACN,IAAI,EAAE,QAAQ;YACd,YAAY,EAAE,IAAI;YAClB,MAAM,EACL,6EAA6E;SAC9E,CAAC;IACH,CAAC;IAED,IAAI,MAAM,EAAE,CAAC;QACZ,OAAO;YACN,IAAI,EAAE,QAAQ;YACd,YAAY,EAAE,KAAK;YACnB,MAAM,EAAE,4DAA4D;SACpE,CAAC;IACH,CAAC;IAED,IAAI,MAAM,EAAE,CAAC;QACZ,OAAO;YACN,IAAI,EAAE,UAAU;YAChB,YAA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AED,OAAO,UAAU,CAAC;IACnB,CAAC;CACD,CAAC,CAAC","sourcesContent":["import jsonfile from \"jsonfile\";\nimport type { PackageJson } from \"type-fest\";\nimport type { PolicyFailure, PolicyFixResult } from \"../policy.js\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\nimport { detectIndentation } from \"../utils/indentation.js\";\n\nconst { writeFile: writeJson } = jsonfile;\n\n/**\n * Configuration for the PackageEsmType policy.\n *\n * This policy ensures the `type` field in package.json is correctly set\n * to indicate whether the package is an ESM or CommonJS module.\n *\n * @example\n * ```typescript\n * const config: PackageEsmTypeConfig = {\n * // Require ESM type field for all packages\n * requiredType: \"module\",\n * // Or detect based on exports/main field\n * detectFromExports: true,\n * };\n * ```\n *\n * @alpha\n */\nexport interface PackageEsmTypeConfig {\n\t/**\n\t * The required `type` field value for all packages.\n\t *\n\t * - `\"module\"` - All packages must be ESM (use import/export)\n\t * - `\"commonjs\"` - All packages must be CommonJS (use require)\n\t * - `undefined` - Use detection logic based on exports/main\n\t *\n\t * @remarks\n\t *\n\t * If set, this overrides automatic detection. Use `detectFromExports`\n\t * for more flexible per-package type detection.\n\t */\n\trequiredType?: \"module\" | \"commonjs\";\n\n\t/**\n\t * Detect the expected `type` field from the exports or main field.\n\t *\n\t * When enabled, the policy analyzes the package's entry points:\n\t * - If exports use `.mjs` or have `import` conditions → `\"module\"`\n\t * - If exports use `.cjs` or have `require` conditions → `\"commonjs\"`\n\t * - If main field uses `.mjs` → `\"module\"`\n\t * - If main field uses `.cjs` → `\"commonjs\"`\n\t *\n\t * @defaultValue false\n\t */\n\tdetectFromExports?: boolean;\n\n\t/**\n\t * Package names or scopes to exclude from validation.\n\t *\n\t * @example\n\t * ```typescript\n\t * excludePackages: [\"@myorg/legacy-package\", \"old-cjs-lib\"]\n\t * ```\n\t */\n\texcludePackages?: string[];\n\n\t/**\n\t * Behavior when module type cannot be detected from exports/main.\n\t *\n\t * - `\"skip\"` (default) - Skip validation for packages where type cannot be detected\n\t * - `\"warn\"` - Pass validation but include a warning in the output\n\t * - `\"fail\"` - Fail validation when type cannot be detected\n\t *\n\t * This only applies when `detectFromExports` is true and no `requiredType` is set.\n\t *\n\t * @defaultValue \"skip\"\n\t */\n\tonDetectionFailure?: \"skip\" | \"warn\" | \"fail\";\n}\n\n/**\n * Result of detecting the module type from a package.\n */\ninterface DetectionResult {\n\t/**\n\t * The detected type, if any.\n\t */\n\ttype: \"module\" | \"commonjs\" | undefined;\n\n\t/**\n\t * Whether the package appears to be dual-format (both ESM and CommonJS).\n\t * When true, the `type` field should typically match the package's default format.\n\t */\n\tisDualFormat: boolean;\n\n\t/**\n\t * Human-readable reason for the detection result.\n\t */\n\treason: string;\n}\n\n/**\n * Detect module type from the exports field.\n */\nfunction detectTypeFromExports(\n\texports: PackageJson[\"exports\"],\n): DetectionResult | undefined {\n\tif (exports === undefined) {\n\t\treturn undefined;\n\t}\n\n\tconst exportsStr = JSON.stringify(exports);\n\tconst hasEsm = exportsStr.includes(\".mjs\") || exportsStr.includes('\"import\"');\n\tconst hasCjs =\n\t\texportsStr.includes(\".cjs\") || exportsStr.includes('\"require\"');\n\n\t// Dual format packages have both ESM and CJS exports\n\tif (hasEsm && hasCjs) {\n\t\treturn {\n\t\t\ttype: \"module\",\n\t\t\tisDualFormat: true,\n\t\t\treason:\n\t\t\t\t\"Package has both ESM and CommonJS exports (dual format). Defaulting to ESM.\",\n\t\t};\n\t}\n\n\tif (hasEsm) {\n\t\treturn {\n\t\t\ttype: \"module\",\n\t\t\tisDualFormat: false,\n\t\t\treason: \"Detected ESM from exports field (.mjs or import condition)\",\n\t\t};\n\t}\n\n\tif (hasCjs) {\n\t\treturn {\n\t\t\ttype: \"commonjs\",\n\t\t\tisDualFormat: false,\n\t\t\treason:\n\t\t\t\t\"Detected CommonJS from exports field (.cjs or require condition)\",\n\t\t};\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Detect module type from the main field.\n */\nfunction detectTypeFromMain(main: unknown): DetectionResult | undefined {\n\tif (typeof main !== \"string\") {\n\t\treturn undefined;\n\t}\n\n\tif (main.endsWith(\".mjs\")) {\n\t\treturn {\n\t\t\ttype: \"module\",\n\t\t\tisDualFormat: false,\n\t\t\treason: \"Detected ESM from main field (.mjs extension)\",\n\t\t};\n\t}\n\n\tif (main.endsWith(\".cjs\")) {\n\t\treturn {\n\t\t\ttype: \"commonjs\",\n\t\t\tisDualFormat: false,\n\t\t\treason: \"Detected CommonJS from main field (.cjs extension)\",\n\t\t};\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Detect the expected type field from the package's exports or main field.\n *\n * @param json - The package.json content\n * @returns Detection result with type, dual-format indicator, and reason\n */\nfunction detectTypeFromPackage(json: PackageJson): DetectionResult {\n\t// Check exports field for type hints\n\tconst exportsResult = detectTypeFromExports(json.exports);\n\tif (exportsResult !== undefined) {\n\t\treturn exportsResult;\n\t}\n\n\t// Check main field\n\tconst mainResult = detectTypeFromMain(json.main);\n\tif (mainResult !== undefined) {\n\t\treturn mainResult;\n\t}\n\n\t// Check module field (ESM entry point)\n\tif (json.module !== undefined) {\n\t\treturn {\n\t\t\ttype: \"module\",\n\t\t\tisDualFormat: false,\n\t\t\treason: \"Detected ESM from module field presence\",\n\t\t};\n\t}\n\n\treturn {\n\t\ttype: undefined,\n\t\tisDualFormat: false,\n\t\treason: \"Could not detect module type from exports, main, or module fields\",\n\t};\n}\n\n/**\n * Check if a package matches an exclusion pattern.\n *\n * @param packageName - The package name\n * @param excludePackages - List of patterns to exclude\n * @returns true if the package should be excluded\n */\nfunction isExcluded(\n\tpackageName: string,\n\texcludePackages: string[] | undefined,\n): boolean {\n\tif (excludePackages === undefined || excludePackages.length === 0) {\n\t\treturn false;\n\t}\n\n\tfor (const pattern of excludePackages) {\n\t\t// Scope matching\n\t\tif (pattern.startsWith(\"@\") && packageName.startsWith(`${pattern}/`)) {\n\t\t\treturn true;\n\t\t}\n\t\t// Exact match\n\t\tif (packageName === pattern) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n/**\n * Check if the package should be skipped from validation.\n */\nfunction shouldSkipPackage(\n\tpackageName: string | undefined,\n\tconfig: PackageEsmTypeConfig,\n): boolean {\n\t// Skip packages without a name\n\tif (packageName === undefined) {\n\t\treturn true;\n\t}\n\n\t// Skip the root package\n\tif (packageName === \"root\") {\n\t\treturn true;\n\t}\n\n\t// Check exclusions\n\treturn isExcluded(packageName, config.excludePackages);\n}\n\n/**\n * Determine the expected type and reason from the config and package.\n */\nfunction determineExpectedType(\n\tjson: PackageJson,\n\tconfig: PackageEsmTypeConfig,\n): {\n\texpectedType: \"module\" | \"commonjs\" | undefined;\n\tdetectionReason: string | undefined;\n} {\n\tif (config.requiredType !== undefined) {\n\t\treturn {\n\t\t\texpectedType: config.requiredType,\n\t\t\tdetectionReason: `Required type: ${config.requiredType}`,\n\t\t};\n\t}\n\n\tif (config.detectFromExports === true) {\n\t\tconst detection = detectTypeFromPackage(json);\n\t\treturn {\n\t\t\texpectedType: detection.type,\n\t\t\tdetectionReason: detection.reason,\n\t\t};\n\t}\n\n\treturn { expectedType: undefined, detectionReason: undefined };\n}\n\n/**\n * Build the error message for a type mismatch.\n */\nfunction buildErrorMessage(\n\tpackageName: string,\n\tcurrentType: string | undefined,\n\texpectedType: string,\n): string {\n\tif (currentType === undefined) {\n\t\treturn `Package \"${packageName}\" is missing the \"type\" field. Expected: \"type\": \"${expectedType}\"`;\n\t}\n\treturn `Package \"${packageName}\" has \"type\": \"${currentType}\" but expected \"type\": \"${expectedType}\"`;\n}\n\n/**\n * Apply the auto-fix for the type field.\n */\nasync function applyFix(\n\tjson: PackageJson,\n\tfile: string,\n\texpectedType: \"module\" | \"commonjs\",\n\tfailResult: PolicyFailure,\n): Promise<PolicyFixResult> {\n\tconst fixResult: PolicyFixResult = {\n\t\t...failResult,\n\t\tresolved: false,\n\t};\n\n\ttry {\n\t\tjson.type = expectedType;\n\t\tconst indent = await detectIndentation(file);\n\t\tawait writeJson(file, json, { spaces: indent });\n\t\tfixResult.resolved = true;\n\t} catch {\n\t\tfixResult.resolved = false;\n\t\tfixResult.errorMessages = [`Failed to update ${file}`];\n\t}\n\n\treturn fixResult;\n}\n\n/**\n * A policy that ensures the `type` field in package.json is correctly set.\n *\n * This policy helps maintain consistency in how packages declare their module format,\n * which is essential for proper module resolution in Node.js and bundlers.\n *\n * @remarks\n *\n * The policy can operate in two modes:\n *\n * 1. **Required Type**: Set `requiredType` to enforce a specific type for all packages\n * 2. **Detection**: Set `detectFromExports: true` to detect the expected type from\n * the package's exports or main field\n *\n * When `detectFromExports` is enabled, the policy looks for:\n * - `.mjs` extensions or `\"import\"` conditions → expects `\"module\"`\n * - `.cjs` extensions or `\"require\"` conditions → expects `\"commonjs\"`\n *\n * @example\n * ```typescript\n * import { makePolicy } from \"repopo\";\n * import { PackageEsmType } from \"repopo/policies\";\n *\n * // Require all packages to be ESM\n * makePolicy(PackageEsmType, { requiredType: \"module\" })\n *\n * // Or detect based on exports\n * makePolicy(PackageEsmType, { detectFromExports: true })\n * ```\n *\n * @alpha\n */\nexport const PackageEsmType = definePackagePolicy<\n\tPackageJson,\n\tPackageEsmTypeConfig | undefined\n>({\n\tname: \"PackageEsmType\",\n\tdescription:\n\t\t\"Ensures the type field in package.json correctly indicates ESM or CommonJS module format.\",\n\thandler: async (json, { file, config, resolve }) => {\n\t\t// If no config provided, skip validation\n\t\tif (config === undefined) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst packageName = json.name;\n\n\t\t// Check if package should be skipped\n\t\tif (shouldSkipPackage(packageName, config)) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Determine the expected type\n\t\tconst { expectedType, detectionReason } = determineExpectedType(\n\t\t\tjson,\n\t\t\tconfig,\n\t\t);\n\n\t\t// Handle case where type cannot be detected\n\t\tif (expectedType === undefined) {\n\t\t\tif (config.onDetectionFailure === \"fail\") {\n\t\t\t\treturn {\n\t\t\t\t\tname: \"PackageEsmType\",\n\t\t\t\t\tfile,\n\t\t\t\t\tautoFixable: false,\n\t\t\t\t\terrorMessages: [\n\t\t\t\t\t\t`Package \"${packageName}\": ${detectionReason ?? \"Could not detect module type\"}. Set \"type\" field explicitly or use \"requiredType\" config.`,\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t\t// \"skip\" and \"warn\" both pass validation\n\t\t\treturn true;\n\t\t}\n\n\t\t// Check if current type matches expected\n\t\tif (json.type === expectedType) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Build failure result\n\t\tconst failResult: PolicyFailure = {\n\t\t\tname: PackageEsmType.name,\n\t\t\tfile,\n\t\t\tautoFixable: true,\n\t\t\t// packageName is guaranteed to be defined here due to shouldSkipPackage check\n\t\t\terrorMessages: [\n\t\t\t\tbuildErrorMessage(packageName as string, json.type, expectedType),\n\t\t\t],\n\t\t};\n\n\t\tif (resolve) {\n\t\t\treturn applyFix(json, file, expectedType, failResult);\n\t\t}\n\n\t\treturn failResult;\n\t},\n});\n"]}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Configuration for the PackageFolderName policy.
3
+ *
4
+ * This policy ensures that package folder names match their package names,
5
+ * promoting consistency and making packages easier to find in the file system.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const config: PackageFolderNameConfig = {
10
+ * // Strip all scopes when matching folder names
11
+ * stripScopes: ["@*"],
12
+ * // Or strip specific scopes
13
+ * stripScopes: ["@myorg", "@internal"],
14
+ * // These packages are exempt from the check
15
+ * excludePackages: ["@myorg/special-name"],
16
+ * };
17
+ * ```
18
+ *
19
+ * @alpha
20
+ */
21
+ export interface PackageFolderNameConfig {
22
+ /**
23
+ * A list of npm scope patterns to strip when comparing folder names to package names.
24
+ *
25
+ * For scoped packages like `@myorg/my-package`, if `@myorg` matches a pattern in this list,
26
+ * the policy will expect the folder to be named `my-package` (not `@myorg/my-package`).
27
+ *
28
+ * @remarks
29
+ *
30
+ * Supports glob patterns:
31
+ * - `@*` - matches all scopes
32
+ * - `@myorg` - matches exactly `@myorg`
33
+ * - `@my*` - matches scopes starting with `@my`
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * // Strip all scopes - @anything/my-package → folder "my-package"
38
+ * stripScopes: ["@*"]
39
+ *
40
+ * // Strip specific scopes only
41
+ * stripScopes: ["@myorg", "@internal"]
42
+ *
43
+ * // Strip scopes matching a pattern
44
+ * stripScopes: ["@fluid*"]
45
+ * ```
46
+ */
47
+ stripScopes?: string[];
48
+ /**
49
+ * A list of package names that are exempt from folder name matching.
50
+ *
51
+ * Use this for packages that intentionally have different folder names,
52
+ * such as legacy packages or packages with special naming requirements.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * excludePackages: ["@myorg/cli", "legacy-package"]
57
+ * ```
58
+ */
59
+ excludePackages?: string[];
60
+ }
61
+ /**
62
+ * A policy that ensures package folder names match package names.
63
+ *
64
+ * This policy helps maintain a consistent and predictable file structure
65
+ * in monorepos by ensuring that folder names correspond to package names.
66
+ * This makes it easier to locate packages in the file system.
67
+ *
68
+ * @remarks
69
+ *
70
+ * The policy compares the immediate parent folder name of the package.json
71
+ * against the package name. For scoped packages, the scope can optionally
72
+ * be stripped based on the `stripScopes` configuration.
73
+ *
74
+ * For example:
75
+ * - `packages/my-lib/package.json` with name `my-lib` ✓
76
+ * - `packages/my-lib/package.json` with name `@myorg/my-lib` ✓ (if @myorg in stripScopes)
77
+ * - `packages/wrong-name/package.json` with name `my-lib` ✗
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * import { makePolicy } from "repopo";
82
+ * import { PackageFolderName } from "repopo/policies";
83
+ *
84
+ * const config: RepopoConfig = {
85
+ * policies: [
86
+ * makePolicy(PackageFolderName, {
87
+ * stripScopes: ["@myorg", "@internal"],
88
+ * excludePackages: ["@myorg/special-case"],
89
+ * }),
90
+ * ],
91
+ * };
92
+ * ```
93
+ *
94
+ * @alpha
95
+ */
96
+ export declare const PackageFolderName: import("../policy.js").PolicyDefinition<PackageFolderNameConfig | undefined>;
97
+ //# sourceMappingURL=PackageFolderName.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PackageFolderName.d.ts","sourceRoot":"","sources":["../../src/policies/PackageFolderName.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,uBAAuB;IACvC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IAEvB;;;;;;;;;;OAUG;IACH,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B;AAuCD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,iBAAiB,8EAqD5B,CAAC"}
@@ -0,0 +1,108 @@
1
+ import path from "pathe";
2
+ import picomatch from "picomatch";
3
+ import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
4
+ import { parsePackageName } from "../utils/packageName.js";
5
+ /**
6
+ * Check if a scope should be stripped based on the patterns.
7
+ *
8
+ * @param scope - The scope to check (e.g., `@myorg`)
9
+ * @param stripScopes - List of scope patterns to match (supports glob patterns via picomatch)
10
+ * @returns `true` if the scope matches any pattern
11
+ */
12
+ function shouldStripScope(scope, stripScopes) {
13
+ if (stripScopes.length === 0) {
14
+ return false;
15
+ }
16
+ return picomatch.isMatch(scope, stripScopes);
17
+ }
18
+ /**
19
+ * Get the expected folder name from a package name.
20
+ *
21
+ * @param packageName - The package name (e.g., `@myorg/my-package`)
22
+ * @param stripScopes - Scope patterns to strip from the package name
23
+ * @returns The expected folder name
24
+ */
25
+ function getExpectedFolderName(packageName, stripScopes) {
26
+ const { scope, unscopedName } = parsePackageName(packageName);
27
+ // Check if the package is scoped and should have scope stripped
28
+ if (scope !== undefined && shouldStripScope(scope, stripScopes)) {
29
+ return unscopedName;
30
+ }
31
+ // Return full package name for unscoped packages or scopes not in stripScopes
32
+ return packageName;
33
+ }
34
+ /**
35
+ * A policy that ensures package folder names match package names.
36
+ *
37
+ * This policy helps maintain a consistent and predictable file structure
38
+ * in monorepos by ensuring that folder names correspond to package names.
39
+ * This makes it easier to locate packages in the file system.
40
+ *
41
+ * @remarks
42
+ *
43
+ * The policy compares the immediate parent folder name of the package.json
44
+ * against the package name. For scoped packages, the scope can optionally
45
+ * be stripped based on the `stripScopes` configuration.
46
+ *
47
+ * For example:
48
+ * - `packages/my-lib/package.json` with name `my-lib` ✓
49
+ * - `packages/my-lib/package.json` with name `@myorg/my-lib` ✓ (if @myorg in stripScopes)
50
+ * - `packages/wrong-name/package.json` with name `my-lib` ✗
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * import { makePolicy } from "repopo";
55
+ * import { PackageFolderName } from "repopo/policies";
56
+ *
57
+ * const config: RepopoConfig = {
58
+ * policies: [
59
+ * makePolicy(PackageFolderName, {
60
+ * stripScopes: ["@myorg", "@internal"],
61
+ * excludePackages: ["@myorg/special-case"],
62
+ * }),
63
+ * ],
64
+ * };
65
+ * ```
66
+ *
67
+ * @alpha
68
+ */
69
+ export const PackageFolderName = definePackagePolicy({
70
+ name: "PackageFolderName",
71
+ description: "Ensures package folder names match package names for consistent file structure.",
72
+ handler: async (json, { file, config }) => {
73
+ // If no config provided, use defaults (check all packages)
74
+ const effectiveConfig = config ?? {};
75
+ const packageName = json.name;
76
+ // Skip packages without a name
77
+ if (packageName === undefined) {
78
+ return true;
79
+ }
80
+ // Skip the root package (common in monorepos)
81
+ if (packageName === "root") {
82
+ return true;
83
+ }
84
+ // Skip explicitly excluded packages
85
+ if (effectiveConfig.excludePackages?.includes(packageName)) {
86
+ return true;
87
+ }
88
+ // Get the folder name (parent directory of package.json)
89
+ const folderName = path.basename(path.dirname(file));
90
+ // Get the expected folder name
91
+ const expectedFolderName = getExpectedFolderName(packageName, effectiveConfig.stripScopes ?? []);
92
+ // Compare folder name to expected name
93
+ if (folderName === expectedFolderName) {
94
+ return true;
95
+ }
96
+ // Mismatch - build error message
97
+ const failResult = {
98
+ name: PackageFolderName.name,
99
+ file,
100
+ autoFixable: false, // Can't auto-fix folder names
101
+ errorMessages: [
102
+ `Package folder "${folderName}" does not match package name "${packageName}". Expected folder name: "${expectedFolderName}"`,
103
+ ],
104
+ };
105
+ return failResult;
106
+ },
107
+ });
108
+ //# sourceMappingURL=PackageFolderName.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PackageFolderName.js","sourceRoot":"","sources":["../../src/policies/PackageFolderName.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,OAAO,CAAC;AACzB,OAAO,SAAS,MAAM,WAAW,CAAC;AAGlC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAgE3D;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAAa,EAAE,WAAqB;IAC7D,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACd,CAAC;IAED,OAAO,SAAS,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;;GAMG;AACH,SAAS,qBAAqB,CAC7B,WAAmB,EACnB,WAAqB;IAErB,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAE9D,gEAAgE;IAChE,IAAI,KAAK,KAAK,SAAS,IAAI,gBAAgB,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE,CAAC;QACjE,OAAO,YAAY,CAAC;IACrB,CAAC;IAED,8EAA8E;IAC9E,OAAO,WAAW,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,mBAAmB,CAGlD;IACD,IAAI,EAAE,mBAAmB;IACzB,WAAW,EACV,iFAAiF;IAClF,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE;QACzC,2DAA2D;QAC3D,MAAM,eAAe,GAAG,MAAM,IAAI,EAAE,CAAC;QAErC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC;QAE9B,+BAA+B;QAC/B,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC;QACb,CAAC;QAED,8CAA8C;QAC9C,IAAI,WAAW,KAAK,MAAM,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC;QACb,CAAC;QAED,oCAAoC;QACpC,IAAI,eAAe,CAAC,eAAe,EAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YAC5D,OAAO,IAAI,CAAC;QACb,CAAC;QAED,yDAAyD;QACzD,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QAErD,+BAA+B;QAC/B,MAAM,kBAAkB,GAAG,qBAAqB,CAC/C,WAAW,EACX,eAAe,CAAC,WAAW,IAAI,EAAE,CACjC,CAAC;QAEF,uCAAuC;QACvC,IAAI,UAAU,KAAK,kBAAkB,EAAE,CAAC;YACvC,OAAO,IAAI,CAAC;QACb,CAAC;QAED,iCAAiC;QACjC,MAAM,UAAU,GAAkB;YACjC,IAAI,EAAE,iBAAiB,CAAC,IAAI;YAC5B,IAAI;YACJ,WAAW,EAAE,KAAK,EAAE,8BAA8B;YAClD,aAAa,EAAE;gBACd,mBAAmB,UAAU,kCAAkC,WAAW,6BAA6B,kBAAkB,GAAG;aAC5H;SACD,CAAC;QACF,OAAO,UAAU,CAAC;IACnB,CAAC;CACD,CAAC,CAAC","sourcesContent":["import path from \"pathe\";\nimport picomatch from \"picomatch\";\nimport type { PackageJson } from \"type-fest\";\nimport type { PolicyFailure } from \"../policy.js\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\nimport { parsePackageName } from \"../utils/packageName.js\";\n\n/**\n * Configuration for the PackageFolderName policy.\n *\n * This policy ensures that package folder names match their package names,\n * promoting consistency and making packages easier to find in the file system.\n *\n * @example\n * ```typescript\n * const config: PackageFolderNameConfig = {\n * // Strip all scopes when matching folder names\n * stripScopes: [\"@*\"],\n * // Or strip specific scopes\n * stripScopes: [\"@myorg\", \"@internal\"],\n * // These packages are exempt from the check\n * excludePackages: [\"@myorg/special-name\"],\n * };\n * ```\n *\n * @alpha\n */\nexport interface PackageFolderNameConfig {\n\t/**\n\t * A list of npm scope patterns to strip when comparing folder names to package names.\n\t *\n\t * For scoped packages like `@myorg/my-package`, if `@myorg` matches a pattern in this list,\n\t * the policy will expect the folder to be named `my-package` (not `@myorg/my-package`).\n\t *\n\t * @remarks\n\t *\n\t * Supports glob patterns:\n\t * - `@*` - matches all scopes\n\t * - `@myorg` - matches exactly `@myorg`\n\t * - `@my*` - matches scopes starting with `@my`\n\t *\n\t * @example\n\t * ```typescript\n\t * // Strip all scopes - @anything/my-package → folder \"my-package\"\n\t * stripScopes: [\"@*\"]\n\t *\n\t * // Strip specific scopes only\n\t * stripScopes: [\"@myorg\", \"@internal\"]\n\t *\n\t * // Strip scopes matching a pattern\n\t * stripScopes: [\"@fluid*\"]\n\t * ```\n\t */\n\tstripScopes?: string[];\n\n\t/**\n\t * A list of package names that are exempt from folder name matching.\n\t *\n\t * Use this for packages that intentionally have different folder names,\n\t * such as legacy packages or packages with special naming requirements.\n\t *\n\t * @example\n\t * ```typescript\n\t * excludePackages: [\"@myorg/cli\", \"legacy-package\"]\n\t * ```\n\t */\n\texcludePackages?: string[];\n}\n\n/**\n * Check if a scope should be stripped based on the patterns.\n *\n * @param scope - The scope to check (e.g., `@myorg`)\n * @param stripScopes - List of scope patterns to match (supports glob patterns via picomatch)\n * @returns `true` if the scope matches any pattern\n */\nfunction shouldStripScope(scope: string, stripScopes: string[]): boolean {\n\tif (stripScopes.length === 0) {\n\t\treturn false;\n\t}\n\n\treturn picomatch.isMatch(scope, stripScopes);\n}\n\n/**\n * Get the expected folder name from a package name.\n *\n * @param packageName - The package name (e.g., `@myorg/my-package`)\n * @param stripScopes - Scope patterns to strip from the package name\n * @returns The expected folder name\n */\nfunction getExpectedFolderName(\n\tpackageName: string,\n\tstripScopes: string[],\n): string {\n\tconst { scope, unscopedName } = parsePackageName(packageName);\n\n\t// Check if the package is scoped and should have scope stripped\n\tif (scope !== undefined && shouldStripScope(scope, stripScopes)) {\n\t\treturn unscopedName;\n\t}\n\n\t// Return full package name for unscoped packages or scopes not in stripScopes\n\treturn packageName;\n}\n\n/**\n * A policy that ensures package folder names match package names.\n *\n * This policy helps maintain a consistent and predictable file structure\n * in monorepos by ensuring that folder names correspond to package names.\n * This makes it easier to locate packages in the file system.\n *\n * @remarks\n *\n * The policy compares the immediate parent folder name of the package.json\n * against the package name. For scoped packages, the scope can optionally\n * be stripped based on the `stripScopes` configuration.\n *\n * For example:\n * - `packages/my-lib/package.json` with name `my-lib` ✓\n * - `packages/my-lib/package.json` with name `@myorg/my-lib` ✓ (if @myorg in stripScopes)\n * - `packages/wrong-name/package.json` with name `my-lib` ✗\n *\n * @example\n * ```typescript\n * import { makePolicy } from \"repopo\";\n * import { PackageFolderName } from \"repopo/policies\";\n *\n * const config: RepopoConfig = {\n * policies: [\n * makePolicy(PackageFolderName, {\n * stripScopes: [\"@myorg\", \"@internal\"],\n * excludePackages: [\"@myorg/special-case\"],\n * }),\n * ],\n * };\n * ```\n *\n * @alpha\n */\nexport const PackageFolderName = definePackagePolicy<\n\tPackageJson,\n\tPackageFolderNameConfig | undefined\n>({\n\tname: \"PackageFolderName\",\n\tdescription:\n\t\t\"Ensures package folder names match package names for consistent file structure.\",\n\thandler: async (json, { file, config }) => {\n\t\t// If no config provided, use defaults (check all packages)\n\t\tconst effectiveConfig = config ?? {};\n\n\t\tconst packageName = json.name;\n\n\t\t// Skip packages without a name\n\t\tif (packageName === undefined) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Skip the root package (common in monorepos)\n\t\tif (packageName === \"root\") {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Skip explicitly excluded packages\n\t\tif (effectiveConfig.excludePackages?.includes(packageName)) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Get the folder name (parent directory of package.json)\n\t\tconst folderName = path.basename(path.dirname(file));\n\n\t\t// Get the expected folder name\n\t\tconst expectedFolderName = getExpectedFolderName(\n\t\t\tpackageName,\n\t\t\teffectiveConfig.stripScopes ?? [],\n\t\t);\n\n\t\t// Compare folder name to expected name\n\t\tif (folderName === expectedFolderName) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Mismatch - build error message\n\t\tconst failResult: PolicyFailure = {\n\t\t\tname: PackageFolderName.name,\n\t\t\tfile,\n\t\t\tautoFixable: false, // Can't auto-fix folder names\n\t\t\terrorMessages: [\n\t\t\t\t`Package folder \"${folderName}\" does not match package name \"${packageName}\". Expected folder name: \"${expectedFolderName}\"`,\n\t\t\t],\n\t\t};\n\t\treturn failResult;\n\t},\n});\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"PackageJsonProperties.d.ts","sourceRoot":"","sources":["../../src/policies/PackageJsonProperties.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAM7C;;;;GAIG;AACH,MAAM,WAAW,6BAA6B;IAC7C;;OAEG;IACH,QAAQ,EAAE,WAAW,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,oFA4ChC,CAAC"}
1
+ {"version":3,"file":"PackageJsonProperties.d.ts","sourceRoot":"","sources":["../../src/policies/PackageJsonProperties.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAM7C;;;;GAIG;AACH,MAAM,WAAW,6BAA6B;IAC7C;;OAEG;IACH,QAAQ,EAAE,WAAW,CAAC;CACtB;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,oFAkDhC,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { defu } from "defu";
2
+ import { call } from "effection";
2
3
  import jsonfile from "jsonfile";
3
4
  import diff from "microdiff";
4
5
  import { definePackagePolicy } from "../policyDefiners/definePackagePolicy.js";
@@ -8,36 +9,41 @@ const { writeFile: writeJson } = jsonfile;
8
9
  *
9
10
  * @alpha
10
11
  */
11
- export const PackageJsonProperties = definePackagePolicy("PackageJsonProperties", async (json, { file, config, resolve }) => {
12
- if (config === undefined) {
13
- return true;
14
- }
15
- const { verbatim } = config;
16
- const failResult = {
17
- name: PackageJsonProperties.name,
18
- file,
19
- autoFixable: true,
20
- };
21
- const merged = defu(verbatim, json);
22
- const result = diff(merged, json);
23
- const messages = [];
24
- for (const diffResult of result) {
25
- messages.push(`Incorrect package.json field value for '${diffResult.path}'.`);
26
- }
27
- if (messages.length > 0) {
28
- if (resolve) {
29
- const fixResult = {
30
- ...failResult,
31
- resolved: false,
32
- };
33
- await writeJson(file, merged, { spaces: "\t" });
34
- fixResult.resolved = true;
35
- return fixResult;
12
+ export const PackageJsonProperties = definePackagePolicy({
13
+ name: "PackageJsonProperties",
14
+ description: "Ensures package.json files contain required properties with correct values.",
15
+ handler: function* (json, { file, config, resolve }) {
16
+ if (config === undefined) {
17
+ return true;
18
+ }
19
+ const { verbatim } = config;
20
+ const failResult = {
21
+ name: PackageJsonProperties.name,
22
+ file,
23
+ autoFixable: true,
24
+ errorMessages: [],
25
+ };
26
+ const merged = defu(verbatim, json);
27
+ const result = diff(merged, json);
28
+ const messages = [];
29
+ for (const diffResult of result) {
30
+ messages.push(`Incorrect package.json field value for '${diffResult.path}'.`);
36
31
  }
37
- // There were errors, and we're not resolving them, so return a fail result
38
- failResult.errorMessage = messages.join("\n");
39
- return failResult;
40
- }
41
- return true;
32
+ if (messages.length > 0) {
33
+ if (resolve) {
34
+ const fixResult = {
35
+ ...failResult,
36
+ resolved: false,
37
+ };
38
+ yield* call(() => writeJson(file, merged, { spaces: "\t" }));
39
+ fixResult.resolved = true;
40
+ return fixResult;
41
+ }
42
+ // There were errors, and we're not resolving them, so return a fail result
43
+ failResult.errorMessages = messages;
44
+ return failResult;
45
+ }
46
+ return true;
47
+ },
42
48
  });
43
49
  //# sourceMappingURL=PackageJsonProperties.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PackageJsonProperties.js","sourceRoot":"","sources":["../../src/policies/PackageJsonProperties.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAE/E,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;AAc1C;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,mBAAmB,CAGtD,uBAAuB,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE;IACpE,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IACb,CAAC;IACD,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;IAE5B,MAAM,UAAU,GAAkB;QACjC,IAAI,EAAE,qBAAqB,CAAC,IAAI;QAChC,IAAI;QACJ,WAAW,EAAE,IAAI;KACjB,CAAC;IAEF,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACpC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAElC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,KAAK,MAAM,UAAU,IAAI,MAAM,EAAE,CAAC;QACjC,QAAQ,CAAC,IAAI,CACZ,2CAA2C,UAAU,CAAC,IAAI,IAAI,CAC9D,CAAC;IACH,CAAC;IAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzB,IAAI,OAAO,EAAE,CAAC;YACb,MAAM,SAAS,GAAoB;gBAClC,GAAG,UAAU;gBACb,QAAQ,EAAE,KAAK;aACf,CAAC;YAEF,MAAM,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YAEhD,SAAS,CAAC,QAAQ,GAAG,IAAI,CAAC;YAC1B,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,2EAA2E;QAC3E,UAAU,CAAC,YAAY,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9C,OAAO,UAAU,CAAC;IACnB,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CAAC,CAAC","sourcesContent":["import { defu } from \"defu\";\nimport jsonfile from \"jsonfile\";\nimport diff from \"microdiff\";\nimport type { PackageJson } from \"type-fest\";\nimport type { PolicyFailure, PolicyFixResult } from \"../policy.js\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\n\nconst { writeFile: writeJson } = jsonfile;\n\n/**\n * Policy settings for the PackageJsonProperties repo policy.\n *\n * @alpha\n */\nexport interface PackageJsonPropertiesSettings {\n\t/**\n\t * Sets a package.json property to the value provided. The value will be used verbatim.\n\t */\n\tverbatim: PackageJson;\n}\n\n/**\n * A RepoPolicy that checks that package.json properties in packages match expected values.\n *\n * @alpha\n */\nexport const PackageJsonProperties = definePackagePolicy<\n\tPackageJson,\n\tPackageJsonPropertiesSettings | undefined\n>(\"PackageJsonProperties\", async (json, { file, config, resolve }) => {\n\tif (config === undefined) {\n\t\treturn true;\n\t}\n\tconst { verbatim } = config;\n\n\tconst failResult: PolicyFailure = {\n\t\tname: PackageJsonProperties.name,\n\t\tfile,\n\t\tautoFixable: true,\n\t};\n\n\tconst merged = defu(verbatim, json);\n\tconst result = diff(merged, json);\n\n\tconst messages: string[] = [];\n\tfor (const diffResult of result) {\n\t\tmessages.push(\n\t\t\t`Incorrect package.json field value for '${diffResult.path}'.`,\n\t\t);\n\t}\n\n\tif (messages.length > 0) {\n\t\tif (resolve) {\n\t\t\tconst fixResult: PolicyFixResult = {\n\t\t\t\t...failResult,\n\t\t\t\tresolved: false,\n\t\t\t};\n\n\t\t\tawait writeJson(file, merged, { spaces: \"\\t\" });\n\n\t\t\tfixResult.resolved = true;\n\t\t\treturn fixResult;\n\t\t}\n\n\t\t// There were errors, and we're not resolving them, so return a fail result\n\t\tfailResult.errorMessage = messages.join(\"\\n\");\n\t\treturn failResult;\n\t}\n\n\treturn true;\n});\n"]}
1
+ {"version":3,"file":"PackageJsonProperties.js","sourceRoot":"","sources":["../../src/policies/PackageJsonProperties.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAE/E,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;AAc1C;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,mBAAmB,CAGtD;IACD,IAAI,EAAE,uBAAuB;IAC7B,WAAW,EACV,6EAA6E;IAC9E,OAAO,EAAE,QAAQ,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE;QAClD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC;QACb,CAAC;QACD,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;QAE5B,MAAM,UAAU,GAAkB;YACjC,IAAI,EAAE,qBAAqB,CAAC,IAAI;YAChC,IAAI;YACJ,WAAW,EAAE,IAAI;YACjB,aAAa,EAAE,EAAE;SACjB,CAAC;QAEF,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QACpC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAElC,MAAM,QAAQ,GAAa,EAAE,CAAC;QAC9B,KAAK,MAAM,UAAU,IAAI,MAAM,EAAE,CAAC;YACjC,QAAQ,CAAC,IAAI,CACZ,2CAA2C,UAAU,CAAC,IAAI,IAAI,CAC9D,CAAC;QACH,CAAC;QAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACzB,IAAI,OAAO,EAAE,CAAC;gBACb,MAAM,SAAS,GAAoB;oBAClC,GAAG,UAAU;oBACb,QAAQ,EAAE,KAAK;iBACf,CAAC;gBAEF,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;gBAE7D,SAAS,CAAC,QAAQ,GAAG,IAAI,CAAC;gBAC1B,OAAO,SAAS,CAAC;YAClB,CAAC;YAED,2EAA2E;YAC3E,UAAU,CAAC,aAAa,GAAG,QAAQ,CAAC;YACpC,OAAO,UAAU,CAAC;QACnB,CAAC;QAED,OAAO,IAAI,CAAC;IACb,CAAC;CACD,CAAC,CAAC","sourcesContent":["import { defu } from \"defu\";\nimport { call } from \"effection\";\nimport jsonfile from \"jsonfile\";\nimport diff from \"microdiff\";\nimport type { PackageJson } from \"type-fest\";\nimport type { PolicyFailure, PolicyFixResult } from \"../policy.js\";\nimport { definePackagePolicy } from \"../policyDefiners/definePackagePolicy.js\";\n\nconst { writeFile: writeJson } = jsonfile;\n\n/**\n * Policy settings for the PackageJsonProperties repo policy.\n *\n * @alpha\n */\nexport interface PackageJsonPropertiesSettings {\n\t/**\n\t * Sets a package.json property to the value provided. The value will be used verbatim.\n\t */\n\tverbatim: PackageJson;\n}\n\n/**\n * A RepoPolicy that checks that package.json properties in packages match expected values.\n *\n * @alpha\n */\nexport const PackageJsonProperties = definePackagePolicy<\n\tPackageJson,\n\tPackageJsonPropertiesSettings | undefined\n>({\n\tname: \"PackageJsonProperties\",\n\tdescription:\n\t\t\"Ensures package.json files contain required properties with correct values.\",\n\thandler: function* (json, { file, config, resolve }) {\n\t\tif (config === undefined) {\n\t\t\treturn true;\n\t\t}\n\t\tconst { verbatim } = config;\n\n\t\tconst failResult: PolicyFailure = {\n\t\t\tname: PackageJsonProperties.name,\n\t\t\tfile,\n\t\t\tautoFixable: true,\n\t\t\terrorMessages: [],\n\t\t};\n\n\t\tconst merged = defu(verbatim, json);\n\t\tconst result = diff(merged, json);\n\n\t\tconst messages: string[] = [];\n\t\tfor (const diffResult of result) {\n\t\t\tmessages.push(\n\t\t\t\t`Incorrect package.json field value for '${diffResult.path}'.`,\n\t\t\t);\n\t\t}\n\n\t\tif (messages.length > 0) {\n\t\t\tif (resolve) {\n\t\t\t\tconst fixResult: PolicyFixResult = {\n\t\t\t\t\t...failResult,\n\t\t\t\t\tresolved: false,\n\t\t\t\t};\n\n\t\t\t\tyield* call(() => writeJson(file, merged, { spaces: \"\\t\" }));\n\n\t\t\t\tfixResult.resolved = true;\n\t\t\t\treturn fixResult;\n\t\t\t}\n\n\t\t\t// There were errors, and we're not resolving them, so return a fail result\n\t\t\tfailResult.errorMessages = messages;\n\t\t\treturn failResult;\n\t\t}\n\n\t\treturn true;\n\t},\n});\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"PackageJsonRepoDirectoryProperty.d.ts","sourceRoot":"","sources":["../../src/policies/PackageJsonRepoDirectoryProperty.ts"],"names":[],"mappings":"AAOA;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,oDAgD3C,CAAC"}
1
+ {"version":3,"file":"PackageJsonRepoDirectoryProperty.d.ts","sourceRoot":"","sources":["../../src/policies/PackageJsonRepoDirectoryProperty.ts"],"names":[],"mappings":"AAOA;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,oDAuD3C,CAAC"}