repopo 0.9.0 → 0.12.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 (271) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/README.md +41 -2
  3. package/THIRD-PARTY-LICENSES.txt +71 -11
  4. package/bin/dev.js +5 -1
  5. package/esm/baseCommand.d.ts +1 -1
  6. package/esm/baseCommand.d.ts.map +1 -1
  7. package/esm/baseCommand.js +6 -9
  8. package/esm/baseCommand.js.map +1 -1
  9. package/esm/commands/check-native.d.ts +25 -0
  10. package/esm/commands/check-native.d.ts.map +1 -0
  11. package/esm/commands/check-native.js +137 -0
  12. package/esm/commands/check-native.js.map +1 -0
  13. package/esm/commands/check.d.ts +8 -19
  14. package/esm/commands/check.d.ts.map +1 -1
  15. package/esm/commands/check.js +97 -169
  16. package/esm/commands/check.js.map +1 -1
  17. package/esm/commands/list.d.ts.map +1 -1
  18. package/esm/commands/list.js +5 -6
  19. package/esm/commands/list.js.map +1 -1
  20. package/esm/config.d.ts +19 -4
  21. package/esm/config.d.ts.map +1 -1
  22. package/esm/config.js +2 -3
  23. package/esm/config.js.map +1 -1
  24. package/esm/context.d.ts +13 -3
  25. package/esm/context.d.ts.map +1 -1
  26. package/esm/context.js +11 -1
  27. package/esm/context.js.map +1 -1
  28. package/esm/index.d.ts +7 -2
  29. package/esm/index.d.ts.map +1 -1
  30. package/esm/index.js +9 -1
  31. package/esm/index.js.map +1 -1
  32. package/esm/makePolicy.d.ts +77 -8
  33. package/esm/makePolicy.d.ts.map +1 -1
  34. package/esm/makePolicy.js +108 -4
  35. package/esm/makePolicy.js.map +1 -1
  36. package/esm/perf.d.ts +7 -2
  37. package/esm/perf.d.ts.map +1 -1
  38. package/esm/perf.js.map +1 -1
  39. package/esm/policies/CargoLicenceValidated.d.ts +41 -0
  40. package/esm/policies/CargoLicenceValidated.d.ts.map +1 -0
  41. package/esm/policies/CargoLicenceValidated.js +45 -0
  42. package/esm/policies/CargoLicenceValidated.js.map +1 -0
  43. package/esm/policies/CargoLintsConfigured.d.ts +30 -0
  44. package/esm/policies/CargoLintsConfigured.d.ts.map +1 -0
  45. package/esm/policies/CargoLintsConfigured.js +61 -0
  46. package/esm/policies/CargoLintsConfigured.js.map +1 -0
  47. package/esm/policies/CargoLockPolicy.d.ts +34 -0
  48. package/esm/policies/CargoLockPolicy.d.ts.map +1 -0
  49. package/esm/policies/CargoLockPolicy.js +57 -0
  50. package/esm/policies/CargoLockPolicy.js.map +1 -0
  51. package/esm/policies/CargoTomlRequired.d.ts +34 -0
  52. package/esm/policies/CargoTomlRequired.d.ts.map +1 -0
  53. package/esm/policies/CargoTomlRequired.js +77 -0
  54. package/esm/policies/CargoTomlRequired.js.map +1 -0
  55. package/esm/policies/CargoTomlSorted.d.ts +20 -0
  56. package/esm/policies/CargoTomlSorted.d.ts.map +1 -0
  57. package/esm/policies/CargoTomlSorted.js +164 -0
  58. package/esm/policies/CargoTomlSorted.js.map +1 -0
  59. package/esm/policies/ConditionalScripts.d.ts +94 -0
  60. package/esm/policies/ConditionalScripts.d.ts.map +1 -0
  61. package/esm/policies/ConditionalScripts.js +145 -0
  62. package/esm/policies/ConditionalScripts.js.map +1 -0
  63. package/esm/policies/ExactScripts.d.ts +51 -0
  64. package/esm/policies/ExactScripts.d.ts.map +1 -0
  65. package/esm/policies/ExactScripts.js +108 -0
  66. package/esm/policies/ExactScripts.js.map +1 -0
  67. package/esm/policies/GleamDepVersions.d.ts +25 -0
  68. package/esm/policies/GleamDepVersions.d.ts.map +1 -0
  69. package/esm/policies/GleamDepVersions.js +67 -0
  70. package/esm/policies/GleamDepVersions.js.map +1 -0
  71. package/esm/policies/GleamDocExists.d.ts +24 -0
  72. package/esm/policies/GleamDocExists.d.ts.map +1 -0
  73. package/esm/policies/GleamDocExists.js +39 -0
  74. package/esm/policies/GleamDocExists.js.map +1 -0
  75. package/esm/policies/GleamInternalModulesConfigured.d.ts +21 -0
  76. package/esm/policies/GleamInternalModulesConfigured.d.ts.map +1 -0
  77. package/esm/policies/GleamInternalModulesConfigured.js +30 -0
  78. package/esm/policies/GleamInternalModulesConfigured.js.map +1 -0
  79. package/esm/policies/GleamLicenceConfigured.d.ts +29 -0
  80. package/esm/policies/GleamLicenceConfigured.d.ts.map +1 -0
  81. package/esm/policies/GleamLicenceConfigured.js +34 -0
  82. package/esm/policies/GleamLicenceConfigured.js.map +1 -0
  83. package/esm/policies/GleamManifestCommitted.d.ts +9 -0
  84. package/esm/policies/GleamManifestCommitted.d.ts.map +1 -0
  85. package/esm/policies/GleamManifestCommitted.js +26 -0
  86. package/esm/policies/GleamManifestCommitted.js.map +1 -0
  87. package/esm/policies/GleamMonorepoConsistency.d.ts +30 -0
  88. package/esm/policies/GleamMonorepoConsistency.d.ts.map +1 -0
  89. package/esm/policies/GleamMonorepoConsistency.js +74 -0
  90. package/esm/policies/GleamMonorepoConsistency.js.map +1 -0
  91. package/esm/policies/GleamNameRestrictions.d.ts +25 -0
  92. package/esm/policies/GleamNameRestrictions.d.ts.map +1 -0
  93. package/esm/policies/GleamNameRestrictions.js +44 -0
  94. package/esm/policies/GleamNameRestrictions.js.map +1 -0
  95. package/esm/policies/GleamNoPathDepsInPublished.d.ts +22 -0
  96. package/esm/policies/GleamNoPathDepsInPublished.d.ts.map +1 -0
  97. package/esm/policies/GleamNoPathDepsInPublished.js +43 -0
  98. package/esm/policies/GleamNoPathDepsInPublished.js.map +1 -0
  99. package/esm/policies/GleamPathDepsValid.d.ts +9 -0
  100. package/esm/policies/GleamPathDepsValid.d.ts.map +1 -0
  101. package/esm/policies/GleamPathDepsValid.js +59 -0
  102. package/esm/policies/GleamPathDepsValid.js.map +1 -0
  103. package/esm/policies/GleamProjectStructure.d.ts +25 -0
  104. package/esm/policies/GleamProjectStructure.d.ts.map +1 -0
  105. package/esm/policies/GleamProjectStructure.js +33 -0
  106. package/esm/policies/GleamProjectStructure.js.map +1 -0
  107. package/esm/policies/GleamSharedDependencyVersions.d.ts +29 -0
  108. package/esm/policies/GleamSharedDependencyVersions.d.ts.map +1 -0
  109. package/esm/policies/GleamSharedDependencyVersions.js +101 -0
  110. package/esm/policies/GleamSharedDependencyVersions.js.map +1 -0
  111. package/esm/policies/GleamTomlRequired.d.ts +30 -0
  112. package/esm/policies/GleamTomlRequired.d.ts.map +1 -0
  113. package/esm/policies/GleamTomlRequired.js +37 -0
  114. package/esm/policies/GleamTomlRequired.js.map +1 -0
  115. package/esm/policies/GleamTomlSorted.d.ts +20 -0
  116. package/esm/policies/GleamTomlSorted.d.ts.map +1 -0
  117. package/esm/policies/GleamTomlSorted.js +153 -0
  118. package/esm/policies/GleamTomlSorted.js.map +1 -0
  119. package/esm/policies/GleamVersionConstraint.d.ts +20 -0
  120. package/esm/policies/GleamVersionConstraint.d.ts.map +1 -0
  121. package/esm/policies/GleamVersionConstraint.js +64 -0
  122. package/esm/policies/GleamVersionConstraint.js.map +1 -0
  123. package/esm/policies/HtmlFileHeaders.d.ts +1 -1
  124. package/esm/policies/HtmlFileHeaders.d.ts.map +1 -1
  125. package/esm/policies/HtmlFileHeaders.js +1 -1
  126. package/esm/policies/HtmlFileHeaders.js.map +1 -1
  127. package/esm/policies/JsTsFileHeaders.d.ts +1 -1
  128. package/esm/policies/JsTsFileHeaders.d.ts.map +1 -1
  129. package/esm/policies/MutuallyExclusiveScripts.d.ts +46 -0
  130. package/esm/policies/MutuallyExclusiveScripts.d.ts.map +1 -0
  131. package/esm/policies/MutuallyExclusiveScripts.js +52 -0
  132. package/esm/policies/MutuallyExclusiveScripts.js.map +1 -0
  133. package/esm/policies/NoBuildDirectory.d.ts +9 -0
  134. package/esm/policies/NoBuildDirectory.d.ts.map +1 -0
  135. package/esm/policies/NoBuildDirectory.js +29 -0
  136. package/esm/policies/NoBuildDirectory.js.map +1 -0
  137. package/esm/policies/NoPrivateWorkspaceDependencies.d.ts +1 -1
  138. package/esm/policies/NoPrivateWorkspaceDependencies.d.ts.map +1 -1
  139. package/esm/policies/NoTargetArtifacts.d.ts +8 -0
  140. package/esm/policies/NoTargetArtifacts.d.ts.map +1 -0
  141. package/esm/policies/NoTargetArtifacts.js +42 -0
  142. package/esm/policies/NoTargetArtifacts.js.map +1 -0
  143. package/esm/policies/NoUnsafeWithoutJustification.d.ts +24 -0
  144. package/esm/policies/NoUnsafeWithoutJustification.d.ts.map +1 -0
  145. package/esm/policies/NoUnsafeWithoutJustification.js +80 -0
  146. package/esm/policies/NoUnsafeWithoutJustification.js.map +1 -0
  147. package/esm/policies/NoWildcardDependencies.d.ts +30 -0
  148. package/esm/policies/NoWildcardDependencies.d.ts.map +1 -0
  149. package/esm/policies/NoWildcardDependencies.js +83 -0
  150. package/esm/policies/NoWildcardDependencies.js.map +1 -0
  151. package/esm/policies/PackageAllowedScopes.d.ts +1 -1
  152. package/esm/policies/PackageAllowedScopes.d.ts.map +1 -1
  153. package/esm/policies/PackageEsmType.d.ts +1 -1
  154. package/esm/policies/PackageEsmType.d.ts.map +1 -1
  155. package/esm/policies/PackageEsmType.js +7 -5
  156. package/esm/policies/PackageEsmType.js.map +1 -1
  157. package/esm/policies/PackageFolderName.d.ts +1 -1
  158. package/esm/policies/PackageFolderName.d.ts.map +1 -1
  159. package/esm/policies/PackageJsonProperties.d.ts +1 -1
  160. package/esm/policies/PackageJsonProperties.d.ts.map +1 -1
  161. package/esm/policies/PackageJsonProperties.js +12 -4
  162. package/esm/policies/PackageJsonProperties.js.map +1 -1
  163. package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts +1 -1
  164. package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts.map +1 -1
  165. package/esm/policies/PackageJsonRepoDirectoryProperty.js +3 -2
  166. package/esm/policies/PackageJsonRepoDirectoryProperty.js.map +1 -1
  167. package/esm/policies/PackageJsonSorted.d.ts +1 -1
  168. package/esm/policies/PackageJsonSorted.d.ts.map +1 -1
  169. package/esm/policies/PackageJsonSorted.js +42 -7
  170. package/esm/policies/PackageJsonSorted.js.map +1 -1
  171. package/esm/policies/PackageLicense.d.ts +1 -1
  172. package/esm/policies/PackageLicense.d.ts.map +1 -1
  173. package/esm/policies/PackageLicense.js +1 -2
  174. package/esm/policies/PackageLicense.js.map +1 -1
  175. package/esm/policies/PackagePrivateField.d.ts +1 -1
  176. package/esm/policies/PackagePrivateField.d.ts.map +1 -1
  177. package/esm/policies/PackagePrivateField.js +3 -2
  178. package/esm/policies/PackagePrivateField.js.map +1 -1
  179. package/esm/policies/PackageReadme.d.ts +1 -1
  180. package/esm/policies/PackageReadme.d.ts.map +1 -1
  181. package/esm/policies/PackageReadme.js +2 -2
  182. package/esm/policies/PackageReadme.js.map +1 -1
  183. package/esm/policies/PackageScripts.d.ts +1 -1
  184. package/esm/policies/PackageScripts.d.ts.map +1 -1
  185. package/esm/policies/PackageScripts.js +3 -2
  186. package/esm/policies/PackageScripts.js.map +1 -1
  187. package/esm/policies/PackageTestScripts.d.ts +1 -1
  188. package/esm/policies/PackageTestScripts.d.ts.map +1 -1
  189. package/esm/policies/PackageTestScripts.js +3 -3
  190. package/esm/policies/PackageTestScripts.js.map +1 -1
  191. package/esm/policies/PublicApiDocumented.d.ts +21 -0
  192. package/esm/policies/PublicApiDocumented.d.ts.map +1 -0
  193. package/esm/policies/PublicApiDocumented.js +36 -0
  194. package/esm/policies/PublicApiDocumented.js.map +1 -0
  195. package/esm/policies/RequiredScripts.d.ts +71 -0
  196. package/esm/policies/RequiredScripts.d.ts.map +1 -0
  197. package/esm/policies/RequiredScripts.js +121 -0
  198. package/esm/policies/RequiredScripts.js.map +1 -0
  199. package/esm/policies/RustDocExists.d.ts +24 -0
  200. package/esm/policies/RustDocExists.d.ts.map +1 -0
  201. package/esm/policies/RustDocExists.js +49 -0
  202. package/esm/policies/RustDocExists.js.map +1 -0
  203. package/esm/policies/RustToolchainExists.d.ts +20 -0
  204. package/esm/policies/RustToolchainExists.d.ts.map +1 -0
  205. package/esm/policies/RustToolchainExists.js +27 -0
  206. package/esm/policies/RustToolchainExists.js.map +1 -0
  207. package/esm/policies/RustfmtConfigExists.d.ts +20 -0
  208. package/esm/policies/RustfmtConfigExists.d.ts.map +1 -0
  209. package/esm/policies/RustfmtConfigExists.js +27 -0
  210. package/esm/policies/RustfmtConfigExists.js.map +1 -0
  211. package/esm/policies/ScriptContains.d.ts +64 -0
  212. package/esm/policies/ScriptContains.d.ts.map +1 -0
  213. package/esm/policies/ScriptContains.js +62 -0
  214. package/esm/policies/ScriptContains.js.map +1 -0
  215. package/esm/policies/SharedDependencyVersions.d.ts +24 -0
  216. package/esm/policies/SharedDependencyVersions.d.ts.map +1 -0
  217. package/esm/policies/SharedDependencyVersions.js +135 -0
  218. package/esm/policies/SharedDependencyVersions.js.map +1 -0
  219. package/esm/policies/WorkspaceInheritance.d.ts +24 -0
  220. package/esm/policies/WorkspaceInheritance.d.ts.map +1 -0
  221. package/esm/policies/WorkspaceInheritance.js +94 -0
  222. package/esm/policies/WorkspaceInheritance.js.map +1 -0
  223. package/esm/policies/WorkspaceMembersValid.d.ts +20 -0
  224. package/esm/policies/WorkspaceMembersValid.d.ts.map +1 -0
  225. package/esm/policies/WorkspaceMembersValid.js +88 -0
  226. package/esm/policies/WorkspaceMembersValid.js.map +1 -0
  227. package/esm/policies/constants.d.ts +8 -0
  228. package/esm/policies/constants.d.ts.map +1 -1
  229. package/esm/policies/constants.js +8 -0
  230. package/esm/policies/constants.js.map +1 -1
  231. package/esm/policies.d.ts +36 -1
  232. package/esm/policies.d.ts.map +1 -1
  233. package/esm/policies.js +68 -0
  234. package/esm/policies.js.map +1 -1
  235. package/esm/policy.d.ts +202 -84
  236. package/esm/policy.d.ts.map +1 -1
  237. package/esm/policy.js +103 -21
  238. package/esm/policy.js.map +1 -1
  239. package/esm/policyDefiners/defineCargoPolicy.d.ts +66 -0
  240. package/esm/policyDefiners/defineCargoPolicy.d.ts.map +1 -0
  241. package/esm/policyDefiners/defineCargoPolicy.js +60 -0
  242. package/esm/policyDefiners/defineCargoPolicy.js.map +1 -0
  243. package/esm/policyDefiners/defineFileHeaderPolicy.d.ts +7 -2
  244. package/esm/policyDefiners/defineFileHeaderPolicy.d.ts.map +1 -1
  245. package/esm/policyDefiners/defineFileHeaderPolicy.js +11 -7
  246. package/esm/policyDefiners/defineFileHeaderPolicy.js.map +1 -1
  247. package/esm/policyDefiners/defineGleamPolicy.d.ts +65 -0
  248. package/esm/policyDefiners/defineGleamPolicy.d.ts.map +1 -0
  249. package/esm/policyDefiners/defineGleamPolicy.js +59 -0
  250. package/esm/policyDefiners/defineGleamPolicy.js.map +1 -0
  251. package/esm/policyDefiners/definePackagePolicy.d.ts +18 -5
  252. package/esm/policyDefiners/definePackagePolicy.d.ts.map +1 -1
  253. package/esm/policyDefiners/definePackagePolicy.js +14 -4
  254. package/esm/policyDefiners/definePackagePolicy.js.map +1 -1
  255. package/esm/policyDefiners/spdxValidator.d.ts +74 -0
  256. package/esm/policyDefiners/spdxValidator.d.ts.map +1 -0
  257. package/esm/policyDefiners/spdxValidator.js +127 -0
  258. package/esm/policyDefiners/spdxValidator.js.map +1 -0
  259. package/esm/policyDefiners/tomlParser.d.ts +8 -0
  260. package/esm/policyDefiners/tomlParser.d.ts.map +1 -0
  261. package/esm/policyDefiners/tomlParser.js +17 -0
  262. package/esm/policyDefiners/tomlParser.js.map +1 -0
  263. package/esm/runner.d.ts +60 -0
  264. package/esm/runner.d.ts.map +1 -0
  265. package/esm/runner.js +174 -0
  266. package/esm/runner.js.map +1 -0
  267. package/esm/tsdoc-metadata.json +1 -1
  268. package/oclif.manifest.json +100 -3
  269. package/package.json +46 -31
  270. package/scripts/postinstall.mjs +154 -0
  271. package/sidecar/sidecar.mjs +468 -0
