-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpackage-manifest.ts
375 lines (347 loc) · 11.5 KB
/
package-manifest.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import {
ManifestFieldNames as PackageManifestFieldNames,
ManifestDependencyFieldNames as PackageManifestDependenciesFieldNames,
} from '@metamask/action-utils';
import { isPlainObject } from '@metamask/utils';
import path from 'path';
import { readJsonObjectFile } from './fs';
import { isTruthyString } from './misc-utils';
import { isValidSemver, SemVer } from './semver';
export { PackageManifestFieldNames, PackageManifestDependenciesFieldNames };
/**
* An unverified representation of the data in a package's `package.json`.
*/
export type UnvalidatedPackageManifest = Readonly<Record<string, any>>;
/**
* A type-checked representation of the data in a package's `package.json`.
*
* @property name - The name of the package.
* @property version - The version of the package.
* @property private - Whether the package is private.
* @property workspaces - Paths to subpackages within the package.
* @property bundledDependencies - The set of packages that are expected to be
* bundled when publishing the package.
*/
export type ValidatedPackageManifest = {
readonly [PackageManifestFieldNames.Name]: string;
readonly [PackageManifestFieldNames.Version]: SemVer;
readonly [PackageManifestFieldNames.Private]: boolean;
readonly [PackageManifestFieldNames.Workspaces]: string[];
readonly [PackageManifestDependenciesFieldNames.Production]: Record<
string,
string
>;
readonly [PackageManifestDependenciesFieldNames.Peer]: Record<string, string>;
};
/**
* Constructs a validation error message for a field within the manifest.
*
* @param args - The arguments.
* @param args.manifest - The manifest data that's invalid.
* @param args.parentDirectory - The directory of the package to which the
* manifest belongs.
* @param args.fieldName - The name of the field in the manifest.
* @param args.verbPhrase - Either the fact that the field is invalid or an
* explanation for why it is invalid.
* @returns The error message.
*/
function buildPackageManifestFieldValidationErrorMessage({
manifest,
parentDirectory,
fieldName,
verbPhrase,
}: {
manifest: UnvalidatedPackageManifest;
parentDirectory: string;
fieldName: keyof UnvalidatedPackageManifest;
verbPhrase: string;
}) {
const subject = isTruthyString(manifest[PackageManifestFieldNames.Name])
? `The value of "${fieldName}" in the manifest for "${
manifest[PackageManifestFieldNames.Name]
}"`
: `The value of "${fieldName}" in the manifest located at "${parentDirectory}"`;
return `${subject} ${verbPhrase}`;
}
/**
* Object that includes checks for validating fields within a manifest
* along with error messages if those validations fail.
*/
const schemata = {
[PackageManifestFieldNames.Name]: {
validate: isTruthyString,
errorMessage: 'must be a non-empty string',
},
[PackageManifestFieldNames.Version]: {
validate: isValidPackageManifestVersionField,
errorMessage: 'must be a valid SemVer version string',
},
[PackageManifestFieldNames.Workspaces]: {
validate: isValidPackageManifestWorkspacesField,
errorMessage: 'must be an array of non-empty strings (if present)',
},
[PackageManifestFieldNames.Private]: {
validate: isValidPackageManifestPrivateField,
errorMessage: 'must be true or false (if present)',
},
[PackageManifestDependenciesFieldNames.Production]: {
validate: isValidPackageManifestDependenciesField,
errorMessage: 'must be a valid dependencies field',
},
[PackageManifestDependenciesFieldNames.Peer]: {
validate: isValidPackageManifestDependenciesField,
errorMessage: 'must be a valid peerDependencies field',
},
};
/**
* Retrieves and validates the "name" field within the package manifest object.
*
* @param manifest - The manifest object.
* @param parentDirectory - The directory in which the manifest lives.
* @returns The value of the "name" field.
* @throws If the value of the field is not a truthy string.
*/
export function readPackageManifestNameField(
manifest: UnvalidatedPackageManifest,
parentDirectory: string,
): string {
const fieldName = PackageManifestFieldNames.Name;
const value = manifest[fieldName];
const schema = schemata[fieldName];
if (!schema.validate(value)) {
throw new Error(
buildPackageManifestFieldValidationErrorMessage({
manifest,
parentDirectory,
fieldName: PackageManifestFieldNames.Name,
verbPhrase: schema.errorMessage,
}),
);
}
return value;
}
/**
* Type guard to ensure that the value of the "version" field of a manifest is
* valid.
*
* @param version - The value to check.
* @returns Whether the version is a valid SemVer version string.
*/
function isValidPackageManifestVersionField(
version: unknown,
): version is string {
return isTruthyString(version) && isValidSemver(version);
}
/**
* Retrieves and validates the "version" field within the package manifest
* object.
*
* @param manifest - The manifest object.
* @param parentDirectory - The directory in which the manifest lives.
* @returns The value of the "version" field wrapped in a SemVer object.
* @throws If the value of the field is not a valid SemVer version string.
*/
export function readPackageManifestVersionField(
manifest: UnvalidatedPackageManifest,
parentDirectory: string,
): SemVer {
const fieldName = PackageManifestFieldNames.Version;
const value = manifest[fieldName];
const schema = schemata[fieldName];
if (!schema.validate(value)) {
throw new Error(
buildPackageManifestFieldValidationErrorMessage({
manifest,
parentDirectory,
fieldName: PackageManifestFieldNames.Version,
verbPhrase: schema.errorMessage,
}),
);
}
return new SemVer(value);
}
/**
* Type guard to ensure that the value of the "workspaces" field of a manifest
* is valid.
*
* @param workspaces - The value to check.
* @returns Whether the value is an array of truthy strings.
*/
function isValidPackageManifestWorkspacesField(
workspaces: unknown,
): workspaces is string[] | undefined {
return (
workspaces === undefined ||
(Array.isArray(workspaces) &&
workspaces.every((workspace) => isTruthyString(workspace)))
);
}
/**
* Retrieves and validates the "workspaces" field within the package manifest
* object.
*
* @param manifest - The manifest object.
* @param parentDirectory - The directory in which the manifest lives.
* @returns The value of the "workspaces" field, or an empty array if no such
* field exists.
* @throws If the value of the field is not an array of truthy strings.
*/
export function readPackageManifestWorkspacesField(
manifest: UnvalidatedPackageManifest,
parentDirectory: string,
): string[] {
const fieldName = PackageManifestFieldNames.Workspaces;
const value = manifest[fieldName];
const schema = schemata[fieldName];
if (!schema.validate(value)) {
throw new Error(
buildPackageManifestFieldValidationErrorMessage({
manifest,
parentDirectory,
fieldName,
verbPhrase: schema.errorMessage,
}),
);
}
return value ?? [];
}
/**
* Type guard to ensure that the value of the "private" field of a manifest is
* valid.
*
* @param privateValue - The value to check.
* @returns Whether the value is undefined, true, or false.
*/
function isValidPackageManifestPrivateField(
privateValue: unknown,
): privateValue is boolean | undefined {
return (
privateValue === undefined ||
privateValue === true ||
privateValue === false
);
}
/**
* Retrieves and validates the "private" field within the package manifest
* object.
*
* @param manifest - The manifest object.
* @param parentDirectory - The directory in which the manifest lives.
* @returns The value of the "private" field, or false if no such field exists.
* @throws If the value of the field is not true or false.
*/
export function readPackageManifestPrivateField(
manifest: UnvalidatedPackageManifest,
parentDirectory: string,
): boolean {
const fieldName = PackageManifestFieldNames.Private;
const value = manifest[fieldName];
const schema = schemata[fieldName];
if (!schema.validate(value)) {
throw new Error(
buildPackageManifestFieldValidationErrorMessage({
manifest,
parentDirectory,
fieldName,
verbPhrase: schema.errorMessage,
}),
);
}
return value ?? false;
}
/**
* Type guard to ensure that the value of the "dependencies" or "peerDependencies" field of a manifest is
* valid.
*
* @param depsValue - The value to check.
* @returns Whether the value is has valid values.
*/
function isValidPackageManifestDependenciesField(
depsValue: unknown,
): depsValue is Record<string, string> {
return (
depsValue === undefined ||
(isPlainObject(depsValue) &&
Object.entries(depsValue).every(([pkgName, version]) => {
return (
isTruthyString(pkgName) && isValidPackageManifestVersionField(version)
);
}))
);
}
/**
* Retrieves and validates the "dependencies" or "peerDependencies" fields within the package manifest
* object.
*
* @param manifest - The manifest object.
* @param parentDirectory - The directory in which the manifest lives.
* @param fieldName - The field name "dependencies" or "peerDependencies".
* @returns The value of the "dependencies" or "peerDependencies" field.
* @throws If the value of the field is not valid.
*/
export function readPackageManifestDependenciesField(
manifest: UnvalidatedPackageManifest,
parentDirectory: string,
fieldName:
| PackageManifestDependenciesFieldNames.Production
| PackageManifestDependenciesFieldNames.Peer,
): Record<string, string> {
const value = manifest[fieldName];
const schema = schemata[fieldName];
if (!schema.validate(value)) {
throw new Error(
buildPackageManifestFieldValidationErrorMessage({
manifest,
parentDirectory,
fieldName,
verbPhrase: schema.errorMessage,
}),
);
}
return value || {};
}
/**
* Reads the package manifest at the given path, verifying key data within the
* manifest.
*
* @param manifestPath - The path of the manifest file.
* @returns The correctly typed version of the manifest.
* @throws If key data within the manifest is missing (currently `name` and
* `version`) or the value of any other fields is unexpected.
*/
export async function readPackageManifest(manifestPath: string): Promise<{
unvalidated: UnvalidatedPackageManifest;
validated: ValidatedPackageManifest;
}> {
const unvalidated = await readJsonObjectFile(manifestPath);
const parentDirectory = path.dirname(manifestPath);
const name = readPackageManifestNameField(unvalidated, parentDirectory);
const version = readPackageManifestVersionField(unvalidated, parentDirectory);
const workspaces = readPackageManifestWorkspacesField(
unvalidated,
parentDirectory,
);
const privateValue = readPackageManifestPrivateField(
unvalidated,
parentDirectory,
);
const dependencies = readPackageManifestDependenciesField(
unvalidated,
parentDirectory,
PackageManifestDependenciesFieldNames.Production,
);
const peerDependencies = readPackageManifestDependenciesField(
unvalidated,
parentDirectory,
PackageManifestDependenciesFieldNames.Peer,
);
const validated = {
[PackageManifestFieldNames.Name]: name,
[PackageManifestFieldNames.Version]: version,
[PackageManifestFieldNames.Workspaces]: workspaces,
[PackageManifestFieldNames.Private]: privateValue,
[PackageManifestDependenciesFieldNames.Production]: dependencies,
[PackageManifestDependenciesFieldNames.Peer]: peerDependencies,
};
return { unvalidated, validated };
}