1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.fixPackage = fixPackage;
|
7 | exports.validatePackage = validatePackage;
|
8 |
|
9 | var _errors = require("./errors");
|
10 |
|
11 | var _messages = require("./messages");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let camelToPkgJsonField = {
|
17 | main: "main",
|
18 | module: "module",
|
19 | umdMain: "umd:main",
|
20 | browser: "browser",
|
21 | reactNative: "react-native"
|
22 | };
|
23 |
|
24 | async 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 |
|
42 | function validatePackage(pkg) {
|
43 | if (pkg.entrypoints.length === 0) {
|
44 | throw new _errors.FatalError(_messages.errors.noEntrypoints, pkg);
|
45 | }
|
46 |
|
47 | let fields = {
|
48 |
|
49 |
|
50 |
|
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 (
|
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 (
|
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 |