UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.fixPackage = fixPackage;
7exports.validatePackage = validatePackage;
8
9var _errors = require("./errors");
10
11var _messages = require("./messages");
12
13/*::
14import { Package } from "./package";
15*/
16let camelToPkgJsonField = {
17 main: "main",
18 module: "module",
19 umdMain: "umd:main",
20 browser: "browser",
21 reactNative: "react-native"
22};
23
24async function fixPackage(pkg) {
25 if (pkg.entrypoints.length === 0) {
26 throw new _errors.FatalError(_messages.errors.noEntrypoints, pkg);
27 }
28
29 let fields = {
30 main: true,
31 module: pkg.entrypoints.some(x => x.module),
32 umdMain: pkg.entrypoints.some(x => x.umdMain),
33 browser: pkg.entrypoints.some(x => x.browser),
34 reactNative: pkg.entrypoints.some(x => x.reactNative)
35 };
36 Object.keys(fields).filter(x => fields[x]).forEach(field => {
37 pkg.setFieldOnEntrypoints(field);
38 });
39 return (await Promise.all(pkg.entrypoints.map(x => x.save()))).some(x => x);
40}
41
42function validatePackage(pkg) {
43 if (pkg.entrypoints.length === 0) {
44 throw new _errors.FatalError(_messages.errors.noEntrypoints, pkg);
45 }
46
47 let fields = {
48 // main is intentionally not here, since it's always required
49 // it will be validated in validateEntrypoint and the case
50 // which this function validates will never happen
51 module: !!pkg.entrypoints[0].module,
52 umdMain: !!pkg.entrypoints[0].umdMain,
53 browser: !!pkg.entrypoints[0].browser,
54 reactNative: !!pkg.entrypoints[0].reactNative
55 };
56 pkg.entrypoints.forEach(entrypoint => {
57 Object.keys(fields).forEach(field => {
58 if ( // $FlowFixMe
59 entrypoint[field] && !fields[field]) {
60 throw new _errors.FixableError(`${pkg.entrypoints[0].name} has a ${camelToPkgJsonField[field]} build but ${entrypoint.name} does not have a ${camelToPkgJsonField[field]} build. Entrypoints in a package must either all have a particular build type or all not have a particular build type.`, pkg);
61 }
62
63 if ( // $FlowFixMe
64 !entrypoint[field] && fields[field]) {
65 throw new _errors.FixableError(`${entrypoint.name} has a ${camelToPkgJsonField[field]} build but ${pkg.entrypoints[0].name} does not have a ${camelToPkgJsonField[field]} build. Entrypoints in a package must either all have a particular build type or all not have a particular build type.`, pkg);
66 }
67 });
68 });
69}
\No newline at end of file