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 _resolveFrom = _interopRequireDefault(require("resolve-from"));
|
12 |
|
13 | var _chalk = _interopRequireDefault(require("chalk"));
|
14 |
|
15 | var _messages = require("./messages");
|
16 |
|
17 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | let camelToPkgJsonField = {
|
23 | main: "main",
|
24 | module: "module",
|
25 | umdMain: "umd:main",
|
26 | browser: "browser",
|
27 | reactNative: "react-native"
|
28 | };
|
29 |
|
30 | async function fixPackage(pkg) {
|
31 | if (pkg.entrypoints.length === 0) {
|
32 | throw new _errors.FatalError(_messages.errors.noEntrypoints, pkg.name);
|
33 | }
|
34 |
|
35 | let fields = {
|
36 | main: true,
|
37 | module: pkg.entrypoints.some(x => x.module),
|
38 | umdMain: pkg.entrypoints.some(x => x.umdMain),
|
39 | browser: pkg.entrypoints.some(x => x.browser),
|
40 | reactNative: pkg.entrypoints.some(x => x.reactNative)
|
41 | };
|
42 | Object.keys(fields).filter(x => fields[x]).forEach(field => {
|
43 | pkg.setFieldOnEntrypoints(field);
|
44 | });
|
45 | return (await Promise.all(pkg.entrypoints.map(x => x.save()))).some(x => x);
|
46 | }
|
47 |
|
48 | let unsafeRequire = require;
|
49 |
|
50 | function validatePackage(pkg) {
|
51 | if (pkg.entrypoints.length === 0) {
|
52 | throw new _errors.FatalError(_messages.errors.noEntrypoints, pkg.name);
|
53 | }
|
54 |
|
55 | let fields = {
|
56 |
|
57 |
|
58 |
|
59 | module: !!pkg.entrypoints[0].module,
|
60 | umdMain: !!pkg.entrypoints[0].umdMain,
|
61 | browser: !!pkg.entrypoints[0].browser,
|
62 | reactNative: !!pkg.entrypoints[0].reactNative
|
63 | };
|
64 | pkg.entrypoints.forEach(entrypoint => {
|
65 | Object.keys(fields).forEach(field => {
|
66 | if (
|
67 | entrypoint[field] && !fields[field]) {
|
68 | 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.name);
|
69 | }
|
70 |
|
71 | if (
|
72 | !entrypoint[field] && fields[field]) {
|
73 | 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.name);
|
74 | }
|
75 | });
|
76 | });
|
77 |
|
78 | if (fields.umdMain) {
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | let sortaAllDeps = new Set([...(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : []), ...(pkg.dependencies ? Object.keys(pkg.dependencies) : [])]);
|
85 |
|
86 | for (let depName in pkg.dependencies) {
|
87 | let depPkgJson = unsafeRequire((0, _resolveFrom.default)(pkg.directory, depName + "/package.json"));
|
88 |
|
89 | if (depPkgJson.peerDependencies) {
|
90 | for (let pkgName in depPkgJson.peerDependencies) {
|
91 | if (!sortaAllDeps.has(pkgName)) {
|
92 | throw new _errors.FatalError(`the package ${_chalk.default.blue(pkg.name)} depends on ${_chalk.default.blue(depName)} which has a peerDependency on ${_chalk.default.blue(pkgName)} but ${_chalk.default.blue(pkgName)} is not specified in the dependencies or peerDependencies of ${_chalk.default.blue(pkg.name)}. please add ${_chalk.default.blue(pkgName)} to the dependencies or peerDependencies of ${_chalk.default.blue(pkg.name)}`, pkg.name);
|
93 | }
|
94 | }
|
95 | }
|
96 | }
|
97 | }
|
98 | } |
\ | No newline at end of file |