package/esm/policy.js CHANGED
@@ -1,31 +1,11 @@
1
1
  import { NoJsFileExtensions } from "./policies/NoJsFileExtensions.js";
2
2
  import { PackageJsonRepoDirectoryProperty } from "./policies/PackageJsonRepoDirectoryProperty.js";
3
3
  import { PackageJsonSorted } from "./policies/PackageJsonSorted.js";
4
- import { PackageScripts } from "./policies/PackageScripts.js";
5
- // biome-ignore lint/suspicious/noExplicitAny: type guard
6
- export function isPolicyFixResult(toCheck) {
7
- if (typeof toCheck !== "object" || toCheck === null) {
8
- return false;
9
- }
10
- return "resolved" in toCheck;
11
- }
12
- /**
13
- * Default policies included with repopo.
14
- *
15
- * @alpha
16
- */
17
- // biome-ignore lint/suspicious/noExplicitAny: FIXME
18
- export const DefaultPolicies = [
19
- NoJsFileExtensions,
20
- PackageJsonRepoDirectoryProperty,
21
- PackageJsonSorted,
22
- PackageScripts,
23
- ];
24
4
  /**
25
5
  * Abstract base class for creating policies with object-based construction.
26
6
  *
27
7
  * @remarks
28
- * This class accepts a {@link PolicyDefinition} object in its constructor,
8
+ * This class accepts a {@link PolicyShape} object in its constructor,
29
9
  * making it easier to add new optional properties in the future without breaking changes.
30
10
  *
31
11
  * @alpha
@@ -46,4 +26,106 @@ export class Policy {
46
26
  this.resolver = definition.resolver;
47
27
  }
48
28
  }
29
+ // ============================================================================
30
+ // Type Guards
31
+ // ============================================================================
32
+ /**
33
+ * Type guard to check if a result is a {@link PolicyFixResult} (legacy).
34
+ *
35
+ * @deprecated Use {@link isPolicyError} instead when working with the new {@link PolicyError} format.
36
+ *
37
+ * @alpha
38
+ */
39
+ // biome-ignore lint/suspicious/noExplicitAny: type guard
40
+ export function isPolicyFixResult(toCheck) {
41
+ if (typeof toCheck !== "object" || toCheck === null) {
42
+ return false;
43
+ }
44
+ return "resolved" in toCheck;
45
+ }
46
+ /**
47
+ * Type guard to check if a result is a PolicyError (new format).
48
+ *
49
+ * @alpha
50
+ */
51
+ // biome-ignore lint/suspicious/noExplicitAny: type guard
52
+ export function isPolicyError(toCheck) {
53
+ if (typeof toCheck !== "object" || toCheck === null) {
54
+ return false;
55
+ }
56
+ return "error" in toCheck && typeof toCheck.error === "string";
57
+ }
58
+ /**
59
+ * Type guard to check if a result is a {@link PolicyFailure} (legacy format).
60
+ *
61
+ * @deprecated Use {@link isPolicyError} instead when working with the new {@link PolicyError} format.
62
+ *
63
+ * @alpha
64
+ */
65
+ // biome-ignore lint/suspicious/noExplicitAny: type guard
66
+ export function isPolicyFailure(toCheck) {
67
+ if (typeof toCheck !== "object" || toCheck === null) {
68
+ return false;
69
+ }
70
+ return "errorMessages" in toCheck && Array.isArray(toCheck.errorMessages);
71
+ }
72
+ // ============================================================================
73
+ // Conversion Functions
74
+ // ============================================================================
75
+ /**
76
+ * Converts a legacy PolicyFailure/PolicyFixResult to the new PolicyError format.
77
+ * @internal
78
+ */
79
+ export function convertLegacyResult(result, _file) {
80
+ if (result === true) {
81
+ return true;
82
+ }
83
+ // Check if it's already the new format
84
+ if (isPolicyError(result)) {
85
+ return result;
86
+ }
87
+ // It's a legacy PolicyFailure or PolicyFixResult
88
+ const error = result.errorMessages?.join("; ") ?? "Policy violation";
89
+ const isFixResult = isPolicyFixResult(result);
90
+ return {
91
+ error,
92
+ fixable: result.autoFixable ?? false,
93
+ fixed: isFixResult ? result.resolved : undefined,
94
+ manualFix: result.manualFix,
95
+ };
96
+ }
97
+ /**
98
+ * Converts a new PolicyError to the legacy PolicyFailure format.
99
+ * @internal
100
+ */
101
+ export function convertToLegacyResult(result, policyName, file) {
102
+ const base = {
103
+ name: policyName,
104
+ file,
105
+ autoFixable: result.fixable,
106
+ errorMessages: [result.error],
107
+ manualFix: result.manualFix,
108
+ };
109
+ if (result.fixed !== undefined) {
110
+ return {
111
+ ...base,
112
+ resolved: result.fixed,
113
+ };
114
+ }
115
+ return base;
116
+ }
117
+ // ============================================================================
118
+ // Default Policies
119
+ // ============================================================================
120
+ /**
121
+ * Default policies included with repopo.
122
+ *
123
+ * @alpha
124
+ */
125
+ // biome-ignore lint/suspicious/noExplicitAny: FIXME
126
+ export const DefaultPolicies = [
127
+ NoJsFileExtensions,
128
+ PackageJsonRepoDirectoryProperty,
129
+ PackageJsonSorted,
130
+ ];
49
131
  //# sourceMappingURL=policy.js.map
