forked from MetaMask/snaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints.pro
291 lines (263 loc) · 12.8 KB
/
constraints.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
%===============================================================================
% Utility predicates
%===============================================================================
% True if and only if VersionRange is a value that we would expect to see
% following a package in a "*dependencies" field within a `package.json`.
is_valid_version_range(VersionRange) :-
VersionRange = 'workspace:^';
VersionRange = 'workspace:~';
parse_version_range(VersionRange, _, _, _, _).
% Succeeds if Number can be unified with Atom converted to a number; throws if
% not.
atom_to_number(Atom, Number) :-
atom_chars(Atom, Chars),
number_chars(Number, Chars).
% True if and only if Atom can be converted to a number.
is_atom_number(Atom) :-
catch(atom_to_number(Atom, _), _, false).
% True if and only if Modifier can be unified with the leading character of the
% version range ("^" or "~" if present, or "" if not present), Major can be
% unified with the major part of the version string, Minor with the minor, and
% Patch with the patch.
parse_version_range(VersionRange, Modifier, Major, Minor, Patch) :-
% Identify and extract the modifier (^ or ~) from the version string
atom_chars(VersionRange, Chars),
Chars = [PossibleModifier | CharsWithoutPossibleModifier],
(
(
PossibleModifier = '^';
PossibleModifier = '~'
) ->
(
Modifier = PossibleModifier,
CharsWithoutModifier = CharsWithoutPossibleModifier
) ;
(
is_atom_number(PossibleModifier) ->
(
Modifier = '',
CharsWithoutModifier = Chars
) ;
false
)
),
atomic_list_concat(CharsWithoutModifier, '', VersionRangeWithoutModifier),
atomic_list_concat(VersionParts, '.', VersionRangeWithoutModifier),
% Validate version string while extracting each part
length(VersionParts, 3),
nth0(0, VersionParts, MajorAtom),
nth0(1, VersionParts, MinorAtom),
nth0(2, VersionParts, PatchAtom),
atom_to_number(MajorAtom, Major),
atom_to_number(MinorAtom, Minor),
atom_to_number(PatchAtom, Patch).
% True if and only if the first SemVer version range is greater than the second
% SemVer version range. Such a range must match "^MAJOR.MINOR.PATCH",
% "~MAJOR.MINOR.PATCH", "MAJOR.MINOR.PATCH". If two ranges do not have the same
% modifier ("^" or "~"), then they cannot be compared and the first cannot be
% considered as less than the second.
%
% Borrowed from: <https://github.com/npm/node-semver/blob/a7b8722674e2eedfd89960b4155ffddd6a20ee21/classes/semver.js#L107>
npm_version_range_out_of_sync(VersionRange1, VersionRange2) :-
parse_version_range(VersionRange1, VersionRange1Modifier, VersionRange1Major, VersionRange1Minor, VersionRange1Patch),
parse_version_range(VersionRange2, VersionRange2Modifier, VersionRange2Major, VersionRange2Minor, VersionRange2Patch),
VersionRange1Modifier == VersionRange2Modifier,
(
% 2.0.0 > 1.0.0
% 2.0.0 > 1.1.0
% 2.0.0 > 1.0.1
VersionRange1Major @> VersionRange2Major ;
(
VersionRange1Major == VersionRange2Major ,
(
% 1.1.0 > 1.0.0
% 1.1.0 > 1.0.1
VersionRange1Minor @> VersionRange2Minor ;
(
VersionRange1Minor == VersionRange2Minor ,
% 1.0.1 > 1.0.0
VersionRange1Patch @> VersionRange2Patch
)
)
)
).
% Slice a list from From to To.
slice(Left, From, To, Right):-
length(LeftFrom, From),
length([_|LeftTo], To),
append(LeftTo, _, Left),
append(LeftFrom, Right, LeftTo).
% True if and only if the given workspace directory is an example.
is_example(WorkspaceCwd) :-
atomic_list_concat(Parts, '/', WorkspaceCwd),
slice(Parts, 1, 3, RootParts),
atomic_list_concat(RootParts, '/', RootCwd),
RootCwd = 'examples',
WorkspaceCwd \= 'packages/examples',
WorkspaceCwd \= 'packages/examples/packages/invoke-snap'.
% Repeat a value a given number of times. This is useful for generating lists of
% a given length. For example, repeat('foo', 3, Result) will unify Result with
% ['foo', 'foo', 'foo'].
repeat(Value, Amount, Result) :-
length(Result, Amount),
maplist(=(Value), Result).
% Resolve a relative path from a workspace directory.
relative_path(WorkspaceCwd, Path, RelativePath) :-
atomic_list_concat(Parts, '/', WorkspaceCwd),
length(Parts, Distance),
repeat('..', Distance, DistanceParts),
atomic_list_concat(DistanceParts, '/', DistanceString),
atomic_list_concat([DistanceString, Path], '/', RelativePath).
%===============================================================================
% Constraints
%===============================================================================
% All dependency ranges must be recognizable (this makes it possible to apply
% the next two rules effectively).
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, 'a range optionally starting with ^ or ~', DependencyType) :-
workspace_has_dependency(WorkspaceCwd, DependencyIdent, DependencyRange, DependencyType),
\+ is_valid_version_range(DependencyRange).
% All dependency ranges for a package must be synchronized across the monorepo
% (the least version range wins), regardless of which "*dependencies" field
% where the package appears.
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, OtherDependencyRange, DependencyType) :-
workspace_has_dependency(WorkspaceCwd, DependencyIdent, DependencyRange, DependencyType),
workspace_has_dependency(OtherWorkspaceCwd, DependencyIdent, OtherDependencyRange, OtherDependencyType),
WorkspaceCwd \= OtherWorkspaceCwd,
DependencyRange \= OtherDependencyRange,
npm_version_range_out_of_sync(DependencyRange, OtherDependencyRange).
% If a dependency is listed under "dependencies", it should not be listed under
% "devDependencies". We match on the same dependency range so that if a
% dependency is listed under both lists, their versions are synchronized and
% then this constraint will apply and remove the "right" duplicate.
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, null, DependencyType) :-
workspace_has_dependency(WorkspaceCwd, DependencyIdent, DependencyRange, 'dependencies'),
workspace_has_dependency(WorkspaceCwd, DependencyIdent, DependencyRange, DependencyType),
DependencyType == 'devDependencies'.
% Dependencies may not have side effects.
gen_enforced_field(WorkspaceCwd, 'sideEffects', 'false') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% The type definitions entrypoint for the dependency must be `./dist/types/index.d.ts`.
gen_enforced_field(WorkspaceCwd, 'types', './dist/types/index.d.ts') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'exports["."].types', './dist/types/index.d.ts') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% The entrypoint for the dependency must be `./dist/cjs/index.js`.
gen_enforced_field(WorkspaceCwd, 'main', './dist/index.js') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'exports["."].require', './dist/index.js') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% The module entrypoint for the dependency must be `./dist/esm/index.js`.
gen_enforced_field(WorkspaceCwd, 'module', './dist/index.mjs') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'exports["."].import', './dist/index.mjs') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% `package.json` must be exported.
gen_enforced_field(WorkspaceCwd, 'exports["./package.json"]', './package.json') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% The list of files included in the package must only include files generated
% during the build step.
gen_enforced_field(WorkspaceCwd, 'files', ['dist']) :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.',
WorkspaceCwd \= 'packages/snaps-jest',
WorkspaceCwd \= 'packages/snaps-cli',
WorkspaceCwd \= 'packages/snaps-sdk'.
gen_enforced_field(WorkspaceCwd, 'files', ['dist', 'jest-preset.js']) :-
WorkspaceCwd = 'packages/snaps-jest'.
% Dependencies must have a build script.
gen_enforced_field(WorkspaceCwd, 'scripts.build', 'tsup --clean && yarn build:types') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.',
WorkspaceCwd \= 'packages/snaps-simulator',
WorkspaceCwd \= 'packages/snaps-cli',
WorkspaceCwd \= 'packages/snaps-execution-environments'.
gen_enforced_field(WorkspaceCwd, 'scripts.build:types', 'tsc --project tsconfig.build.json') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'scripts.build:ci', 'tsup --clean') :-
\+ is_example(WorkspaceCwd),
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.',
WorkspaceCwd \= 'packages/snaps-simulator'.
% Dependencies must have preview scripts.
gen_enforced_field(WorkspaceCwd, 'scripts.publish:preview', 'yarn npm publish --tag preview') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% Dependencies must have a "publishConfig" field.
gen_enforced_field(WorkspaceCwd, 'publishConfig.access', 'public') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
gen_enforced_field(WorkspaceCwd, 'publishConfig.registry', 'https://registry.npmjs.org/') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
WorkspaceCwd \= '.'.
% The "changelog:validate" script for each published package must run a common
% script with the name of the package as an argument.
gen_enforced_field(WorkspaceCwd, 'scripts.lint:changelog', ChangelogValidationScript) :-
\+ workspace_field(WorkspaceCwd, 'private', true),
workspace_field(WorkspaceCwd, 'name', WorkspacePackageName),
relative_path(WorkspaceCwd, 'scripts/validate-changelog.sh', BaseChangelogValidationScript),
atomic_list_concat([BaseChangelogValidationScript, ' ', WorkspacePackageName], ChangelogValidationScript).
% The "lint:dependencies" script must be the same for all packages.
gen_enforced_field(WorkspaceCwd, 'scripts.lint:dependencies', 'depcheck') :-
WorkspaceCwd \= '.'.
% The "engines.node" field must be the same for all packages.
gen_enforced_field(WorkspaceCwd, 'engines.node', '^18.16 || >=20').
% Ensure all examples have the same scripts.
gen_enforced_field(WorkspaceCwd, 'scripts.build', 'mm-snap build') :-
is_example(WorkspaceCwd),
WorkspaceCwd \= 'packages/examples/packages/wasm',
WorkspaceCwd \= 'packages/examples/packages/browserify-plugin',
WorkspaceCwd \= 'packages/examples/packages/rollup-plugin',
WorkspaceCwd \= 'packages/examples/packages/webpack-plugin'.
gen_enforced_field(WorkspaceCwd, 'scripts.build:clean', 'yarn clean && yarn build') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.start', 'mm-snap watch') :-
is_example(WorkspaceCwd),
WorkspaceCwd \= 'packages/examples/packages/wasm',
WorkspaceCwd \= 'packages/examples/packages/browserify-plugin',
WorkspaceCwd \= 'packages/examples/packages/rollup-plugin',
WorkspaceCwd \= 'packages/examples/packages/webpack-plugin'.
gen_enforced_field(WorkspaceCwd, 'scripts.clean', 'rimraf "dist"') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'yarn test:e2e') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.test:e2e', 'jest') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.lint', 'yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog && yarn lint:dependencies') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.lint:ci', 'yarn lint') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.lint:fix', 'yarn lint:eslint --fix && yarn lint:misc --write') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.lint:eslint', 'eslint . --cache --ext js,ts,jsx,tsx') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'scripts.lint:misc', LintMiscScript) :-
is_example(WorkspaceCwd),
relative_path(WorkspaceCwd, '.gitignore', GitIgnorePath),
atomic_list_concat(['prettier --no-error-on-unmatched-pattern --loglevel warn "**/*.json" "**/*.md" "**/*.html" "!CHANGELOG.md" "!snap.manifest.json" --ignore-path ', GitIgnorePath], LintMiscScript).
% Ensure all examples have the same `main` and `types` fields.
gen_enforced_field(WorkspaceCwd, 'main', './dist/bundle.js') :-
is_example(WorkspaceCwd).
gen_enforced_field(WorkspaceCwd, 'types', null) :-
is_example(WorkspaceCwd).
% Ensure all examples have the same license.
gen_enforced_field(WorkspaceCwd, 'license', '(MIT-0 OR Apache-2.0)') :-
is_example(WorkspaceCwd).