repopo 0.9.0 → 0.10.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.
- package/CHANGELOG.md +21 -0
- package/esm/commands/check.d.ts +1 -0
- package/esm/commands/check.d.ts.map +1 -1
- package/esm/commands/check.js +48 -11
- package/esm/commands/check.js.map +1 -1
- package/esm/config.d.ts +19 -4
- package/esm/config.d.ts.map +1 -1
- package/esm/config.js +2 -3
- package/esm/config.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +6 -1
- package/esm/index.js.map +1 -1
- package/esm/makePolicy.d.ts +50 -8
- package/esm/makePolicy.d.ts.map +1 -1
- package/esm/makePolicy.js +64 -4
- package/esm/makePolicy.js.map +1 -1
- package/esm/policies/ConditionalScripts.d.ts +94 -0
- package/esm/policies/ConditionalScripts.d.ts.map +1 -0
- package/esm/policies/ConditionalScripts.js +144 -0
- package/esm/policies/ConditionalScripts.js.map +1 -0
- package/esm/policies/ExactScripts.d.ts +51 -0
- package/esm/policies/ExactScripts.d.ts.map +1 -0
- package/esm/policies/ExactScripts.js +107 -0
- package/esm/policies/ExactScripts.js.map +1 -0
- package/esm/policies/HtmlFileHeaders.d.ts +1 -1
- package/esm/policies/HtmlFileHeaders.d.ts.map +1 -1
- package/esm/policies/JsTsFileHeaders.d.ts +1 -1
- package/esm/policies/JsTsFileHeaders.d.ts.map +1 -1
- package/esm/policies/MutuallyExclusiveScripts.d.ts +46 -0
- package/esm/policies/MutuallyExclusiveScripts.d.ts.map +1 -0
- package/esm/policies/MutuallyExclusiveScripts.js +52 -0
- package/esm/policies/MutuallyExclusiveScripts.js.map +1 -0
- package/esm/policies/NoPrivateWorkspaceDependencies.d.ts +1 -1
- package/esm/policies/NoPrivateWorkspaceDependencies.d.ts.map +1 -1
- package/esm/policies/PackageAllowedScopes.d.ts +1 -1
- package/esm/policies/PackageAllowedScopes.d.ts.map +1 -1
- package/esm/policies/PackageEsmType.d.ts +1 -1
- package/esm/policies/PackageEsmType.d.ts.map +1 -1
- package/esm/policies/PackageFolderName.d.ts +1 -1
- package/esm/policies/PackageFolderName.d.ts.map +1 -1
- package/esm/policies/PackageJsonProperties.d.ts +1 -1
- package/esm/policies/PackageJsonProperties.d.ts.map +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts +1 -1
- package/esm/policies/PackageJsonRepoDirectoryProperty.d.ts.map +1 -1
- package/esm/policies/PackageJsonSorted.d.ts +1 -1
- package/esm/policies/PackageJsonSorted.d.ts.map +1 -1
- package/esm/policies/PackageLicense.d.ts +1 -1
- package/esm/policies/PackageLicense.d.ts.map +1 -1
- package/esm/policies/PackagePrivateField.d.ts +1 -1
- package/esm/policies/PackagePrivateField.d.ts.map +1 -1
- package/esm/policies/PackageReadme.d.ts +1 -1
- package/esm/policies/PackageReadme.d.ts.map +1 -1
- package/esm/policies/PackageScripts.d.ts +1 -1
- package/esm/policies/PackageScripts.d.ts.map +1 -1
- package/esm/policies/PackageTestScripts.d.ts +1 -1
- package/esm/policies/PackageTestScripts.d.ts.map +1 -1
- package/esm/policies/RequiredScripts.d.ts +71 -0
- package/esm/policies/RequiredScripts.d.ts.map +1 -0
- package/esm/policies/RequiredScripts.js +120 -0
- package/esm/policies/RequiredScripts.js.map +1 -0
- package/esm/policies/ScriptContains.d.ts +64 -0
- package/esm/policies/ScriptContains.d.ts.map +1 -0
- package/esm/policies/ScriptContains.js +62 -0
- package/esm/policies/ScriptContains.js.map +1 -0
- package/esm/policies.d.ts +6 -1
- package/esm/policies.d.ts.map +1 -1
- package/esm/policies.js +18 -0
- package/esm/policies.js.map +1 -1
- package/esm/policy.d.ts +180 -86
- package/esm/policy.d.ts.map +1 -1
- package/esm/policy.js +103 -21
- package/esm/policy.js.map +1 -1
- package/esm/policyDefiners/defineFileHeaderPolicy.d.ts +7 -2
- package/esm/policyDefiners/defineFileHeaderPolicy.d.ts.map +1 -1
- package/esm/policyDefiners/defineFileHeaderPolicy.js +5 -0
- package/esm/policyDefiners/defineFileHeaderPolicy.js.map +1 -1
- package/esm/policyDefiners/definePackagePolicy.d.ts +18 -5
- package/esm/policyDefiners/definePackagePolicy.d.ts.map +1 -1
- package/esm/policyDefiners/definePackagePolicy.js +14 -4
- package/esm/policyDefiners/definePackagePolicy.js.map +1 -1
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/esm/policy.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Operation } from "effection";
|
|
2
|
-
import type { RequireExactlyOne } from "type-fest";
|
|
3
2
|
/**
|
|
4
3
|
* A type representing a policy name.
|
|
5
4
|
*
|
|
@@ -7,11 +6,11 @@ import type { RequireExactlyOne } from "type-fest";
|
|
|
7
6
|
*/
|
|
8
7
|
export type PolicyName = string;
|
|
9
8
|
/**
|
|
10
|
-
* Arguments passed to policy functions.
|
|
9
|
+
* Arguments passed to policy handler functions.
|
|
11
10
|
*
|
|
12
11
|
* @alpha
|
|
13
12
|
*/
|
|
14
|
-
export interface
|
|
13
|
+
export interface PolicyArgs<C = void> {
|
|
15
14
|
/**
|
|
16
15
|
* Path to the file, relative to the repo root.
|
|
17
16
|
*/
|
|
@@ -25,171 +24,266 @@ export interface PolicyFunctionArguments<C> {
|
|
|
25
24
|
*/
|
|
26
25
|
resolve: boolean;
|
|
27
26
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* Note that the handler function receives the config as an argument.
|
|
27
|
+
* Optional configuration for the policy.
|
|
31
28
|
*/
|
|
32
29
|
config?: C | undefined;
|
|
33
30
|
}
|
|
34
31
|
/**
|
|
35
|
-
*
|
|
32
|
+
* @deprecated Use {@link PolicyArgs} instead.
|
|
33
|
+
* @alpha
|
|
34
|
+
*/
|
|
35
|
+
export type PolicyFunctionArguments<C> = PolicyArgs<C>;
|
|
36
|
+
/**
|
|
37
|
+
* A policy error returned when a file fails a policy check.
|
|
36
38
|
*
|
|
37
|
-
* @
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
* @alpha
|
|
40
|
+
*/
|
|
41
|
+
export interface PolicyError {
|
|
42
|
+
/**
|
|
43
|
+
* The error message describing what failed.
|
|
44
|
+
*/
|
|
45
|
+
error: string;
|
|
46
|
+
/**
|
|
47
|
+
* Set to `true` if the policy violation can be fixed automatically.
|
|
48
|
+
*/
|
|
49
|
+
fixable?: boolean | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Set to `true` if the violation was successfully fixed (only set when resolve=true).
|
|
52
|
+
*/
|
|
53
|
+
fixed?: boolean | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* An optional string that tells the user how to manually fix the failure.
|
|
56
|
+
*/
|
|
57
|
+
manualFix?: string | undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The result of a policy handler.
|
|
61
|
+
* Returns `true` if the file passes the policy, or a {@link PolicyError} if it fails.
|
|
62
|
+
*
|
|
63
|
+
* @alpha
|
|
64
|
+
*/
|
|
65
|
+
export type PolicyResult = true | PolicyError;
|
|
66
|
+
/**
|
|
67
|
+
* A policy failure (legacy format).
|
|
68
|
+
*
|
|
69
|
+
* @deprecated Use {@link PolicyError} instead, which has a simpler API.
|
|
70
|
+
*
|
|
71
|
+
* @alpha
|
|
72
|
+
*/
|
|
73
|
+
export interface PolicyFailure {
|
|
74
|
+
/**
|
|
75
|
+
* Name of the policy that failed.
|
|
76
|
+
*/
|
|
77
|
+
name: PolicyName;
|
|
78
|
+
/**
|
|
79
|
+
* Path to the file that failed the policy.
|
|
80
|
+
*/
|
|
81
|
+
file: string;
|
|
82
|
+
/**
|
|
83
|
+
* Set to `true` if the policy can be fixed automatically.
|
|
84
|
+
*/
|
|
85
|
+
autoFixable?: boolean | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* An optional error message accompanying the failure.
|
|
88
|
+
*/
|
|
89
|
+
errorMessages: string[];
|
|
90
|
+
/**
|
|
91
|
+
* An optional string that tells the user how to fix the failure(s).
|
|
92
|
+
*/
|
|
93
|
+
manualFix?: string | undefined;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The result of an automatic fix for a failing policy (legacy format).
|
|
41
97
|
*
|
|
42
|
-
*
|
|
98
|
+
* @deprecated Use {@link PolicyError} with `fixed: boolean` instead.
|
|
43
99
|
*
|
|
44
100
|
* @alpha
|
|
45
101
|
*/
|
|
46
|
-
export
|
|
102
|
+
export interface PolicyFixResult extends PolicyFailure {
|
|
103
|
+
/**
|
|
104
|
+
* Set to true if the failure was resolved by the automated fixer.
|
|
105
|
+
*/
|
|
106
|
+
resolved: boolean;
|
|
107
|
+
}
|
|
47
108
|
/**
|
|
48
|
-
*
|
|
109
|
+
* Result type that accepts both legacy and new formats.
|
|
110
|
+
*
|
|
111
|
+
* @alpha
|
|
112
|
+
*/
|
|
113
|
+
export type PolicyHandlerResult = true | PolicyFailure | PolicyFixResult | PolicyError;
|
|
114
|
+
/**
|
|
115
|
+
* A policy handler function that checks a file against a policy.
|
|
49
116
|
*
|
|
50
117
|
* @remarks
|
|
51
|
-
*
|
|
118
|
+
* Policy handlers can be implemented in two ways:
|
|
52
119
|
* - As an async function returning a Promise
|
|
53
120
|
* - As an Effection generator function returning an Operation
|
|
54
121
|
*
|
|
55
|
-
* Both styles are supported
|
|
122
|
+
* Both styles are supported. Async handlers are automatically wrapped for Effection.
|
|
123
|
+
*
|
|
124
|
+
* Handlers can return either the legacy format ({@link PolicyFailure}) or
|
|
125
|
+
* the new format ({@link PolicyError}). The new format is simpler and recommended
|
|
126
|
+
* for new policies.
|
|
56
127
|
*
|
|
57
128
|
* @alpha
|
|
58
129
|
*/
|
|
59
|
-
export type
|
|
130
|
+
export type PolicyHandler<C = unknown | undefined> = ((args: PolicyArgs<C>) => Promise<PolicyHandlerResult>) | ((args: PolicyArgs<C>) => Operation<PolicyHandlerResult>);
|
|
60
131
|
/**
|
|
61
|
-
* A
|
|
132
|
+
* A standalone resolver function that can fix policy violations (legacy format).
|
|
133
|
+
*
|
|
134
|
+
* @deprecated Use {@link PolicyResolver} instead, which uses {@link PolicyError}.
|
|
62
135
|
*
|
|
63
|
-
*
|
|
64
|
-
|
|
136
|
+
* @alpha
|
|
137
|
+
*/
|
|
138
|
+
export type PolicyStandaloneResolver<C = undefined> = (args: Omit<PolicyArgs<C>, "resolve">) => Promise<PolicyFixResult> | Operation<PolicyFixResult>;
|
|
139
|
+
/**
|
|
140
|
+
* A standalone resolver function using the new format.
|
|
65
141
|
*
|
|
66
|
-
*
|
|
67
|
-
|
|
142
|
+
* @alpha
|
|
143
|
+
*/
|
|
144
|
+
export type PolicyResolver<C = void> = (args: Omit<PolicyArgs<C>, "resolve">) => Promise<PolicyError> | Operation<PolicyError>;
|
|
145
|
+
/**
|
|
146
|
+
* Interface describing the shape of a policy definition.
|
|
68
147
|
*
|
|
69
|
-
* @typeParam C -
|
|
148
|
+
* @typeParam C - Type of configuration object used by the policy
|
|
70
149
|
*
|
|
71
150
|
* @alpha
|
|
72
151
|
*/
|
|
73
|
-
export interface
|
|
152
|
+
export interface PolicyShape<C = void> {
|
|
74
153
|
/**
|
|
75
154
|
* The name of the policy; displayed in UI and used in settings.
|
|
76
155
|
*/
|
|
77
156
|
name: PolicyName;
|
|
78
157
|
/**
|
|
79
|
-
* A
|
|
158
|
+
* A detailed description of the policy and its purpose.
|
|
80
159
|
*/
|
|
81
160
|
description: string;
|
|
82
161
|
/**
|
|
83
|
-
* A regular expression that
|
|
162
|
+
* A regular expression that matches files this policy applies to.
|
|
84
163
|
*/
|
|
85
164
|
match: RegExp;
|
|
86
165
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* @param file - Repo-relative path to the file to check.
|
|
90
|
-
* @param root - Absolute path to the root of the repo.
|
|
91
|
-
* @param resolve - If true, automated policy fixes will be applied. Not all policies support automated fixes.
|
|
92
|
-
* @returns True if the file passed the policy; otherwise a PolicyFailure object will be returned.
|
|
166
|
+
* The handler function that checks if a file complies with the policy.
|
|
93
167
|
*/
|
|
94
168
|
handler: PolicyHandler<C>;
|
|
95
169
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* @param file - Repo-relative path to the file to check.
|
|
99
|
-
* @param root - Absolute path to the root of the repo.
|
|
100
|
-
* @returns true if the file passed the policy; otherwise a PolicyFailure object will be returned.
|
|
170
|
+
* An optional resolver function that can automatically fix violations.
|
|
101
171
|
*/
|
|
102
172
|
resolver?: PolicyStandaloneResolver<C> | undefined;
|
|
103
173
|
/**
|
|
104
|
-
* A default
|
|
174
|
+
* A default configuration that will be used if none is provided.
|
|
105
175
|
*/
|
|
106
176
|
defaultConfig?: C | undefined;
|
|
107
177
|
}
|
|
108
178
|
/**
|
|
179
|
+
* Abstract base class for creating policies with object-based construction.
|
|
180
|
+
*
|
|
181
|
+
* @remarks
|
|
182
|
+
* This class accepts a {@link PolicyShape} object in its constructor,
|
|
183
|
+
* making it easier to add new optional properties in the future without breaking changes.
|
|
184
|
+
*
|
|
109
185
|
* @alpha
|
|
110
186
|
*/
|
|
111
|
-
export
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* The config that is applied to the policy instance.
|
|
121
|
-
*/
|
|
122
|
-
config?: C | undefined;
|
|
187
|
+
export declare abstract class Policy<C = void> implements PolicyShape<C> {
|
|
188
|
+
readonly name: string;
|
|
189
|
+
readonly description: string;
|
|
190
|
+
readonly match: RegExp;
|
|
191
|
+
readonly handler: PolicyHandler<C>;
|
|
192
|
+
readonly defaultConfig?: C | undefined;
|
|
193
|
+
readonly resolver?: PolicyStandaloneResolver<C> | undefined;
|
|
194
|
+
constructor(definition: PolicyShape<C>);
|
|
123
195
|
}
|
|
124
196
|
/**
|
|
197
|
+
* @deprecated Use {@link PolicyShape} instead.
|
|
125
198
|
* @alpha
|
|
126
199
|
*/
|
|
127
|
-
export type
|
|
200
|
+
export type PolicyDefinition<C = undefined> = PolicyShape<C>;
|
|
128
201
|
/**
|
|
129
|
-
* A policy
|
|
202
|
+
* A policy instance with configuration and exclusion settings applied.
|
|
203
|
+
*
|
|
204
|
+
* @typeParam C - Type of configuration object used by the policy
|
|
130
205
|
*
|
|
131
206
|
* @alpha
|
|
132
207
|
*/
|
|
133
|
-
export interface
|
|
208
|
+
export interface ConfiguredPolicy<C = void> extends PolicyShape<C> {
|
|
134
209
|
/**
|
|
135
|
-
*
|
|
210
|
+
* The configuration applied to this policy instance.
|
|
136
211
|
*/
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Path to the file that failed the policy.
|
|
140
|
-
*/
|
|
141
|
-
file: string;
|
|
212
|
+
config?: C | undefined;
|
|
142
213
|
/**
|
|
143
|
-
*
|
|
214
|
+
* File paths matching these patterns will be excluded from this policy.
|
|
215
|
+
* @deprecated Use `excludeFiles` instead for backward compatibility.
|
|
144
216
|
*/
|
|
145
|
-
|
|
217
|
+
exclude?: (string | RegExp)[] | undefined;
|
|
146
218
|
/**
|
|
147
|
-
*
|
|
219
|
+
* File paths matching these patterns will be excluded from this policy.
|
|
148
220
|
*/
|
|
149
|
-
|
|
221
|
+
excludeFiles?: (string | RegExp)[] | undefined;
|
|
150
222
|
/**
|
|
151
|
-
*
|
|
223
|
+
* Internal normalized handler (always an Effection Operation).
|
|
224
|
+
* This is set by the policy() function and used by the execution engine.
|
|
225
|
+
* @internal
|
|
152
226
|
*/
|
|
153
|
-
|
|
227
|
+
_internalHandler?: (args: PolicyArgs<C>) => Operation<PolicyHandlerResult>;
|
|
154
228
|
}
|
|
155
229
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
230
|
+
* Settings for configuring a policy instance.
|
|
158
231
|
* @alpha
|
|
159
232
|
*/
|
|
160
|
-
export interface
|
|
233
|
+
export interface PolicyInstanceSettings<C> {
|
|
161
234
|
/**
|
|
162
|
-
*
|
|
235
|
+
* An array of strings/regular expressions. File paths that match any of these expressions will be completely excluded
|
|
236
|
+
* from policy.
|
|
237
|
+
*
|
|
238
|
+
* Paths will be matched relative to the root of the repo.
|
|
163
239
|
*/
|
|
164
|
-
|
|
240
|
+
excludeFiles?: (string | RegExp)[];
|
|
241
|
+
/**
|
|
242
|
+
* The config that is applied to the policy instance.
|
|
243
|
+
*/
|
|
244
|
+
config?: C | undefined;
|
|
165
245
|
}
|
|
166
246
|
/**
|
|
247
|
+
* @deprecated Use {@link ConfiguredPolicy} instead.
|
|
248
|
+
* @alpha
|
|
249
|
+
*/
|
|
250
|
+
export type PolicyInstance<C = undefined> = ConfiguredPolicy<C>;
|
|
251
|
+
/**
|
|
252
|
+
* Type guard to check if a result is a {@link PolicyFixResult} (legacy).
|
|
253
|
+
*
|
|
254
|
+
* @deprecated Use {@link isPolicyError} instead when working with the new {@link PolicyError} format.
|
|
255
|
+
*
|
|
167
256
|
* @alpha
|
|
168
257
|
*/
|
|
169
|
-
export type PolicyHandlerResult = true | PolicyFailure | PolicyFixResult;
|
|
170
258
|
export declare function isPolicyFixResult(toCheck: any): toCheck is PolicyFixResult;
|
|
171
259
|
/**
|
|
172
|
-
*
|
|
260
|
+
* Type guard to check if a result is a PolicyError (new format).
|
|
173
261
|
*
|
|
174
262
|
* @alpha
|
|
175
263
|
*/
|
|
176
|
-
export declare
|
|
264
|
+
export declare function isPolicyError(toCheck: any): toCheck is PolicyError;
|
|
177
265
|
/**
|
|
178
|
-
*
|
|
266
|
+
* Type guard to check if a result is a {@link PolicyFailure} (legacy format).
|
|
179
267
|
*
|
|
180
|
-
* @
|
|
181
|
-
* This class accepts a {@link PolicyDefinition} object in its constructor,
|
|
182
|
-
* making it easier to add new optional properties in the future without breaking changes.
|
|
268
|
+
* @deprecated Use {@link isPolicyError} instead when working with the new {@link PolicyError} format.
|
|
183
269
|
*
|
|
184
270
|
* @alpha
|
|
185
271
|
*/
|
|
186
|
-
export declare
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
272
|
+
export declare function isPolicyFailure(toCheck: any): toCheck is PolicyFailure;
|
|
273
|
+
/**
|
|
274
|
+
* Converts a legacy PolicyFailure/PolicyFixResult to the new PolicyError format.
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
export declare function convertLegacyResult(result: PolicyHandlerResult, _file: string): PolicyResult;
|
|
278
|
+
/**
|
|
279
|
+
* Converts a new PolicyError to the legacy PolicyFailure format.
|
|
280
|
+
* @internal
|
|
281
|
+
*/
|
|
282
|
+
export declare function convertToLegacyResult(result: PolicyError, policyName: string, file: string): PolicyFailure | PolicyFixResult;
|
|
283
|
+
/**
|
|
284
|
+
* Default policies included with repopo.
|
|
285
|
+
*
|
|
286
|
+
* @alpha
|
|
287
|
+
*/
|
|
288
|
+
export declare const DefaultPolicies: Policy<any>[];
|
|
195
289
|
//# sourceMappingURL=policy.d.ts.map
|
package/esm/policy.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAK3C;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAEhC;;;;GAIG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,IAAI;IACnC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,MAAM,uBAAuB,C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AM,GACX,YAAY,CAoBd;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACpC,MAAM,EAAE,WAAW,EACnB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,GACV,aAAa,GAAG,eAAe,CAiBjC;AAMD;;;;GAIG;AAEH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,GAAG,CAAC,EAI/B,CAAC"}
|
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
|
|
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,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,gCAAgC,EAAE,MAAM,gDAAgD,CAAC;AAClG,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAuOpE;;;;;;;;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,UAA0B;QAC5C,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;AAiED,+EAA+E;AAC/E,cAAc;AACd,+EAA+E;AAE/E;;;;;;GAMG;AACH,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;AACH,yDAAyD;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B,UAAkB,EAClB,IAAY;IAEZ,MAAM,IAAI,GAAkB;QAC3B,IAAI,EAAE,UAAU;QAChB,IAAI;QACJ,WAAW,EAAE,MAAM,CAAC,OAAO;QAC3B,aAAa,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;QAC7B,SAAS,EAAE,MAAM,CAAC,SAAS;KAC3B,CAAC;IAEF,IAAI,MAAM,CAAC,KAAK,KAAK,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 * 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 * 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 * 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// 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"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
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):
|
|
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,
|
|
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,CAoErC;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,+BAAyB,CAAC"}
|