package/esm/policy.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,gCAAgC,EAAE,MAAM,gDAAgD,CAAC;AAClG,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAyM9D,yDAAyD;AACzD,MAAM,UAAU,iBAAiB,CAAC,OAAY;IAC7C,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACrD,OAAO,KAAK,CAAC;IACd,CAAC;IACD,OAAO,UAAU,IAAI,OAAO,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AAEH,oDAAoD;AACpD,MAAM,CAAC,MAAM,eAAe,GAA4B;IACvD,kBAAkB;IAClB,gCAAgC;IAChC,iBAAiB;IACjB,cAAc;CACL,CAAC;AAEX;;;;;;;;GAQG;AACH,MAAM,OAAgB,MAAM;IACX,IAAI,CAAS;IACb,WAAW,CAAS;IACpB,KAAK,CAAS;IACd,OAAO,CAAmB;IAC1B,aAAa,CAAiB;IAC9B,QAAQ,CAA2C;IAEnE,YAAmB,UAA+B;QACjD,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;QAC5B,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC;QAC1C,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;QAC9B,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC;QAClC,IAAI,CAAC,aAAa,GAAG,UAAU,CAAC,aAAa,CAAC;QAC9C,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;IACrC,CAAC;CACD","sourcesContent":["import type { Operation } from \"effection\";\nimport type { RequireExactlyOne } from \"type-fest\";\nimport { NoJsFileExtensions } from \"./policies/NoJsFileExtensions.js\";\nimport { PackageJsonRepoDirectoryProperty } from \"./policies/PackageJsonRepoDirectoryProperty.js\";\nimport { PackageJsonSorted } from \"./policies/PackageJsonSorted.js\";\nimport { PackageScripts } from \"./policies/PackageScripts.js\";\n\n/**\n * A type representing a policy name.\n *\n * @alpha\n */\nexport type PolicyName = string;\n\n/**\n * Arguments passed to policy functions.\n *\n * @alpha\n */\n\nexport interface PolicyFunctionArguments<C> {\n\t/**\n\t * Path to the file, relative to the repo root.\n\t */\n\tfile: string;\n\n\t/**\n\t * Absolute path to the root of the repo.\n\t */\n\troot: string;\n\n\t/**\n\t * If true, the handler should resolve any violations automatically if possible.\n\t */\n\tresolve: boolean;\n\n\t/**\n\t * @remarks\n\t *\n\t * Note that the handler function receives the config as an argument.\n\t */\n\tconfig?: C | undefined;\n}\n\n/**\n * A policy handler function that is called to check policy against a file.\n *\n * @remarks\n * Policy handlers can be implemented in two ways:\n * - As an async function returning a Promise\n * - As an Effection generator function returning an Operation\n *\n * Both styles are supported to allow gradual migration and flexibility.\n *\n * @alpha\n */\nexport type PolicyHandler<C = unknown | undefined> =\n\t| ((args: PolicyFunctionArguments<C>) => Promise<PolicyHandlerResult>)\n\t| ((args: PolicyFunctionArguments<C>) => Operation<PolicyHandlerResult>);\n\n/**\n * A standalone function that can be called to resolve a policy failure.\n *\n * @remarks\n * Resolvers can be implemented in two ways:\n * - As an async function returning a Promise\n * - As an Effection generator function returning an Operation\n *\n * Both styles are supported to allow gradual migration and flexibility.\n *\n * @alpha\n */\nexport type PolicyStandaloneResolver<C = undefined> = (\n\targs: Omit<PolicyFunctionArguments<C>, \"resolve\">,\n) => Promise<PolicyFixResult> | Operation<PolicyFixResult>;\n\n/**\n * A RepoPolicyDefinition checks and applies policies to files in the repository.\n *\n * Each policy has a name and a match regex for matching which files it should apply to. Every file in th repo is\n * enumerated and if it matches the regex for a policy, that policy is applied.\n *\n * Each policy includes a handler function that checks a file against the policy and can optionally resolve any problems\n * (automated resolutions depend on the policy implementation).\n *\n * @typeParam C - type of configuration object used by the policy\n *\n * @alpha\n */\nexport interface PolicyDefinition<C = undefined> {\n\t/**\n\t * The name of the policy; displayed in UI and used in settings.\n\t */\n\tname: PolicyName;\n\n\t/**\n\t * A more detailed description of the policy and its intended function.\n\t */\n\tdescription: string;\n\n\t/**\n\t * A regular expression that is used to match files in the repo.\n\t */\n\tmatch: RegExp;\n\n\t/**\n\t * A handler function that checks if a file is compliant with the policy.\n\t *\n\t * @param file - Repo-relative path to the file to check.\n\t * @param root - Absolute path to the root of the repo.\n\t * @param resolve - If true, automated policy fixes will be applied. Not all policies support automated fixes.\n\t * @returns True if the file passed the policy; otherwise a PolicyFailure object will be returned.\n\t */\n\n\thandler: PolicyHandler<C>;\n\n\t/**\n\t * A resolver function that can be used to automatically address the policy violation.\n\t *\n\t * @param file - Repo-relative path to the file to check.\n\t * @param root - Absolute path to the root of the repo.\n\t * @returns true if the file passed the policy; otherwise a PolicyFailure object will be returned.\n\t */\n\tresolver?: PolicyStandaloneResolver<C> | undefined;\n\n\t/**\n\t * A default config that will be used if none is provided.\n\t */\n\tdefaultConfig?: C | undefined;\n}\n\n/**\n * @alpha\n */\nexport interface PolicyInstanceSettings<C> {\n\t/**\n\t * An array of strings/regular expressions. File paths that match any of these expressions will be completely excluded\n\t * from policy.\n\t *\n\t * Paths will be matched relative to the root of the repo.\n\t */\n\texcludeFiles?: (string | RegExp)[];\n\n\t/**\n\t * The config that is applied to the policy instance.\n\t */\n\tconfig?: C | undefined;\n}\n\n/**\n * @alpha\n */\nexport type PolicyInstance<C = undefined> = RequireExactlyOne<\n\tPolicyDefinition<C> & PolicyInstanceSettings<C>,\n\t\"handler\"\n>;\n\n/**\n * A policy failure.\n *\n * @alpha\n */\nexport interface PolicyFailure {\n\t/**\n\t * Name of the policy that failed.\n\t */\n\tname: PolicyName;\n\n\t/**\n\t * Path to the file that failed the policy.\n\t */\n\tfile: string;\n\n\t/**\n\t * Set to `true` if the policy can be fixed automatically.\n\t */\n\tautoFixable?: boolean | undefined;\n\n\t/**\n\t * An optional error message accompanying the failure.\n\t */\n\terrorMessages: string[];\n\n\t/**\n\t * An optional string that tells the user how to fix the failure(s).\n\t */\n\tmanualFix?: string;\n}\n\n/**\n * The result of an automatic fix for a failing policy.\n *\n * @alpha\n */\nexport interface PolicyFixResult extends PolicyFailure {\n\t/**\n\t * Set to true if the failure was resolved by the automated fixer.\n\t */\n\tresolved: boolean;\n}\n\n/**\n * @alpha\n */\nexport type PolicyHandlerResult = true | PolicyFailure | PolicyFixResult;\n\n// biome-ignore lint/suspicious/noExplicitAny: type guard\nexport function isPolicyFixResult(toCheck: any): toCheck is PolicyFixResult {\n\tif (typeof toCheck !== \"object\" || toCheck === null) {\n\t\treturn false;\n\t}\n\treturn \"resolved\" in toCheck;\n}\n\n/**\n * Default policies included with repopo.\n *\n * @alpha\n */\n\n// biome-ignore lint/suspicious/noExplicitAny: FIXME\nexport const DefaultPolicies: PolicyDefinition<any>[] = [\n\tNoJsFileExtensions,\n\tPackageJsonRepoDirectoryProperty,\n\tPackageJsonSorted,\n\tPackageScripts,\n] as const;\n\n/**\n * Abstract base class for creating policies with object-based construction.\n *\n * @remarks\n * This class accepts a {@link PolicyDefinition} object in its constructor,\n * making it easier to add new optional properties in the future without breaking changes.\n *\n * @alpha\n */\nexport abstract class Policy<C> implements PolicyDefinition<C> {\n\tpublic readonly name: string;\n\tpublic readonly description: string;\n\tpublic readonly match: RegExp;\n\tpublic readonly handler: PolicyHandler<C>;\n\tpublic readonly defaultConfig?: C | undefined;\n\tpublic readonly resolver?: PolicyStandaloneResolver<C> | undefined;\n\n\tpublic constructor(definition: PolicyDefinition<C>) {\n\t\tthis.name = definition.name;\n\t\tthis.description = definition.description;\n\t\tthis.match = definition.match;\n\t\tthis.handler = definition.handler;\n\t\tthis.defaultConfig = definition.defaultConfig;\n\t\tthis.resolver = definition.resolver;\n\t}\n}\n"]}
1
+ {"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AACA,OAAO,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,SAAS,EAAE,CAAC;QAChC,OAAO;YACN,GAAG,IAAI;YACP,QAAQ,EAAE,MAAM,CAAC,KAAK;SACI,CAAC;IAC7B,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAED,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;GAIG;AACH,oDAAoD;AACpD,MAAM,CAAC,MAAM,eAAe,GAAkB;IAC7C,kBAAkB;IAClB,gCAAgC;IAChC,iBAAiB;CACR,CAAC","sourcesContent":["import type { Operation } from \"effection\";\nimport { NoJsFileExtensions } from \"./policies/NoJsFileExtensions.js\";\nimport { PackageJsonRepoDirectoryProperty } from \"./policies/PackageJsonRepoDirectoryProperty.js\";\nimport { PackageJsonSorted } from \"./policies/PackageJsonSorted.js\";\n\n/**\n * A type representing a policy name.\n *\n * @alpha\n */\nexport type PolicyName = string;\n\n/**\n * A stable identifier for a configured policy instance.\n *\n * @alpha\n */\nexport type PolicyInstanceId = string;\n\n/**\n * Arguments passed to policy handler functions.\n *\n * @alpha\n */\nexport interface PolicyArgs<C = void> {\n\t/**\n\t * Path to the file, relative to the repo root.\n\t */\n\tfile: string;\n\n\t/**\n\t * Absolute path to the root of the repo.\n\t */\n\troot: string;\n\n\t/**\n\t * If true, the handler should resolve any violations automatically if possible.\n\t */\n\tresolve: boolean;\n\n\t/**\n\t * Optional configuration for the policy.\n\t */\n\tconfig?: C | undefined;\n}\n\n/**\n * @deprecated Use {@link PolicyArgs} instead.\n * @alpha\n */\nexport type PolicyFunctionArguments<C> = PolicyArgs<C>;\n\n// ============================================================================\n// New Result Types\n// ============================================================================\n\n/**\n * A policy error returned when a file fails a policy check.\n *\n * @alpha\n */\nexport interface PolicyError {\n\t/**\n\t * The error message describing what failed.\n\t */\n\terror: string;\n\n\t/**\n\t * Set to `true` if the policy violation can be fixed automatically.\n\t */\n\tfixable?: boolean | undefined;\n\n\t/**\n\t * Set to `true` if the violation was successfully fixed (only set when resolve=true).\n\t */\n\tfixed?: boolean | undefined;\n\n\t/**\n\t * An optional string that tells the user how to manually fix the failure.\n\t */\n\tmanualFix?: string | undefined;\n}\n\n/**\n * The result of a policy handler.\n * Returns `true` if the file passes the policy, or a {@link PolicyError} if it fails.\n *\n * @alpha\n */\nexport type PolicyResult = true | PolicyError;\n\n// ============================================================================\n// Legacy Result Types (Still supported for backward compatibility)\n// ============================================================================\n\n/**\n * A policy failure (legacy format).\n *\n * @deprecated Use {@link PolicyError} instead, which has a simpler API.\n *\n * @alpha\n */\nexport interface PolicyFailure {\n\t/**\n\t * Name of the policy that failed.\n\t */\n\tname: PolicyName;\n\n\t/**\n\t * Path to the file that failed the policy.\n\t */\n\tfile: string;\n\n\t/**\n\t * Set to `true` if the policy can be fixed automatically.\n\t */\n\tautoFixable?: boolean | undefined;\n\n\t/**\n\t * An optional error message accompanying the failure.\n\t */\n\terrorMessages: string[];\n\n\t/**\n\t * An optional string that tells the user how to fix the failure(s).\n\t */\n\tmanualFix?: string | undefined;\n}\n\n/**\n * The result of an automatic fix for a failing policy (legacy format).\n *\n * @deprecated Use {@link PolicyError} with `fixed: boolean` instead.\n *\n * @alpha\n */\nexport interface PolicyFixResult extends PolicyFailure {\n\t/**\n\t * Set to true if the failure was resolved by the automated fixer.\n\t */\n\tresolved: boolean;\n}\n\n/**\n * Result type that accepts both legacy and new formats.\n *\n * @alpha\n */\nexport type PolicyHandlerResult =\n\t| true\n\t| PolicyFailure\n\t| PolicyFixResult\n\t| PolicyError;\n\n// ============================================================================\n// Handler Types\n// ============================================================================\n\n/**\n * A policy handler function that checks a file against a policy.\n *\n * @remarks\n * Policy handlers can be implemented in two ways:\n * - As an async function returning a Promise\n * - As an Effection generator function returning an Operation\n *\n * Both styles are supported. Async handlers are automatically wrapped for Effection.\n *\n * Handlers can return either the legacy format ({@link PolicyFailure}) or\n * the new format ({@link PolicyError}). The new format is simpler and recommended\n * for new policies.\n *\n * @alpha\n */\nexport type PolicyHandler<C = unknown | undefined> =\n\t| ((args: PolicyArgs<C>) => Promise<PolicyHandlerResult>)\n\t| ((args: PolicyArgs<C>) => Operation<PolicyHandlerResult>);\n\n/**\n * A standalone resolver function that can fix policy violations (legacy format).\n *\n * @deprecated Use {@link PolicyResolver} instead, which uses {@link PolicyError}.\n *\n * @alpha\n */\nexport type PolicyStandaloneResolver<C = undefined> = (\n\targs: Omit<PolicyArgs<C>, \"resolve\">,\n) => Promise<PolicyFixResult> | Operation<PolicyFixResult>;\n\n/**\n * A standalone resolver function using the new format.\n *\n * @alpha\n */\nexport type PolicyResolver<C = void> = (\n\targs: Omit<PolicyArgs<C>, \"resolve\">,\n) => Promise<PolicyError> | Operation<PolicyError>;\n\n// ============================================================================\n// Policy Definition Types\n// ============================================================================\n\n/**\n * Interface describing the shape of a policy definition.\n *\n * @typeParam C - Type of configuration object used by the policy\n *\n * @alpha\n */\nexport interface PolicyShape<C = void> {\n\t/**\n\t * The name of the policy; displayed in UI and used in settings.\n\t */\n\tname: PolicyName;\n\n\t/**\n\t * A detailed description of the policy and its purpose.\n\t */\n\tdescription: string;\n\n\t/**\n\t * A regular expression that matches files this policy applies to.\n\t */\n\tmatch: RegExp;\n\n\t/**\n\t * The handler function that checks if a file complies with the policy.\n\t */\n\thandler: PolicyHandler<C>;\n\n\t/**\n\t * An optional resolver function that can automatically fix violations.\n\t */\n\tresolver?: PolicyStandaloneResolver<C> | undefined;\n\n\t/**\n\t * A default configuration that will be used if none is provided.\n\t */\n\tdefaultConfig?: C | undefined;\n}\n\n/**\n * Abstract base class for creating policies with object-based construction.\n *\n * @remarks\n * This class accepts a {@link PolicyShape} object in its constructor,\n * making it easier to add new optional properties in the future without breaking changes.\n *\n * @alpha\n */\nexport abstract class Policy<C = void> implements PolicyShape<C> {\n\tpublic readonly name: string;\n\tpublic readonly description: string;\n\tpublic readonly match: RegExp;\n\tpublic readonly handler: PolicyHandler<C>;\n\tpublic readonly defaultConfig?: C | undefined;\n\tpublic readonly resolver?: PolicyStandaloneResolver<C> | undefined;\n\n\tpublic constructor(definition: PolicyShape<C>) {\n\t\tthis.name = definition.name;\n\t\tthis.description = definition.description;\n\t\tthis.match = definition.match;\n\t\tthis.handler = definition.handler;\n\t\tthis.defaultConfig = definition.defaultConfig;\n\t\tthis.resolver = definition.resolver;\n\t}\n}\n\n/**\n * @deprecated Use {@link PolicyShape} instead.\n * @alpha\n */\nexport type PolicyDefinition<C = undefined> = PolicyShape<C>;\n\n/**\n * A policy instance with configuration and exclusion settings applied.\n *\n * @typeParam C - Type of configuration object used by the policy\n *\n * @alpha\n */\nexport interface ConfiguredPolicy<C = void> extends PolicyShape<C> {\n\t/**\n\t * An optional stable identifier for this configured policy instance.\n\t *\n\t * This is required when the same policy definition is configured more than once.\n\t */\n\tinstanceId?: PolicyInstanceId | undefined;\n\n\t/**\n\t * The configuration applied to this policy instance.\n\t */\n\tconfig?: C | undefined;\n\n\t/**\n\t * File paths matching these patterns will be excluded from this policy.\n\t * @deprecated Use `excludeFiles` instead for backward compatibility.\n\t */\n\texclude?: (string | RegExp)[] | undefined;\n\n\t/**\n\t * File paths matching these patterns will be excluded from this policy.\n\t */\n\texcludeFiles?: (string | RegExp)[] | undefined;\n\n\t/**\n\t * Internal normalized handler (always an Effection Operation).\n\t * This is set by the policy() function and used by the execution engine.\n\t * @internal\n\t */\n\t_internalHandler?: (args: PolicyArgs<C>) => Operation<PolicyHandlerResult>;\n}\n\n/**\n * Settings for configuring a policy instance.\n * @alpha\n */\nexport interface PolicyInstanceSettings<C> {\n\t/**\n\t * A stable identifier for this configured policy instance.\n\t */\n\tinstanceId?: PolicyInstanceId | undefined;\n\n\t/**\n\t * An array of strings/regular expressions. File paths that match any of these expressions will be completely excluded\n\t * from policy.\n\t *\n\t * Paths will be matched relative to the root of the repo.\n\t */\n\texcludeFiles?: (string | RegExp)[];\n\n\t/**\n\t * The config that is applied to the policy instance.\n\t */\n\tconfig?: C | undefined;\n}\n\n/**\n * @deprecated Use {@link ConfiguredPolicy} instead.\n * @alpha\n */\nexport type PolicyInstance<C = undefined> = ConfiguredPolicy<C>;\n\n/**\n * A configured policy instance with its runtime identity assigned.\n *\n * @alpha\n */\nexport type IdentifiedPolicy<C = undefined> = ConfiguredPolicy<C> & {\n\tinstanceId: PolicyInstanceId;\n};\n\n// ============================================================================\n// Type Guards\n// ============================================================================\n\n/**\n * Type guard to check if a result is a {@link PolicyFixResult} (legacy).\n *\n * @deprecated Use {@link isPolicyError} instead when working with the new {@link PolicyError} format.\n *\n * @alpha\n */\n// biome-ignore lint/suspicious/noExplicitAny: type guard\nexport function isPolicyFixResult(toCheck: any): toCheck is PolicyFixResult {\n\tif (typeof toCheck !== \"object\" || toCheck === null) {\n\t\treturn false;\n\t}\n\treturn \"resolved\" in toCheck;\n}\n\n/**\n * Type guard to check if a result is a PolicyError (new format).\n *\n * @alpha\n */\n// biome-ignore lint/suspicious/noExplicitAny: type guard\nexport function isPolicyError(toCheck: any): toCheck is PolicyError {\n\tif (typeof toCheck !== \"object\" || toCheck === null) {\n\t\treturn false;\n\t}\n\treturn \"error\" in toCheck && typeof toCheck.error === \"string\";\n}\n\n/**\n * Type guard to check if a result is a {@link PolicyFailure} (legacy format).\n *\n * @deprecated Use {@link isPolicyError} instead when working with the new {@link PolicyError} format.\n *\n * @alpha\n */\n// biome-ignore lint/suspicious/noExplicitAny: type guard\nexport function isPolicyFailure(toCheck: any): toCheck is PolicyFailure {\n\tif (typeof toCheck !== \"object\" || toCheck === null) {\n\t\treturn false;\n\t}\n\treturn \"errorMessages\" in toCheck && Array.isArray(toCheck.errorMessages);\n}\n\n// ============================================================================\n// Conversion Functions\n// ============================================================================\n\n/**\n * Converts a legacy PolicyFailure/PolicyFixResult to the new PolicyError format.\n * @internal\n */\nexport function convertLegacyResult(\n\tresult: PolicyHandlerResult,\n\t_file: string,\n): PolicyResult {\n\tif (result === true) {\n\t\treturn true;\n\t}\n\n\t// Check if it's already the new format\n\tif (isPolicyError(result)) {\n\t\treturn result;\n\t}\n\n\t// It's a legacy PolicyFailure or PolicyFixResult\n\tconst error = result.errorMessages?.join(\"; \") ?? \"Policy violation\";\n\tconst isFixResult = isPolicyFixResult(result);\n\n\treturn {\n\t\terror,\n\t\tfixable: result.autoFixable ?? false,\n\t\tfixed: isFixResult ? result.resolved : undefined,\n\t\tmanualFix: result.manualFix,\n\t};\n}\n\n/**\n * Converts a new PolicyError to the legacy PolicyFailure format.\n * @internal\n */\nexport function convertToLegacyResult(\n\tresult: PolicyError,\n\tpolicyName: string,\n\tfile: string,\n): PolicyFailure | PolicyFixResult {\n\tconst base: PolicyFailure = {\n\t\tname: policyName,\n\t\tfile,\n\t\tautoFixable: result.fixable,\n\t\terrorMessages: [result.error],\n\t\tmanualFix: result.manualFix,\n\t};\n\n\tif (result.fixed !== undefined) {\n\t\treturn {\n\t\t\t...base,\n\t\t\tresolved: result.fixed,\n\t\t} satisfies PolicyFixResult;\n\t}\n\n\treturn base;\n}\n\n// ============================================================================\n// Default Policies\n// ============================================================================\n\n/**\n * Default policies included with repopo.\n *\n * @alpha\n */\n// biome-ignore lint/suspicious/noExplicitAny: FIXME\nexport const DefaultPolicies: Policy<any>[] = [\n\tNoJsFileExtensions,\n\tPackageJsonRepoDirectoryProperty,\n\tPackageJsonSorted,\n] as const;\n"]}
@@ -0,0 +1,66 @@
1
+ import { type Operation } from "effection";
2
+ import type { PolicyArgs, PolicyHandlerResult, PolicyShape } from "../policy.js";
3
+ /**
4
+ * A parsed Cargo.toml represented as a record of sections and values.
5
+ *
6
+ * @alpha
7
+ */
8
+ export type CargoToml = Record<string, unknown>;
9
+ /**
10
+ * A policy handler for Cargo.toml policies.
11
+ *
12
+ * @remarks
13
+ * Receives the parsed TOML content and the standard policy arguments.
14
+ *
15
+ * @alpha
16
+ */
17
+ export type CargoTomlHandler<C> = (toml: CargoToml, args: PolicyArgs<C>) => Operation<PolicyHandlerResult> | Promise<PolicyHandlerResult>;
18
+ /**
19
+ * Input arguments for defining a Cargo.toml policy.
20
+ *
21
+ * @alpha
22
+ */
23
+ export interface DefineCargoPolicyArgs<C> {
24
+ /**
25
+ * The name of the policy.
26
+ */
27
+ name: string;
28
+ /**
29
+ * A description of the policy's purpose.
30
+ */
31
+ description: string;
32
+ /**
33
+ * The handler function that receives the parsed Cargo.toml and policy arguments.
34
+ */
35
+ handler: CargoTomlHandler<C>;
36
+ /**
37
+ * Optional default configuration for the policy.
38
+ */
39
+ defaultConfig?: C;
40
+ }
41
+ /**
42
+ * Define a repo policy for Cargo.toml files.
43
+ *
44
+ * @remarks
45
+ * This is a helper function that creates a policy pre-configured to match
46
+ * Cargo.toml files. The handler receives the parsed TOML content.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const MyCargoPolicy = defineCargoPolicy({
51
+ * name: "MyCargoPolicy",
52
+ * description: "Ensures Cargo.toml has required fields",
53
+ * handler: async (toml, { file }) => {
54
+ * const pkg = toml.package as Record<string, unknown> | undefined;
55
+ * if (!pkg?.name) {
56
+ * return { error: "Missing package name", fixable: false };
57
+ * }
58
+ * return true;
59
+ * },
60
+ * });
61
+ * ```
62
+ *
63
+ * @alpha
64
+ */
65
+ export declare function defineCargoPolicy<C = undefined>(args: DefineCargoPolicyArgs<C>): PolicyShape<C>;
66
+ //# sourceMappingURL=defineCargoPolicy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineCargoPolicy.d.ts","sourceRoot":"","sources":["../../src/policyDefiners/defineCargoPolicy.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAGjD,OAAO,KAAK,EACX,UAAU,EACV,mBAAmB,EACnB,WAAW,EACX,MAAM,cAAc,CAAC;AAGtB;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEhD;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CACjC,IAAI,EAAE,SAAS,EACf,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KACf,SAAS,CAAC,mBAAmB,CAAC,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAcnE;;;;GAIG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACvC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAE7B;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,SAAS,EAC9C,IAAI,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAC5B,WAAW,CAAC,CAAC,CAAC,CAuBhB"}
@@ -0,0 +1,60 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { call } from "effection";
3
+ import { resolve } from "pathe";
4
+ import { CargoTomlRegexMatch } from "../policies/constants.js";
5
+ import { parseToml } from "./tomlParser.js";
6
+ /**
7
+ * Type guard to check if a value is an Effection Operation (generator).
8
+ */
9
+ function isOperation(value) {
10
+ return (typeof value === "object" &&
11
+ value !== null &&
12
+ "next" in value &&
13
+ typeof value.next === "function");
14
+ }
15
+ /**
16
+ * Define a repo policy for Cargo.toml files.
17
+ *
18
+ * @remarks
19
+ * This is a helper function that creates a policy pre-configured to match
20
+ * Cargo.toml files. The handler receives the parsed TOML content.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const MyCargoPolicy = defineCargoPolicy({
25
+ * name: "MyCargoPolicy",
26
+ * description: "Ensures Cargo.toml has required fields",
27
+ * handler: async (toml, { file }) => {
28
+ * const pkg = toml.package as Record<string, unknown> | undefined;
29
+ * if (!pkg?.name) {
30
+ * return { error: "Missing package name", fixable: false };
31
+ * }
32
+ * return true;
33
+ * },
34
+ * });
35
+ * ```
36
+ *
37
+ * @alpha
38
+ */
39
+ export function defineCargoPolicy(args) {
40
+ const { name, description, handler: cargoHandler, defaultConfig } = args;
41
+ return {
42
+ name,
43
+ description,
44
+ match: CargoTomlRegexMatch,
45
+ defaultConfig,
46
+ handler: function* (innerArgs) {
47
+ const content = yield* call(() => readFile(resolve(innerArgs.root, innerArgs.file), "utf-8"));
48
+ const toml = yield* call(() => parseToml(content));
49
+ const result = cargoHandler(toml, innerArgs);
50
+ if (result instanceof Promise) {
51
+ return yield* call(() => result);
52
+ }
53
+ if (isOperation(result)) {
54
+ return yield* result;
55
+ }
56
+ throw new Error(`Unexpected handler result type: ${typeof result}`);
57
+ },
58
+ };
59
+ }
60
+ //# sourceMappingURL=defineCargoPolicy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineCargoPolicy.js","sourceRoot":"","sources":["../../src/policyDefiners/defineCargoPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAkB,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAM/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAsB5C;;GAEG;AACH,SAAS,WAAW,CAAI,KAAc;IACrC,OAAO,CACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,OAAQ,KAA2B,CAAC,IAAI,KAAK,UAAU,CACvD,CAAC;AACH,CAAC;AA6BD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,iBAAiB,CAChC,IAA8B;IAE9B,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC;IACzE,OAAO;QACN,IAAI;QACJ,WAAW;QACX,KAAK,EAAE,mBAAmB;QAC1B,aAAa;QACb,OAAO,EAAE,QAAQ,CAAC,EAAE,SAAS;YAC5B,MAAM,OAAO,GAAW,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CACxC,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAC1D,CAAC;YACF,MAAM,IAAI,GAAc,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;YAC9D,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;YAE7C,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;gBAC/B,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC;YAClC,CAAC;YACD,IAAI,WAAW,CAAsB,MAAM,CAAC,EAAE,CAAC;gBAC9C,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC;YACtB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,mCAAmC,OAAO,MAAM,EAAE,CAAC,CAAC;QACrE,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { call, type Operation } from \"effection\";\nimport { resolve } from \"pathe\";\nimport { CargoTomlRegexMatch } from \"../policies/constants.js\";\nimport type {\n\tPolicyArgs,\n\tPolicyHandlerResult,\n\tPolicyShape,\n} from \"../policy.js\";\nimport { parseToml } from \"./tomlParser.js\";\n\n/**\n * A parsed Cargo.toml represented as a record of sections and values.\n *\n * @alpha\n */\nexport type CargoToml = Record<string, unknown>;\n\n/**\n * A policy handler for Cargo.toml policies.\n *\n * @remarks\n * Receives the parsed TOML content and the standard policy arguments.\n *\n * @alpha\n */\nexport type CargoTomlHandler<C> = (\n\ttoml: CargoToml,\n\targs: PolicyArgs<C>,\n) => Operation<PolicyHandlerResult> | Promise<PolicyHandlerResult>;\n\n/**\n * Type guard to check if a value is an Effection Operation (generator).\n */\nfunction isOperation<T>(value: unknown): value is Operation<T> {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"next\" in value &&\n\t\ttypeof (value as { next: unknown }).next === \"function\"\n\t);\n}\n\n/**\n * Input arguments for defining a Cargo.toml policy.\n *\n * @alpha\n */\nexport interface DefineCargoPolicyArgs<C> {\n\t/**\n\t * The name of the policy.\n\t */\n\tname: string;\n\n\t/**\n\t * A description of the policy's purpose.\n\t */\n\tdescription: string;\n\n\t/**\n\t * The handler function that receives the parsed Cargo.toml and policy arguments.\n\t */\n\thandler: CargoTomlHandler<C>;\n\n\t/**\n\t * Optional default configuration for the policy.\n\t */\n\tdefaultConfig?: C;\n}\n\n/**\n * Define a repo policy for Cargo.toml files.\n *\n * @remarks\n * This is a helper function that creates a policy pre-configured to match\n * Cargo.toml files. The handler receives the parsed TOML content.\n *\n * @example\n * ```typescript\n * const MyCargoPolicy = defineCargoPolicy({\n * name: \"MyCargoPolicy\",\n * description: \"Ensures Cargo.toml has required fields\",\n * handler: async (toml, { file }) => {\n * const pkg = toml.package as Record<string, unknown> | undefined;\n * if (!pkg?.name) {\n * return { error: \"Missing package name\", fixable: false };\n * }\n * return true;\n * },\n * });\n * ```\n *\n * @alpha\n */\nexport function defineCargoPolicy<C = undefined>(\n\targs: DefineCargoPolicyArgs<C>,\n): PolicyShape<C> {\n\tconst { name, description, handler: cargoHandler, defaultConfig } = args;\n\treturn {\n\t\tname,\n\t\tdescription,\n\t\tmatch: CargoTomlRegexMatch,\n\t\tdefaultConfig,\n\t\thandler: function* (innerArgs) {\n\t\t\tconst content: string = yield* call(() =>\n\t\t\t\treadFile(resolve(innerArgs.root, innerArgs.file), \"utf-8\"),\n\t\t\t);\n\t\t\tconst toml: CargoToml = yield* call(() => parseToml(content));\n\t\t\tconst result = cargoHandler(toml, innerArgs);\n\n\t\t\tif (result instanceof Promise) {\n\t\t\t\treturn yield* call(() => result);\n\t\t\t}\n\t\t\tif (isOperation<PolicyHandlerResult>(result)) {\n\t\t\t\treturn yield* result;\n\t\t\t}\n\t\t\tthrow new Error(`Unexpected handler result type: ${typeof result}`);\n\t\t},\n\t};\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { PolicyDefinition } from "../policy.js";
1
+ import type { PolicyShape } from "../policy.js";
2
2
  /**
3
3
  * @alpha
4
4
  */
@@ -83,5 +83,10 @@ export interface DefineFileHeaderPolicyArgs {
83
83
  *
84
84
  * @alpha
85
85
  */
86
- export declare function defineFileHeaderPolicy(args: DefineFileHeaderPolicyArgs): PolicyDefinition<FileHeaderPolicyConfig>;
86
+ export declare function defineFileHeaderPolicy(args: DefineFileHeaderPolicyArgs): PolicyShape<FileHeaderPolicyConfig>;
87
+ /**
88
+ * Alias for defineFileHeaderPolicy.
89
+ * @alpha
90
+ */
91
+ export declare const fileHeaderPolicy: typeof defineFileHeaderPolicy;
87
92
  //# sourceMappingURL=defineFileHeaderPolicy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"defineFileHeaderPolicy.d.ts","sourceRoot":"","sources":["../../src/policyDefiners/defineFileHeaderPolicy.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACX,gBAAgB,EAGhB,MAAM,cAAc,CAAC;AAItB;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACtC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,yBAChB,SAAQ,OAAO,CAAC,sBAAsB,CAAC;IACvC,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;;;OAMG;IACH,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,sBAAsB,KAAK,MAAM,CAAC;CACtE;AAED;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IAC1C;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,MAAM,EAAE,yBAAyB,CAAC;CAClC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CACrC,IAAI,EAAE,0BAA0B,GAC9B,gBAAgB,CAAC,sBAAsB,CAAC,CAoE1C"}
1
+ {"version":3,"file":"defineFileHeaderPolicy.d.ts","sourceRoot":"","sources":["../../src/policyDefiners/defineFileHeaderPolicy.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAkC,WAAW,EAAE,MAAM,cAAc,CAAC;AAIhF;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACtC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,yBAChB,SAAQ,OAAO,CAAC,sBAAsB,CAAC;IACvC,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;;;OAMG;IACH,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,sBAAsB,KAAK,MAAM,CAAC;CACtE;AAED;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IAC1C;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,MAAM,EAAE,yBAAyB,CAAC;CAClC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CACrC,IAAI,EAAE,0BAA0B,GAC9B,WAAW,CAAC,sBAAsB,CAAC,CAqErC;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,+BAAyB,CAAC"}
@@ -1,7 +1,7 @@
1
1
  import { readFile, writeFile } from "node:fs/promises";
2
2
  import { EOL as newline } from "node:os";
3
3
  import { call } from "effection";
4
- import { extname } from "pathe";
4
+ import { extname, resolve as resolvePath } from "pathe";
5
5
  const trailingSpaces = /\s*\\r\?\\n/;
6
6
  /**
7
7
  * Given a {@link FileHeaderPolicyConfig}, produces a function that detects correct file headers
@@ -31,17 +31,15 @@ export function defineFileHeaderPolicy(args) {
31
31
  .map((line) => `${start}${line.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&")}${end}`)
32
32
  .join("")
33
33
  .replace(trailingSpaces, "\\r?\\n"); // Trim trailing spaces at end-of-line.
34
- // Detection regex matches the header start (if any), followed by lines in 'headerText',
35
- // optionally followed by lines in the 'autoGenText', and finally the header end (if any).
36
- const regex = new RegExp(`^${pre}${toRegex(config.headerText ?? "")}(${toRegex(config.autoGenText ?? "")})?${post}`);
37
34
  return {
38
35
  name,
39
36
  description,
40
37
  match: config.match,
41
- handler: function* ({ file, resolve, config: policyConfig }) {
38
+ handler: function* ({ file, root, resolve, config: policyConfig }) {
42
39
  if (policyConfig === undefined) {
43
40
  return true;
44
41
  }
42
+ const regex = new RegExp(`^${pre}${toRegex(policyConfig.headerText)}(${toRegex(policyConfig.autoGenText ?? config.autoGenText ?? "")})?${post}`);
45
43
  const failResult = {
46
44
  name,
47
45
  file,
@@ -50,7 +48,8 @@ export function defineFileHeaderPolicy(args) {
50
48
  };
51
49
  // TODO: Consider reading only the first 512B or so since headers are typically
52
50
  // at the beginning of the file.
53
- const content = yield* call(() => readFile(file, { encoding: "utf8" }));
51
+ const filePath = resolvePath(root, file);
52
+ const content = yield* call(() => readFile(filePath, { encoding: "utf8" }));
54
53
  const failed = !regex.test(content);
55
54
  if (failed) {
56
55
  failResult.errorMessages.push(`${extname(file)} file missing header`);
@@ -58,7 +57,7 @@ export function defineFileHeaderPolicy(args) {
58
57
  if (failed) {
59
58
  if (resolve) {
60
59
  const newContent = config.replacer(content, policyConfig);
61
- yield* call(() => writeFile(file, newContent));
60
+ yield* call(() => writeFile(filePath, newContent));
62
61
  const fixResult = {
63
62
  ...failResult,
64
63
  resolved: true,
@@ -71,4 +70,9 @@ export function defineFileHeaderPolicy(args) {
71
70
  },
72
71
  };
73
72
  }
73
+ /**
74
+ * Alias for defineFileHeaderPolicy.
75
+ * @alpha
76
+ */
77
+ export const fileHeaderPolicy = defineFileHeaderPolicy;
74
78
  //# sourceMappingURL=defineFileHeaderPolicy.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"defineFileHeaderPolicy.js","sourceRoot":"","sources":["../../src/policyDefiners/defineFileHeaderPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAOhC,MAAM,cAAc,GAAG,aAAa,CAAC;AAmFrC;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,sBAAsB,CACrC,IAAgC;IAEhC,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAC3C,MAAM,GAAG,GAAG,MAAM,CAAC,WAAW,EAAE,MAAM,IAAI,EAAE,CAAC;IAC7C,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;IACtC,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;IAClC,MAAM,IAAI,GAAG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,IAAI,MAAM,EAAE,CAAC;gBACZ,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;YACvE,CAAC;YAED,IAAI,MAAM,EAAE,CAAC;gBACZ,IAAI,OAAO,EAAE,CAAC;oBACb,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;oBAC1D,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;oBAE/C,MAAM,SAAS,GAAoB;wBAClC,GAAG,UAAU;wBACb,QAAQ,EAAE,IAAI;qBACd,CAAC;oBAEF,OAAO,SAAS,CAAC;gBAClB,CAAC;gBACD,OAAO,UAAU,CAAC;YACnB,CAAC;YAED,OAAO,IAAI,CAAC;QACb,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { readFile, writeFile } from \"node:fs/promises\";\nimport { EOL as newline } from \"node:os\";\nimport { call } from \"effection\";\nimport { extname } from \"pathe\";\nimport type {\n\tPolicyDefinition,\n\tPolicyFailure,\n\tPolicyFixResult,\n} from \"../policy.js\";\n\nconst trailingSpaces = /\\s*\\\\r\\?\\\\n/;\n\n/**\n * @alpha\n */\nexport interface FileHeaderPolicyConfig {\n\t/**\n\t * The text to use as the header.\n\t */\n\theaderText: string;\n\n\t/**\n\t * An optional string that will be appended to the headerText.\n\t */\n\tautoGenText?: string;\n}\n\n/**\n * Configuration for generating file headers with specific formatting rules.\n *\n * @remarks\n * This interface extends FileHeaderPolicyConfig and adds formatting-specific\n * properties to control how headers are inserted into different file types.\n *\n * @alpha\n */\nexport interface FileHeaderGeneratorConfig\n\textends Partial<FileHeaderPolicyConfig> {\n\t/** Regular expression that matches files this header generator applies to */\n\tmatch: RegExp;\n\n\t/**\n\t * Regex matching header prefix (e.g. `/*!\\r?\\n`)\n\t */\n\theaderStart?: RegExp;\n\n\t/**\n\t * Regex matching beginning of each line (e.g. ' * ')\n\t */\n\tlineStart: RegExp;\n\n\t/**\n\t * Regex matching the end of each line (e.g., `\\r?\\n`)\n\t */\n\tlineEnd: RegExp;\n\n\t/**\n\t * Regex matching the header postfix.\n\t */\n\theaderEnd?: RegExp;\n\n\t/**\n\t * Function that generates the properly formatted header content for insertion into files.\n\t *\n\t * @param content - The original file content\n\t * @param config - The file header policy configuration\n\t * @returns The formatted header string to insert\n\t */\n\treplacer: (content: string, config: FileHeaderPolicyConfig) => string;\n}\n\n/**\n * Input arguments for defining a file header policy.\n *\n * @alpha\n */\nexport interface DefineFileHeaderPolicyArgs {\n\t/**\n\t * The name of the policy.\n\t */\n\tname: string;\n\n\t/**\n\t * A description of the policy's purpose.\n\t */\n\tdescription: string;\n\n\t/**\n\t * Configuration for how headers are detected and inserted.\n\t */\n\tconfig: FileHeaderGeneratorConfig;\n}\n\n/**\n * Given a {@link FileHeaderPolicyConfig}, produces a function that detects correct file headers\n * and returns an error string if the header is missing or incorrect.\n *\n * @example\n * ```typescript\n * const MyHeaderPolicy = defineFileHeaderPolicy({\n * name: \"MyHeaderPolicy\",\n * description: \"Ensures files have required headers\",\n * config: { match: /\\.ts$/, lineStart: /\\/\\/ /, lineEnd: /\\r?\\n/, replacer: ... },\n * });\n * ```\n *\n * @alpha\n */\nexport function defineFileHeaderPolicy(\n\targs: DefineFileHeaderPolicyArgs,\n): PolicyDefinition<FileHeaderPolicyConfig> {\n\tconst { name, description, config } = args;\n\tconst pre = config.headerStart?.source ?? \"\";\n\tconst start = config.lineStart.source;\n\tconst end = config.lineEnd.source;\n\tconst post = config.headerEnd?.source ?? \"\";\n\n\t// Helper which constructs a matching RegExp from a given multiline string.\n\t// (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping)\n\tconst toRegex = (text: string): string =>\n\t\ttext\n\t\t\t.split(newline)\n\t\t\t.map(\n\t\t\t\t(line) =>\n\t\t\t\t\t`${start}${line.replace(/[$()*+.?[\\\\\\]^{|}]/g, \"\\\\$&\")}${end}`,\n\t\t\t)\n\t\t\t.join(\"\")\n\t\t\t.replace(trailingSpaces, \"\\\\r?\\\\n\"); // Trim trailing spaces at end-of-line.\n\n\t// Detection regex matches the header start (if any), followed by lines in 'headerText',\n\t// optionally followed by lines in the 'autoGenText', and finally the header end (if any).\n\tconst regex = new RegExp(\n\t\t`^${pre}${toRegex(config.headerText ?? \"\")}(${toRegex(config.autoGenText ?? \"\")})?${post}`,\n\t);\n\n\treturn {\n\t\tname,\n\t\tdescription,\n\t\tmatch: config.match,\n\t\thandler: function* ({ file, resolve, config: policyConfig }) {\n\t\t\tif (policyConfig === undefined) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tconst failResult: PolicyFailure = {\n\t\t\t\tname,\n\t\t\t\tfile,\n\t\t\t\tautoFixable: true,\n\t\t\t\terrorMessages: [],\n\t\t\t};\n\n\t\t\t// TODO: Consider reading only the first 512B or so since headers are typically\n\t\t\t// at the beginning of the file.\n\t\t\tconst content = yield* call(() => readFile(file, { encoding: \"utf8\" }));\n\t\t\tconst failed = !regex.test(content);\n\n\t\t\tif (failed) {\n\t\t\t\tfailResult.errorMessages.push(`${extname(file)} file missing header`);\n\t\t\t}\n\n\t\t\tif (failed) {\n\t\t\t\tif (resolve) {\n\t\t\t\t\tconst newContent = config.replacer(content, policyConfig);\n\t\t\t\t\tyield* call(() => writeFile(file, newContent));\n\n\t\t\t\t\tconst fixResult: PolicyFixResult = {\n\t\t\t\t\t\t...failResult,\n\t\t\t\t\t\tresolved: true,\n\t\t\t\t\t};\n\n\t\t\t\t\treturn fixResult;\n\t\t\t\t}\n\t\t\t\treturn failResult;\n\t\t\t}\n\n\t\t\treturn true;\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"defineFileHeaderPolicy.js","sourceRoot":"","sources":["../../src/policyDefiners/defineFileHeaderPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA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b,CAAC;KACD,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,sBAAsB,CAAC","sourcesContent":["import { readFile, writeFile } from \"node:fs/promises\";\nimport { EOL as newline } from \"node:os\";\nimport { call } from \"effection\";\nimport { extname, resolve as resolvePath } from \"pathe\";\nimport type { PolicyFailure, PolicyFixResult, PolicyShape } from \"../policy.js\";\n\nconst trailingSpaces = /\\s*\\\\r\\?\\\\n/;\n\n/**\n * @alpha\n */\nexport interface FileHeaderPolicyConfig {\n\t/**\n\t * The text to use as the header.\n\t */\n\theaderText: string;\n\n\t/**\n\t * An optional string that will be appended to the headerText.\n\t */\n\tautoGenText?: string;\n}\n\n/**\n * Configuration for generating file headers with specific formatting rules.\n *\n * @remarks\n * This interface extends FileHeaderPolicyConfig and adds formatting-specific\n * properties to control how headers are inserted into different file types.\n *\n * @alpha\n */\nexport interface FileHeaderGeneratorConfig\n\textends Partial<FileHeaderPolicyConfig> {\n\t/** Regular expression that matches files this header generator applies to */\n\tmatch: RegExp;\n\n\t/**\n\t * Regex matching header prefix (e.g. `/*!\\r?\\n`)\n\t */\n\theaderStart?: RegExp;\n\n\t/**\n\t * Regex matching beginning of each line (e.g. ' * ')\n\t */\n\tlineStart: RegExp;\n\n\t/**\n\t * Regex matching the end of each line (e.g., `\\r?\\n`)\n\t */\n\tlineEnd: RegExp;\n\n\t/**\n\t * Regex matching the header postfix.\n\t */\n\theaderEnd?: RegExp;\n\n\t/**\n\t * Function that generates the properly formatted header content for insertion into files.\n\t *\n\t * @param content - The original file content\n\t * @param config - The file header policy configuration\n\t * @returns The formatted header string to insert\n\t */\n\treplacer: (content: string, config: FileHeaderPolicyConfig) => string;\n}\n\n/**\n * Input arguments for defining a file header policy.\n *\n * @alpha\n */\nexport interface DefineFileHeaderPolicyArgs {\n\t/**\n\t * The name of the policy.\n\t */\n\tname: string;\n\n\t/**\n\t * A description of the policy's purpose.\n\t */\n\tdescription: string;\n\n\t/**\n\t * Configuration for how headers are detected and inserted.\n\t */\n\tconfig: FileHeaderGeneratorConfig;\n}\n\n/**\n * Given a {@link FileHeaderPolicyConfig}, produces a function that detects correct file headers\n * and returns an error string if the header is missing or incorrect.\n *\n * @example\n * ```typescript\n * const MyHeaderPolicy = defineFileHeaderPolicy({\n * name: \"MyHeaderPolicy\",\n * description: \"Ensures files have required headers\",\n * config: { match: /\\.ts$/, lineStart: /\\/\\/ /, lineEnd: /\\r?\\n/, replacer: ... },\n * });\n * ```\n *\n * @alpha\n */\nexport function defineFileHeaderPolicy(\n\targs: DefineFileHeaderPolicyArgs,\n): PolicyShape<FileHeaderPolicyConfig> {\n\tconst { name, description, config } = args;\n\tconst pre = config.headerStart?.source ?? \"\";\n\tconst start = config.lineStart.source;\n\tconst end = config.lineEnd.source;\n\tconst post = config.headerEnd?.source ?? \"\";\n\n\t// Helper which constructs a matching RegExp from a given multiline string.\n\t// (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping)\n\tconst toRegex = (text: string): string =>\n\t\ttext\n\t\t\t.split(newline)\n\t\t\t.map(\n\t\t\t\t(line) =>\n\t\t\t\t\t`${start}${line.replace(/[$()*+.?[\\\\\\]^{|}]/g, \"\\\\$&\")}${end}`,\n\t\t\t)\n\t\t\t.join(\"\")\n\t\t\t.replace(trailingSpaces, \"\\\\r?\\\\n\"); // Trim trailing spaces at end-of-line.\n\n\treturn {\n\t\tname,\n\t\tdescription,\n\t\tmatch: config.match,\n\t\thandler: function* ({ file, root, resolve, config: policyConfig }) {\n\t\t\tif (policyConfig === undefined) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tconst regex = new RegExp(\n\t\t\t\t`^${pre}${toRegex(policyConfig.headerText)}(${toRegex(policyConfig.autoGenText ?? config.autoGenText ?? \"\")})?${post}`,\n\t\t\t);\n\n\t\t\tconst failResult: PolicyFailure = {\n\t\t\t\tname,\n\t\t\t\tfile,\n\t\t\t\tautoFixable: true,\n\t\t\t\terrorMessages: [],\n\t\t\t};\n\n\t\t\t// TODO: Consider reading only the first 512B or so since headers are typically\n\t\t\t// at the beginning of the file.\n\t\t\tconst filePath = resolvePath(root, file);\n\t\t\tconst content = yield* call(() =>\n\t\t\t\treadFile(filePath, { encoding: \"utf8\" }),\n\t\t\t);\n\t\t\tconst failed = !regex.test(content);\n\n\t\t\tif (failed) {\n\t\t\t\tfailResult.errorMessages.push(`${extname(file)} file missing header`);\n\t\t\t}\n\n\t\t\tif (failed) {\n\t\t\t\tif (resolve) {\n\t\t\t\t\tconst newContent = config.replacer(content, policyConfig);\n\t\t\t\t\tyield* call(() => writeFile(filePath, newContent));\n\n\t\t\t\t\tconst fixResult: PolicyFixResult = {\n\t\t\t\t\t\t...failResult,\n\t\t\t\t\t\tresolved: true,\n\t\t\t\t\t};\n\n\t\t\t\t\treturn fixResult;\n\t\t\t\t}\n\t\t\t\treturn failResult;\n\t\t\t}\n\n\t\t\treturn true;\n\t\t},\n\t};\n}\n\n/**\n * Alias for defineFileHeaderPolicy.\n * @alpha\n */\nexport const fileHeaderPolicy = defineFileHeaderPolicy;\n"]}
@@ -0,0 +1,65 @@
1
+ import { type Operation } from "effection";
2
+ import type { PolicyArgs, PolicyHandlerResult, PolicyShape } from "../policy.js";
3
+ /**
4
+ * A parsed gleam.toml represented as a record of sections and values.
5
+ *
6
+ * @alpha
7
+ */
8
+ export type GleamToml = Record<string, unknown>;
9
+ /**
10
+ * A policy handler for gleam.toml policies.
11
+ *
12
+ * @remarks
13
+ * Receives the parsed TOML content and the standard policy arguments.
14
+ *
15
+ * @alpha
16
+ */
17
+ export type GleamTomlHandler<C> = (toml: GleamToml, args: PolicyArgs<C>) => Operation<PolicyHandlerResult> | Promise<PolicyHandlerResult>;
18
+ /**
19
+ * Input arguments for defining a gleam.toml policy.
20
+ *
21
+ * @alpha
22
+ */
23
+ export interface DefineGleamPolicyArgs<C> {
24
+ /**
25
+ * The name of the policy.
26
+ */
27
+ name: string;
28
+ /**
29
+ * A description of the policy's purpose.
30
+ */
31
+ description: string;
32
+ /**
33
+ * The handler function that receives the parsed gleam.toml and policy arguments.
34
+ */
35
+ handler: GleamTomlHandler<C>;
36
+ /**
37
+ * Optional default configuration for the policy.
38
+ */
39
+ defaultConfig?: C;
40
+ }
41
+ /**
42
+ * Define a repo policy for gleam.toml files.
43
+ *
44
+ * @remarks
45
+ * This is a helper function that creates a policy pre-configured to match
46
+ * gleam.toml files. The handler receives the parsed TOML content.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const MyGleamPolicy = defineGleamPolicy({
51
+ * name: "MyGleamPolicy",
52
+ * description: "Ensures gleam.toml has required fields",
53
+ * handler: async (toml, { file }) => {
54
+ * if (!toml.name) {
55
+ * return { error: "Missing name field", fixable: false };
56
+ * }
57
+ * return true;
58
+ * },
59
+ * });
60
+ * ```
61
+ *
62
+ * @alpha
63
+ */
64
+ export declare function defineGleamPolicy<C = undefined>(args: DefineGleamPolicyArgs<C>): PolicyShape<C>;
65
+ //# sourceMappingURL=defineGleamPolicy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineGleamPolicy.d.ts","sourceRoot":"","sources":["../../src/policyDefiners/defineGleamPolicy.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAGjD,OAAO,KAAK,EACX,UAAU,EACV,mBAAmB,EACnB,WAAW,EACX,MAAM,cAAc,CAAC;AAGtB;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEhD;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CACjC,IAAI,EAAE,SAAS,EACf,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KACf,SAAS,CAAC,mBAAmB,CAAC,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAcnE;;;;GAIG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACvC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAE7B;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,SAAS,EAC9C,IAAI,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAC5B,WAAW,CAAC,CAAC,CAAC,CAuBhB"}
@@ -0,0 +1,59 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { call } from "effection";
3
+ import { resolve } from "pathe";
4
+ import { GleamTomlRegexMatch } from "../policies/constants.js";
5
+ import { parseToml } from "./tomlParser.js";
6
+ /**
7
+ * Type guard to check if a value is an Effection Operation (generator).
8
+ */
9
+ function isOperation(value) {
10
+ return (typeof value === "object" &&
11
+ value !== null &&
12
+ "next" in value &&
13
+ typeof value.next === "function");
14
+ }
15
+ /**
16
+ * Define a repo policy for gleam.toml files.
17
+ *
18
+ * @remarks
19
+ * This is a helper function that creates a policy pre-configured to match
20
+ * gleam.toml files. The handler receives the parsed TOML content.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const MyGleamPolicy = defineGleamPolicy({
25
+ * name: "MyGleamPolicy",
26
+ * description: "Ensures gleam.toml has required fields",
27
+ * handler: async (toml, { file }) => {
28
+ * if (!toml.name) {
29
+ * return { error: "Missing name field", fixable: false };
30
+ * }
31
+ * return true;
32
+ * },
33
+ * });
34
+ * ```
35
+ *
36
+ * @alpha
37
+ */
38
+ export function defineGleamPolicy(args) {
39
+ const { name, description, handler: gleamHandler, defaultConfig } = args;
40
+ return {
41
+ name,
42
+ description,
43
+ match: GleamTomlRegexMatch,
44
+ defaultConfig,
45
+ handler: function* (innerArgs) {
46
+ const content = yield* call(() => readFile(resolve(innerArgs.root, innerArgs.file), "utf-8"));
47
+ const toml = yield* call(() => parseToml(content));
48
+ const result = gleamHandler(toml, innerArgs);
49
+ if (result instanceof Promise) {
50
+ return yield* call(() => result);
51
+ }
52
+ if (isOperation(result)) {
53
+ return yield* result;
54
+ }
55
+ throw new Error(`Unexpected handler result type: ${typeof result}`);
56
+ },
57
+ };
58
+ }
59
+ //# sourceMappingURL=defineGleamPolicy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineGleamPolicy.js","sourceRoot":"","sources":["../../src/policyDefiners/defineGleamPolicy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAkB,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAM/D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAsB5C;;GAEG;AACH,SAAS,WAAW,CAAI,KAAc;IACrC,OAAO,CACN,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,OAAQ,KAA2B,CAAC,IAAI,KAAK,UAAU,CACvD,CAAC;AACH,CAAC;AA6BD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,iBAAiB,CAChC,IAA8B;IAE9B,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC;IACzE,OAAO;QACN,IAAI;QACJ,WAAW;QACX,KAAK,EAAE,mBAAmB;QAC1B,aAAa;QACb,OAAO,EAAE,QAAQ,CAAC,EAAE,SAAS;YAC5B,MAAM,OAAO,GAAW,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CACxC,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAC1D,CAAC;YACF,MAAM,IAAI,GAAc,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;YAC9D,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;YAE7C,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;gBAC/B,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,CAAC;YAClC,CAAC;YACD,IAAI,WAAW,CAAsB,MAAM,CAAC,EAAE,CAAC;gBAC9C,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC;YACtB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,mCAAmC,OAAO,MAAM,EAAE,CAAC,CAAC;QACrE,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { call, type Operation } from \"effection\";\nimport { resolve } from \"pathe\";\nimport { GleamTomlRegexMatch } from \"../policies/constants.js\";\nimport type {\n\tPolicyArgs,\n\tPolicyHandlerResult,\n\tPolicyShape,\n} from \"../policy.js\";\nimport { parseToml } from \"./tomlParser.js\";\n\n/**\n * A parsed gleam.toml represented as a record of sections and values.\n *\n * @alpha\n */\nexport type GleamToml = Record<string, unknown>;\n\n/**\n * A policy handler for gleam.toml policies.\n *\n * @remarks\n * Receives the parsed TOML content and the standard policy arguments.\n *\n * @alpha\n */\nexport type GleamTomlHandler<C> = (\n\ttoml: GleamToml,\n\targs: PolicyArgs<C>,\n) => Operation<PolicyHandlerResult> | Promise<PolicyHandlerResult>;\n\n/**\n * Type guard to check if a value is an Effection Operation (generator).\n */\nfunction isOperation<T>(value: unknown): value is Operation<T> {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"next\" in value &&\n\t\ttypeof (value as { next: unknown }).next === \"function\"\n\t);\n}\n\n/**\n * Input arguments for defining a gleam.toml policy.\n *\n * @alpha\n */\nexport interface DefineGleamPolicyArgs<C> {\n\t/**\n\t * The name of the policy.\n\t */\n\tname: string;\n\n\t/**\n\t * A description of the policy's purpose.\n\t */\n\tdescription: string;\n\n\t/**\n\t * The handler function that receives the parsed gleam.toml and policy arguments.\n\t */\n\thandler: GleamTomlHandler<C>;\n\n\t/**\n\t * Optional default configuration for the policy.\n\t */\n\tdefaultConfig?: C;\n}\n\n/**\n * Define a repo policy for gleam.toml files.\n *\n * @remarks\n * This is a helper function that creates a policy pre-configured to match\n * gleam.toml files. The handler receives the parsed TOML content.\n *\n * @example\n * ```typescript\n * const MyGleamPolicy = defineGleamPolicy({\n * name: \"MyGleamPolicy\",\n * description: \"Ensures gleam.toml has required fields\",\n * handler: async (toml, { file }) => {\n * if (!toml.name) {\n * return { error: \"Missing name field\", fixable: false };\n * }\n * return true;\n * },\n * });\n * ```\n *\n * @alpha\n */\nexport function defineGleamPolicy<C = undefined>(\n\targs: DefineGleamPolicyArgs<C>,\n): PolicyShape<C> {\n\tconst { name, description, handler: gleamHandler, defaultConfig } = args;\n\treturn {\n\t\tname,\n\t\tdescription,\n\t\tmatch: GleamTomlRegexMatch,\n\t\tdefaultConfig,\n\t\thandler: function* (innerArgs) {\n\t\t\tconst content: string = yield* call(() =>\n\t\t\t\treadFile(resolve(innerArgs.root, innerArgs.file), \"utf-8\"),\n\t\t\t);\n\t\t\tconst toml: GleamToml = yield* call(() => parseToml(content));\n\t\t\tconst result = gleamHandler(toml, innerArgs);\n\n\t\t\tif (result instanceof Promise) {\n\t\t\t\treturn yield* call(() => result);\n\t\t\t}\n\t\t\tif (isOperation<PolicyHandlerResult>(result)) {\n\t\t\t\treturn yield* result;\n\t\t\t}\n\t\t\tthrow new Error(`Unexpected handler result type: ${typeof result}`);\n\t\t},\n\t};\n}\n"]}